如何向NSMutableArray中添加字符串对象

时间:2023-01-27 23:49:02

I have an NSMutableArray named randomSelection:

我有一个叫随机选择的NSMutableArray:

NSMutableArray *randomSelection;

I am then trying to add strings to this array if certain criteria are met:

然后,如果满足某些条件,我将尝试向该数组添加字符串:

[randomSelection addObject:@"string1"];

I am then trying to output the string, to determine if it has added it:

然后我尝试输出字符串,以确定它是否添加了:

NSString *test = [randomSelection objectAtIndex:0];
NSLog(test);

However nothing is being output to the error log and I can't figure out why.

然而,没有任何东西输出到错误日志,我也不知道为什么。

Any help/hints appreciated.

任何帮助/暗示感激。

6 个解决方案

#1


59  

I think you are missing to allocate the memory for array. So try this

我想你没有分配数组的内存。那么试试这个

NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
[randomSelection addObject:@"string1"];
NSString *test = [randomSelection objectAtIndex:0];
NSLog(test);

#2


5  

NSMutableArray *randomSelection =  [[NSMutableArray alloc]init];
[randomSelection addObject:@"string1"];

You need to alloc it first.

你需要先分配它。

#3


4  

First allocate the array using following statement & then objects in it.

首先使用以下语句分配数组,然后使用其中的对象。

NSMutableArray *randomSelection =  [[NSMutableArray alloc] init];
[randomSelection addObject:[NSString stringWithFormat:@"String1"]];
[randomSelection addObject:[NSString stringWithFormat:@"String2"]];
NSLog(@"Array - %@", randomSelection);

This will definitely solves your problem.

这肯定能解决你的问题。

#4


3  

Just allocate your NSMutableArray. You'll get solved your problem.

只是你NSMutableArray分配。你会解决你的问题的。

#5


2  

Try this:

试试这个:

NSMutableArray *randomSelection = [[NSMutableArray alloc]init]; 
[randomSelection addObject:@"string1"];

#6


0  

Swift :

迅速:

var randomSelection: [AnyObject] = [AnyObject]()
randomSelection.append("string1")
let test: String = randomSelection[0] as! String
print(test)

OR

let array : NSMutableArray = []
array.addObject("test String")
print(array)

#1


59  

I think you are missing to allocate the memory for array. So try this

我想你没有分配数组的内存。那么试试这个

NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
[randomSelection addObject:@"string1"];
NSString *test = [randomSelection objectAtIndex:0];
NSLog(test);

#2


5  

NSMutableArray *randomSelection =  [[NSMutableArray alloc]init];
[randomSelection addObject:@"string1"];

You need to alloc it first.

你需要先分配它。

#3


4  

First allocate the array using following statement & then objects in it.

首先使用以下语句分配数组,然后使用其中的对象。

NSMutableArray *randomSelection =  [[NSMutableArray alloc] init];
[randomSelection addObject:[NSString stringWithFormat:@"String1"]];
[randomSelection addObject:[NSString stringWithFormat:@"String2"]];
NSLog(@"Array - %@", randomSelection);

This will definitely solves your problem.

这肯定能解决你的问题。

#4


3  

Just allocate your NSMutableArray. You'll get solved your problem.

只是你NSMutableArray分配。你会解决你的问题的。

#5


2  

Try this:

试试这个:

NSMutableArray *randomSelection = [[NSMutableArray alloc]init]; 
[randomSelection addObject:@"string1"];

#6


0  

Swift :

迅速:

var randomSelection: [AnyObject] = [AnyObject]()
randomSelection.append("string1")
let test: String = randomSelection[0] as! String
print(test)

OR

let array : NSMutableArray = []
array.addObject("test String")
print(array)