我将在这个可变数组中放入各种东西,但首先我只是尝试通过放入字符串然后拉出字符串来确保它能正常工作。这是我的代码
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/