在 iOS 中创建一个整数数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3340153/
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
Making an array of integers in iOS
提问by neuromancer
If you want to make an array of integers, can you use NSInteger? Do you have to use NSNumber? If so, then why?
如果你想制作一个整数数组,你可以使用 NSInteger 吗?你必须使用 NSNumber 吗?如果是这样,那为什么?
回答by dreamlax
You can use a plain old C array:
您可以使用普通的旧 C 数组:
NSInteger myIntegers[40];
for (NSInteger i = 0; i < 40; i++)
myIntegers[i] = i;
// to get one of them
NSLog (@"The 4th integer is: %d", myIntegers[3]);
Or, you can use an NSArray
or NSMutableArray
, but here you will need to wrap up each integer inside an NSNumber
instance (because NSArray
objects are designed to hold class instances).
或者,您可以使用NSArray
or NSMutableArray
,但在这里您需要将每个整数包装在一个NSNumber
实例中(因为NSArray
对象旨在保存类实例)。
NSMutableArray *myIntegers = [NSMutableArray array];
for (NSInteger i = 0; i < 40; i++)
[myIntegers addObject:[NSNumber numberWithInteger:i]];
// to get one of them
NSLog (@"The 4th integer is: %@", [myIntegers objectAtIndex:3]);
// or
NSLog (@"The 4th integer is: %d", [[myIntegers objectAtIndex:3] integerValue]);
回答by Frédéric Adda
C array:
C数组:
NSInteger array[6] = {1, 2, 3, 4, 5, 6};
Objective-C Array:
Objective-C 数组:
NSArray *array = @[@1, @2, @3, @4, @5, @6];
// numeric values must in that case be wrapped into NSNumbers
Swift Array:
快速数组:
var array = [1, 2, 3, 4, 5, 6]
This is correct too:
这也是正确的:
var array = Array(1...10)
NB: arrays are strongly typed in Swift; in that case, the compiler infers from the content that the array is an array of integers. You could use this explicit-type syntax, too:
注意:数组在 Swift 中是强类型的;在这种情况下,编译器从内容推断该数组是一个整数数组。您也可以使用这种显式类型的语法:
var array: [Int] = [1, 2, 3, 4, 5, 6]
If you wanted an array of Doubles, you would use :
如果你想要一个双打数组,你可以使用:
var array = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] // implicit type-inference
or:
或者:
var array: [Double] = [1, 2, 3, 4, 5, 6] // explicit type
回答by Dennis Munsie
If you want to use a NSArray, you need an Objective-C class to put in it - hence the NSNumber requirement.
如果你想使用 NSArray,你需要一个 Objective-C 类来放置它——因此需要 NSNumber。
That said, Obj-C is still C, so you can use regular C arrays and hold regular ints instead of NSNumbers if you need to.
也就是说,Obj-C 仍然是 C,所以如果需要,您可以使用常规 C 数组并保存常规整数而不是 NSNumber。
回答by coyotte508
You can use CFArrayinstead of NSArray. Hereis an article explaining how.
您可以使用CFArray代替 NSArray。这是一篇解释如何操作的文章。
CFMutableArrayRef ar = CFArrayCreateMutable(NULL, 0, NULL);
for (NSUInteger i = 0; i < 1000; i++)
{
CFArrayAppendValue(ar, (void*)i);
}
CFRelease(ar); /* Releasing the array */
The same applies for the CoreFoundation version of the other containers too.
这同样适用于其他容器的 CoreFoundation 版本。
回答by MrTJ
I created a simple Objective C wrapper around the good old C array to be used more conveniently: https://gist.github.com/4705733
我在旧的 C 数组周围创建了一个简单的 Objective C 包装器,以便更方便地使用:https: //gist.github.com/4705733
回答by didier_v_
If the order of your integers is not required, and if there are only unique values
如果不需要整数的顺序,并且只有唯一值
you can also use NSIndexSet or NSMutableIndexSet You will be able to easily add and remove integers, or check if your array contains an integer with
您还可以使用 NSIndexSet 或 NSMutableIndexSet 您将能够轻松添加和删除整数,或检查您的数组是否包含整数
- (void)addIndex:(NSUInteger)index
- (void)removeIndex:(NSUInteger)index
- (BOOL)containsIndexes:(NSIndexSet *)indexSet
Check the documentationfor more info.
查看文档以获取更多信息。
回答by Neeku
I think it's a lot easier to use NSNumbers. This all you need to do:
我认为使用 NSNumbers 容易得多。这一切你需要做的:
NSNumber *myNum1 = [NSNumber numberWithInt:myNsIntValue1];
NSNumber *myNum2 = [NSNumber numberWithInt:myNsIntValue2];
.
.
.
NSArray *myArray = [NSArray arrayWithObjects: myNum1, myNum2, ..., nil];