xcode 将对象添加到 NSArray
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10558792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
addObject to NSArray
提问by dlcj
I am a newbe and have been working all day readying/watching/searching for a solution to this. The following code works, but now how do I add an integer to the array?
我是新手,一整天都在准备/观看/寻找解决方案。以下代码有效,但现在如何将整数添加到数组中?
//this works
NSArray *array = [NSArray arrayWithObjects:[NSNumber numberWithInt:10],
[NSNumber numberWithFloat:25.96], @"Hello World", nil];
for (id obj in array)
NSLog(@"%@", obj);
The following returns error: Use of undeclared identifier 'obj'
以下返回错误:使用未声明的标识符“obj”
NSArray *array = [NSArray arrayWithObjects:[NSNumber numberWithInt:10],
[NSNumber numberWithFloat:25.96], @"Hello World", nil];
for (id obj in array)
[array addObject:7];
NSLog(@"%@", obj);
回答by Elyes Jlassi
you must use a NSMutabbleArray not an NSArray to add objects dynamically and don't forget [] and arrayWithObjects is only for nsarray. try this:
您必须使用 NSMutableArray 而不是 NSArray 来动态添加对象,并且不要忘记 [] 和 arrayWithObjects 仅适用于 nsarray。尝试这个:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSNumber numberWithInt:10]];
[array addObject:[NSNumber numberWithFloat:25.96]];
[array addObject:@"Hello World"];
[array addObject:[NSNumber numberWithInt:7]];
for (id obj in array) {
NSLog(@"%@", obj);
}
Just Copy paste this code and it will initiate the array with object you want and then show them.
只需复制粘贴此代码,它将使用您想要的对象启动数组,然后显示它们。
回答by Conrad Shultz
There are several issues here:
这里有几个问题:
- You are attempting to mutate (change) an immutable array. You would need an
NSMutableArray
instead ofNSArray
to do what you want. (Incidentally, your actualNSArray
creation code is fine.) - Your
for()
loop is blowing up mainly because you don't have braces to delimit your code. Absent braces, the compiler is going to use onlythe first line following thefor
as the loop contents. Hence yourNSLog()
is outside the loop, andobj
out of scope. I don't know why you don't have brackets around youraddObject:
line, as in[array addObject:...]
. Did that even compile? Is that a copy and paste error?- Finally, you can't add a naked integer to a Cocoa collection such as
NSArray
. You would need to follow what you properly did earlier in your code:[array addObject:[NSNumber numberWithInt:7]]
.
- 您正在尝试改变(更改)不可变数组。你需要一个
NSMutableArray
而不是NSArray
做你想做的事。(顺便说一句,您的实际NSArray
创建代码很好。) - 你的
for()
循环爆炸主要是因为你没有大括号来分隔你的代码。如果没有大括号,编译器将只使用 之后的第一行for
作为循环内容。因此,您NSLog()
在循环之外,obj
超出了范围。 我不知道为什么你的addObject:
行周围没有括号,如[array addObject:...]
. 这甚至编译了吗?那是复制粘贴错误吗?- 最后,您不能将裸整数添加到 Cocoa 集合中,例如
NSArray
. 您需要遵循您之前在代码中正确执行的操作:[array addObject:[NSNumber numberWithInt:7]]
.
Given that you were using indentation as syntax, I'm guessing maybe you have more of a Python (or similar) background? If so, you are going to have to quickly adjust some of your modes of thinking to program effectively in C or Objective-C. I urgeyou to get a beginners book on Cocoa programming and start at the beginning or you are going to get very frustrated very quickly.
鉴于您使用缩进作为语法,我猜您可能有更多的 Python(或类似)背景?如果是这样,您将不得不快速调整一些思维模式,以便在 C 或 Objective-C 中进行有效编程。我强烈建议你买一本关于 Cocoa 编程的初学者书籍并从头开始,否则你很快就会感到非常沮丧。
Edit: I notice that after I posted you added brackets back in, so I struck #3 above. This leads to another important note: make sure that if you ask a question that you copy and paste the exactcode that failed. Never re-type!
编辑:我注意到在我发布后你又加了括号,所以我在上面打了#3。这引出了另一个重要注意事项:确保在您提出问题时复制并粘贴失败的确切代码。永远不要重新输入!
回答by Ben
It's because your using indentation in your for loop which only considers the following line. Try this instead
这是因为您在 for 循环中使用缩进只考虑以下行。试试这个
? ? for (id obj in array) {
? ? ? ? [array addObject:[NSNumber numberWithInt:7]];
? ? ? ? NSLog(@"%@", obj);
}
Note
笔记
I think you need to use a NSMutableArray if you want to add objects after initialising it. Also it's not a good idea to modify a collection your looping through (in this case the loop will never end as you keep adding an object into an array your looping through). You can make a mutable version of the array by using this line.
如果您想在初始化后添加对象,我认为您需要使用 NSMutableArray。此外,修改循环中的集合也不是一个好主意(在这种情况下,循环将永远不会结束,因为您不断将对象添加到循环中的数组中)。您可以使用此行创建数组的可变版本。
NSMutableArray * mutArray = [NSMutableArray arrayWithArray:array];