Reputation: 1806
is it an possible to add a value from an NSMutableString
into an NSArray
? Whats the snippet?
Upvotes: 3
Views: 4071
Reputation: 5780
An elegant solution would be this:
NSMutableString *str; //your string here
NSArray *newArray = @[str];
Using the new notation, it's a piece of cake.
Upvotes: 0
Reputation: 7862
// 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
Upvotes: 0
Reputation: 24964
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];
Upvotes: 2
Reputation: 2473
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)
Upvotes: 4
Reputation: 27889
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];
Upvotes: 1