objective-c 如何在目标 C 中创建一个空数组,并一一为其赋值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2335151/
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
How can I create an empty array in objective C, and assign value into it one by one?
提问by Tattat
In Java, I can do that :
在 Java 中,我可以这样做:
int[] abc = new int[10];
for(int i=0; i<abc.length; i++){
abc[i] = i;
}
How can I implement similar thing in Objective C?
如何在 Objective C 中实现类似的东西?
I see some answer using NSMutableArray, wt's different between this and NSArray?
我看到一些使用 NSMutableArray 的答案,这和 NSArray 有什么不同?
采纳答案by Curtis Inderwiesche
Try something like this.
尝试这样的事情。
#import <foundation/foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *myArray = [NSMutableArray array];
[myArray addObject:@"first string"];
[myArray addObject:@"second string"];
[myArray addObject:@"third string"];
int count = [myArray count];
NSLog(@"There are %d elements in my array", count);
[pool release];
return 0;
}
The loop would look like:
循环看起来像:
NSString *element;
int i;
int count;
for (i = 0, count = [myArray count]; i < count; i = i + 1)
{
element = [myArray objectAtIndex:i];
NSLog(@"The element at index %d in the array is: %@", i, element);
}
See CocoLab
回答by kennytm
NSMutableArray* abc = [NSMutableArray array];
for (int i = 0; i < 10; ++ i)
[abc addObject:[NSNumber numberWithInt:i]];
return abc;
Here,
这里,
- NSMutableArray is a "mutable" array type. There is also a constant array super-type NSArray. Unlike C++, ObjC's array is not typed. You can store any ObjC objects in an NSArray.
-addObject:appends an ObjC at the end of the array.- Since NSArrays can only store ObjC, you have to "box" the integer into an NSNumber first.
- NSMutableArray 是一种“可变”数组类型。还有一个常量数组超类型 NSArray。与 C++ 不同,ObjC 的数组没有类型化。您可以将任何 ObjC 对象存储在 NSArray 中。
-addObject:在数组的末尾附加一个 ObjC。- 由于 NSArrays 只能存储 ObjC,因此您必须先将整数“装箱”到 NSNumber 中。
This is actually more similar to the C++ code
这实际上更类似于 C++ 代码
std::vector<int> abc;
for (int i = 0; i < 10; ++ i)
abc.push_back(i);
return abc;
回答by Sixten Otto
I see some answer using NSMutableArray, wt's different between this and NSArray?
我看到一些使用 NSMutableArray 的答案,这和 NSArray 有什么不同?
NSArrayis immutable: it cannot be changed. NSMutableArrayis a subclass that is mutable, and adds methods to the array interface allowing manipulation of its contents. This is a pattern seen in many places throughout Cocoa: NSString, NSSet, and NSDictionaryare particularly frequent examples that are all immutable objects with mutable subclasses.
NSArray是不可变的:它不能改变。NSMutableArray是一个可变的子类,并向数组接口添加方法,允许对其内容进行操作。这是 Cocoa 中许多地方都可以看到的模式:NSString、NSSet和NSDictionary是特别常见的例子,它们都是具有可变子类的不可变对象。
The biggest reason for this is efficiency. You can make assumptions about memory management and thread safety with immutable objects that you cannot make with objects that might change out from under you.
最大的原因是效率。您可以对不可变对象做出关于内存管理和线程安全的假设,而对于可能从您身下改变的对象则无法做出这些假设。

