I'm struggling to create an array of ints in objective c.
我在objective c中创建一个ints数组。
the way I'd do this is javascript would be:
我这样做的方式是javascript:
arr = new Array ( 1, 2, 3, 4, 3, 2, 2 );
I need to use an NSMutableArray
, as the values need to be updated (it's a tile map for a game).
我需要使用NSMutableArray,因为值需要更新(它是一个游戏的平面图)。
How can I do this?
我该怎么做呢?
4 个解决方案
#1
4
Does the array need to change length?
数组是否需要改变长度?
If not, why not use a normal array of ints:
如果没有,为什么不使用一个普通的ints数组:
int arr[] = { 1, 2, 3, 4, 3, 2, 2};
Note that an NSArray (or subclass thereof) doesn't hold int
types natively, you have to create an NSNumber object.
注意,NSArray(或其子类)本身不包含int类型,您必须创建一个NSNumber对象。
If you really need an Objective-C style array, then use this:
如果您确实需要一个Objective-C样式的数组,那么使用以下方法:
int vals[] = { 1, 2, 3, 4, 3, 2, 2}; // you still need this
int n = sizeof(vals) / sizeof(vals[0]);
[NSMutableArray* array = [[NSMutableArray alloc] initWithCapacity:n];
for (int i = 0; i < n; ++i) {
[array addObject: [NSNumber numberWithInt:vals[i]]];
}
#2
2
For an NSMutableArray (though a C-style array may be preferable, as suggested by Alnitak), you need to do something like this:
对于NSMutableArray(尽管c样式的数组可能更好,正如Alnitak所建议的),您需要做以下事情:
NSMutableArray *array = [NSMutableArray arrayWithObjects: [NSNumber numberWithInt: 1], [NSNumber numberWithInt:2], nil];
(You can put as many as you want into that constructor, just separate them with commas and end with a nil
.)
(您可以在构造函数中输入任意数量的内容,只需用逗号分隔它们,以nil结尾)。
You could also make an empty array ([NSMutableArray array]
) and then fill it with your ints using [array addObject:[NSNumber numberWithInt:1]]
.
您还可以创建一个空数组([NSMutableArray数组]),然后使用[array addObject:[NSNumber numberWithInt:1]]填充它。
If you need a 2D version, take a look here.
如果你需要2D版本,请看这里。
#3
0
NSMutableArray can only contain Cocoa objects that inherit from NSObject (ie. Apple's classes and yours).
NSMutableArray只能包含继承自NSObject(即NSObject)的Cocoa对象。苹果的课程和你的)。
Basically for NSMutableArray you just init it like this:
对于NSMutableArray,你可以这样初始化:
myArray = [[NSMutableArray alloc] init];
and then add/delete objects as needed.
然后根据需要添加/删除对象。
Good luck!
好运!
PS: Hank's class reference is a good starting place.
注:汉克的班级推荐是一个很好的起点。
#4
0
In addition to Alnitak's answer, I would add that this question also suggests some confusion that I certainly felt while learning to code iPhone programming. Here are a few tips in general about programming languages on the iPhone
除了Alnitak的答案之外,我还要补充一点,这个问题也让我在学习编写iPhone编程时感到困惑。下面是一些关于iPhone编程语言的一般提示
- Not all the code you write is objective-c.
- 并不是所有的代码都是objective-c。
- You can (and often should) make use of other languages in programming, most notably C.
- 您可以(而且应该)在编程中使用其他语言,尤其是C语言。
- Alnitak's "normal array" is a C array. It is great for storing information with primitive data types.
- Alnitak的“普通数组”是一个C数组。它非常适合用原始数据类型存储信息。
- If you want to store objects in your array, you use an NSArray. This is not a feature available in C. We know this immediately because it starts with "NS" (incidentally, this stands for Next Step).
- 如果希望在数组中存储对象,可以使用NSArray。这不是c的特性,我们马上就知道了,因为它是从“NS”开始的(顺便说一下,这表示下一步)。
#1
4
Does the array need to change length?
数组是否需要改变长度?
If not, why not use a normal array of ints:
如果没有,为什么不使用一个普通的ints数组:
int arr[] = { 1, 2, 3, 4, 3, 2, 2};
Note that an NSArray (or subclass thereof) doesn't hold int
types natively, you have to create an NSNumber object.
注意,NSArray(或其子类)本身不包含int类型,您必须创建一个NSNumber对象。
If you really need an Objective-C style array, then use this:
如果您确实需要一个Objective-C样式的数组,那么使用以下方法:
int vals[] = { 1, 2, 3, 4, 3, 2, 2}; // you still need this
int n = sizeof(vals) / sizeof(vals[0]);
[NSMutableArray* array = [[NSMutableArray alloc] initWithCapacity:n];
for (int i = 0; i < n; ++i) {
[array addObject: [NSNumber numberWithInt:vals[i]]];
}
#2
2
For an NSMutableArray (though a C-style array may be preferable, as suggested by Alnitak), you need to do something like this:
对于NSMutableArray(尽管c样式的数组可能更好,正如Alnitak所建议的),您需要做以下事情:
NSMutableArray *array = [NSMutableArray arrayWithObjects: [NSNumber numberWithInt: 1], [NSNumber numberWithInt:2], nil];
(You can put as many as you want into that constructor, just separate them with commas and end with a nil
.)
(您可以在构造函数中输入任意数量的内容,只需用逗号分隔它们,以nil结尾)。
You could also make an empty array ([NSMutableArray array]
) and then fill it with your ints using [array addObject:[NSNumber numberWithInt:1]]
.
您还可以创建一个空数组([NSMutableArray数组]),然后使用[array addObject:[NSNumber numberWithInt:1]]填充它。
If you need a 2D version, take a look here.
如果你需要2D版本,请看这里。
#3
0
NSMutableArray can only contain Cocoa objects that inherit from NSObject (ie. Apple's classes and yours).
NSMutableArray只能包含继承自NSObject(即NSObject)的Cocoa对象。苹果的课程和你的)。
Basically for NSMutableArray you just init it like this:
对于NSMutableArray,你可以这样初始化:
myArray = [[NSMutableArray alloc] init];
and then add/delete objects as needed.
然后根据需要添加/删除对象。
Good luck!
好运!
PS: Hank's class reference is a good starting place.
注:汉克的班级推荐是一个很好的起点。
#4
0
In addition to Alnitak's answer, I would add that this question also suggests some confusion that I certainly felt while learning to code iPhone programming. Here are a few tips in general about programming languages on the iPhone
除了Alnitak的答案之外,我还要补充一点,这个问题也让我在学习编写iPhone编程时感到困惑。下面是一些关于iPhone编程语言的一般提示
- Not all the code you write is objective-c.
- 并不是所有的代码都是objective-c。
- You can (and often should) make use of other languages in programming, most notably C.
- 您可以(而且应该)在编程中使用其他语言,尤其是C语言。
- Alnitak's "normal array" is a C array. It is great for storing information with primitive data types.
- Alnitak的“普通数组”是一个C数组。它非常适合用原始数据类型存储信息。
- If you want to store objects in your array, you use an NSArray. This is not a feature available in C. We know this immediately because it starts with "NS" (incidentally, this stands for Next Step).
- 如果希望在数组中存储对象,可以使用NSArray。这不是c的特性,我们马上就知道了,因为它是从“NS”开始的(顺便说一下,这表示下一步)。