我将在此可变数组中放入各种内容,但首先,我试图通过放入字符串,然后拉出字符串来确保它可以工作。这是我的代码

str1=@"1";
str2=@"2";
str3=@"3";
NSMutableArray *testArray;
[testArray addObject:str1];
[testArray addObject:str2];
[testArray addObject:str3];

retrieve =[testArray objectAtIndex:1];
NSLog(@"the test number is %@",retrieve);

问题是从数组接收到字符串后,我的string:retrieve等于“null”。我不确定自己在做什么错,我看过Apple的文档,但很难理解。我知道我必须与数组进行错误的交互,但是我不确定该如何进行。帮助将不胜感激。
-谢谢!

最佳答案

您没有初始化testArray:

NSMutableArray *testArray = [NSMutableArray array];

您可以使用新语法填充数组。如果只需要可变性即可添加这三个项目,则可以改用非可变数组,如下所示:
NSArray *testArray = @[ @"1", @"2", @"3"];

如果您确实需要可变性,请致电mutableCopy:
NSMutableArray *testArray = [@[ @"1", @"2", @"3"] mutableCopy];

关于ios - 无法从NSMutableArray正确检索对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27047120/

10-12 05:42