is it an possible to add a value from an NSMutableString into an NSArray? Whats the snippet?
Actually, Mike is wrong. If you want to instantiate an NSArray with a single NSMutableString object, you can do the following:
NSMutableString *myString; //Assuming your string is here
NSArray *array = [NSArray arrayWithObject:myString];
There is no arrayWithElements in NSArray (see NSArray documentation)
If you want to instantiate an NSArray with a single NSMutableString object, you can do the following:
NSString *myString; //Assuming your string is here
NSArray *array = [NSArray arrayWithObjects:myString,nil];
Note that NSArray will be immutable - that is, you can't add or remove objects to it after you've made it. If you want the array to be mutable, you'll have to create an NSMutableArray. To use an NSMutableArray in this fashion, you can do the following:
NSString *myString; //Assuming your string is here
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:myString];
NSArray is immutable, so you cannot add values to it. You should use NSMutableArray in order to do that with the addObject: method.
NSMutableString *str = ...
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:str];
// You must use NSMutableArray to add Object to array
NSMutableArray *tableCellNames;
// arrayWithCapacity is a required parameter to define limit of your object.
tableCellNames = [NSMutableArray arrayWithCapacity:total_rows];
[tableCellNames addObject:title];
NSLog(#"Array table cell %#",tableCellNames);
//Thanks VKJ
An elegant solution would be this:
NSMutableString *str; //your string here
NSArray *newArray = #[str];
Using the new notation, it's a piece of cake.
Related
I want to store data at run time, I can have a linked list and add at runtime, however as I am new to IOS and objective C, do we have any default list in which we can add our datas, (Datas are two stings and an integer).
Cocoa provides NSArray and NSMutableArray, a pair of ordered containers similar to Java's ArrayList and C#'s List. You can add values to NSMutableArray, and it will grow as you add more elements; NSArray is read-only.
You can use .plist files to store your data.
Read more 'Loading data from .plist files', 'How to use plist in iphone?' or google it like 'load data from .plist'. However you can create NSArray or something like this at run time. If you want dive deeper you must read ObjC Collections Programming Topics
Make a class for your data with default properties and make sure it inherits NSObject then use NSMUtableArray to add/remove elements to the list.
// in the .h file of your object
#interface MyObject : NSObject {
NSString* strAttribute1;
// add more attributes as you want
}
#property (nonatomic, retain) NSString* strAttribute1;
#end
// then in the .m file
// do not forget the #import ""
#implement MyObject
#synthesize strAttribute1;
// override the dealloc to release the retained objects
#end
then in your code where you want to make a list of this object
NSMutableArray* myArray = [[NSMutableArray alloc] init];
// add elements and iterate through them
// do not forgot to free the memory if you are not using ARC
[myArray release];
You can use NSArray or NSMutableArray or NSDictionary or NSMutableDictionary depending on your needs.
NSArray:
NSArray *myArray;
NSDate *aDate = [NSDate distantFuture];
NSValue *aValue = [NSNumber numberWithInt:5];
NSString *aString = #"a string";
myArray = [NSArray arrayWithObjects:aDate, aValue, aString, nil];
NSMutableArray:
NSMutableArray *myArray = [[NSMutableArray alloc] init];
NSDate *aDate = [NSDate distantFuture];
NSValue *aValue = [NSNumber numberWithInt:5];
NSString *aString = #"a string";
[myArray addObject:aDate];
[myArray addObject:aValue];
[myArray addObject:aString];
NSDictionary:
NSDictionary * myDict = [NSDictionary dictionaryWithObjects:aDate, aValue, aString forKeys:firstDate, firstValue, firstString];
NSMutableDictionary:
NSString *aString = #"a string";
NSDate *aDate = [NSDate distantFuture];
NSValue *aValue = [NSNumber numberWithInt:5];
myDict = [[NSMutableDictionary alloc] init];
[myDict setObject:aString forKey:firstString];
[myDict setObject:aDate forKey:firstDate];
[myDict setObject:aValue forKey:firstValue];
My application always popping 'mutating method sent to immutable object'
My dictionary and the array already declare to be a mutable one but seems it doesn't help
I have tried the replaceObjectAtIndex function it also didn't work.
NSString* plistpath = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"plist"];
NSMutableDictionary* dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:plistpath];
NSMutableArray* Array1 = [dictionary objectForKey:#"Array1"];
[Array1 removeObjectAtIndex:num];
[Array1 insertObject:#"1" atIndex:num];
return Array1;
thanks
Try:
NSArray *array = [dictionary objectForKey:#"Array1"]; // Immutable
NSMutableArray * Array1 = [NSMutableArray arrayWithArray:array]; // Mutable
// ...
Or as Julien noted:
NSArray *array = [dictionary objectForKey:#"Array1"]; // Immutable
NSMutableArray * Array1 = [array mutableCopy]; // Mutable
If the right hand side does not return an NSMutableArray (NSArray instead) you try to mutate an in mutable object.
NSMutableArray* Array1 = [dictionary objectForKey:#"Array1"]; // NSArray
I have a NSMutableArray and I need to sort its elements into separate C char.
How to accomplish that? Or is it better to put the elements into NSStrings?
I've tried this, and it crashes when I try to log the result:
NSMutableArray *array = [[NSMutableArray alloc] init];
... do something to fill the array ...
NSString *string;
string = [array objectAtIndex:0];
NSLog(#"String: %#", string);
*I really prefer putting the elements of the array into C char, because I already have some woking code using char instead of NSStrin*g.
Thanks!
Dont see any specific reason to convert NSString to C chars. To sort an array full of NSStrings try this method -
NSMutableArray *array = [[NSMutableArray alloc] init];
sortedArray = [array sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
NSString *string = [sortedArray objectAtIndex:0];
NSLog(#"String: %#", string);
I created a mutable array and I have two NSString variables. Now I want to add these two NSStrings to my array. How is this possible? Thanks.
Use the addObject function of you NSMutableArray.
eg.
[myNSMutableArray addObject:myString1];
[myNSMutableArray addObject:myString2];
Jhaliya's answer is correct. +1 vote.
I added a immutable version so you can see the difference. If you dont want to remove or add more objects (NSStrings) to your container, I would recommend using an Immutable version.
Mutable version:
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
NSString *string_one = #"One"];
[mutableArray addObject:string_one];
//Or
[mutableArray addObject:#"Two"];
NSLog(#"%#", mutableArray);
Immutable version
NSArray *immutableArray = [NSArray arrayWithObjects:#"One", #"Two", nil];
NSLog(#"%#", immutableArray);
You can add at NSMutableArray allocation.
Like :
NSMutableArray *test = [NSMutableArray arrayWithObjects:#"test1",#"test2",nil];
I'm able to put the contents of an NSSet into an NSMutableArray like this:
NSMutableArray *array = [set allObjects];
The compiler complains though because [set allObjects] returns an NSArray not an NSMutableArray. How should this be fixed?
Since -allObjects returns an array, you can create a mutable version with:
NSMutableArray *array = [NSMutableArray arrayWithArray:[set allObjects]];
Or, alternatively, if you want to handle the object ownership:
NSMutableArray *array = [[set allObjects] mutableCopy];
I resolved crashing by using NSMutableArray's method 'addObjectsFromArray' to assign all NSSet objects to NSMutableArray like:
[mutableArray addObjectsFromArray:[cg_Schedule.schedule_Days allObjects]];
Hope this will helps you.
For an ordered set use:
NSArray *myArray = [[myOrderedSet array] mutableCopy];