如何向数组添加整数?

时间:2021-01-04 15:59:07

This must be quite basic, but I was wondering how to add an integer to an array?

这一定是非常基础的,但我想知道如何在数组中添加一个整数?

I know I can add strings like this:

我知道我可以添加这样的字符串:

NSMutableArray *trArray = [[NSMutableArray alloc] init];
[trArray addObject:@"0"];
[trArray addObject:@"1"];
[trArray addObject:@"2"];
[trArray addObject:@"3"];

But I guess I can't simply add integers like so:

但我想我不能简单地添加这样的整数:

NSMutableArray *trArray = [[NSMutableArray alloc] init];
[trArray addObject:0];
[trArray addObject:1];
[trArray addObject:2];
[trArray addObject:3];

At least the compiler isn't happy with that and tells me that I'm doing a cast without having told it so.

至少编译器对此并不满意,并且告诉我,我正在进行演员而没有告诉它。

Any explanations would be very much appreciated.

任何解释都将非常感激。

3 个解决方案

#1


27  

Yes that's right. The compiler won't accept your code like this. The difference is the following:

恩,那就对了。编译器不会接受这样的代码。区别在于:

If you write @"a String", it's the same as if you created a string and autoreleased it. So you create an object by using @"a String".

如果你写@“一个字符串”,它就像你创建一个字符串并自动释放它一样。因此,您使用@“a String”创建对象。

But an array can only store objects (more precise: pointers to object). So you have to create objects which store your integer.

但是数组只能存储对象(更精确:指向对象的指针)。所以你必须创建存储整数的对象。

NSNumber *anumber = [NSNumber numberWithInteger:4];
[yourArray addObject:anumber];

To retrive the integer again, do it like this

要再次检索整数,请执行此操作

NSNumber anumber = [yourArray objectAtIndex:6];
int yourInteger = [anumber intValue];

I hope my answer helps you to understand why it doesn't work. You can't cast an integer to a pointer. And that is the warning you get from Xcode.

我希望我的回答可以帮助你理解为什么它不起作用。您不能将整数强制转换为指针。这是你从Xcode得到的警告。

EDIT:

编辑:

It is now also possible to write the following

现在也可以写下面的内容

[yourArray addObject:@3];

which is a shortcut to create a NSNumber. The same syntax is available for arrays

这是创建NSNumber的快捷方式。数组的语法相同

@[@1, @2];

will give you an NSArray containing 2 NSNumber objects with the values 1 and 2.

将为您提供一个NSArray,其中包含2个值为1和2的NSNumber对象。

#2


7  

You have to use NSNumbers I think, try adding these objects to your array: [NSNumber numberWithInteger:myInt];

您必须使用NSNumbers,尝试将这些对象添加到您的数组中:[NSNumber numberWithInteger:myInt];

NSMutableArray *trArray = [[NSMutableArray alloc] init];     
NSNumber *yourNumber = [[NSNumber alloc] numberWithInt:5];

[trArray addObject: yourNumber];

#3


4  

You can also use this if you want to use strings:

如果要使用字符串,也可以使用它:

NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSString stringWithFormat:@"%d",1]];
[[array objectAtIndex:0] intValue];

#1


27  

Yes that's right. The compiler won't accept your code like this. The difference is the following:

恩,那就对了。编译器不会接受这样的代码。区别在于:

If you write @"a String", it's the same as if you created a string and autoreleased it. So you create an object by using @"a String".

如果你写@“一个字符串”,它就像你创建一个字符串并自动释放它一样。因此,您使用@“a String”创建对象。

But an array can only store objects (more precise: pointers to object). So you have to create objects which store your integer.

但是数组只能存储对象(更精确:指向对象的指针)。所以你必须创建存储整数的对象。

NSNumber *anumber = [NSNumber numberWithInteger:4];
[yourArray addObject:anumber];

To retrive the integer again, do it like this

要再次检索整数,请执行此操作

NSNumber anumber = [yourArray objectAtIndex:6];
int yourInteger = [anumber intValue];

I hope my answer helps you to understand why it doesn't work. You can't cast an integer to a pointer. And that is the warning you get from Xcode.

我希望我的回答可以帮助你理解为什么它不起作用。您不能将整数强制转换为指针。这是你从Xcode得到的警告。

EDIT:

编辑:

It is now also possible to write the following

现在也可以写下面的内容

[yourArray addObject:@3];

which is a shortcut to create a NSNumber. The same syntax is available for arrays

这是创建NSNumber的快捷方式。数组的语法相同

@[@1, @2];

will give you an NSArray containing 2 NSNumber objects with the values 1 and 2.

将为您提供一个NSArray,其中包含2个值为1和2的NSNumber对象。

#2


7  

You have to use NSNumbers I think, try adding these objects to your array: [NSNumber numberWithInteger:myInt];

您必须使用NSNumbers,尝试将这些对象添加到您的数组中:[NSNumber numberWithInteger:myInt];

NSMutableArray *trArray = [[NSMutableArray alloc] init];     
NSNumber *yourNumber = [[NSNumber alloc] numberWithInt:5];

[trArray addObject: yourNumber];

#3


4  

You can also use this if you want to use strings:

如果要使用字符串,也可以使用它:

NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSString stringWithFormat:@"%d",1]];
[[array objectAtIndex:0] intValue];