尝试填充NSMutableArray但是使用arc不允许将int隐式转换为id [英] Trying to populate NSMutableArray but get implicit conversion of int to id is disallowed with arc

查看:103
本文介绍了尝试填充NSMutableArray但是使用arc不允许将int隐式转换为id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想在 NSMutableArray 中添加一系列数字,但我收到错误:

I am simply trying to add a series of numbers to an NSMutableArray but I am getting the error:

不允许使用arc隐式转换int到id

resultArray = [[NSMutableArray alloc]init];

for (int i = 0; i <= 9; i++)
{
[resultArray addObject:i];
}

我做错了什么?

推荐答案

NSArray NSMutableArray 只能保存对象,不是原始数据类型,例如 int s。因此,如果要将它们添加到数组中,则必须将它们包装在 NSNumber 中。您可以执行以下操作:

An NSArray or NSMutableArray can only hold objects, not primitive data types such as ints. So if you want to add them to the array, you'll have to wrap them in an NSNumber. You can do the following:

[resultArray addObject:@(i)];

这相当于:

[resultArray addObject:[NSNumber numberWithInt:i]];

这篇关于尝试填充NSMutableArray但是使用arc不允许将int隐式转换为id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆