How to add data from NSMutableString to NSArray?

Is it possible to add a value from NSMutableStringin NSArray? What is a fragment?

+3
source share
5 answers

Actually, Mike is wrong. If you want to instantiate an NSArray using a single NSMutableString, you can do the following:

NSMutableString *myString; //Assuming your string is here
NSArray *array = [NSArray arrayWithObject:myString];

In NSArrayno arrayWithElements(see. An NSArray documentation )

+4
source

If you want to create an instance NSArrayusing a single object NSMutableString, you can do the following:

NSString *myString; //Assuming your string is here
NSArray *array = [NSArray arrayWithObjects:myString,nil];

, NSArray - . , , NSMutableArray. NSMutableArray , :

NSString *myString; //Assuming your string is here
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:myString];
+2

NSArray is immutable, so you cannot add values ​​to it. You must use NSMutableArray to do this using the method addObject:.

NSMutableString *str = ...
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:str];
+1
source
// 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
0
source

An elegant solution would be the following:

NSMutableString *str; //your string here
NSArray *newArray = @[str];

Using the new notation, this is a piece of cake.

0
source

Source: https://habr.com/ru/post/1727735/


All Articles