如何将字符串对象添加到NSMutableArray [英] How to add string objects to NSMutableArray
本文介绍了如何将字符串对象添加到NSMutableArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为randomSelection的NSMutableArray:
I have an NSMutableArray named randomSelection:
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.
感谢任何帮助/提示.
推荐答案
我认为您缺少为数组分配内存的方法.所以试试这个
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);
这篇关于如何将字符串对象添加到NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文