本文介绍了在iOS中制作整数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要创建整数数组,可以使用NSInteger吗?您必须使用NSNumber吗?如果是这样,那为什么呢?

If you want to make an array of integers, can you use NSInteger? Do you have to use NSNumber? If so, then why?

推荐答案

您可以使用普通的旧C数组:

You can use a plain old C array:

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]);

或者,您可以使用NSArrayNSMutableArray,但是在这里,您需要将每个整数包装在NSNumber实例中(因为NSArray对象被设计为容纳类实例).

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).

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]);

这篇关于在iOS中制作整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 05:54