警告:转换为Boxing语法需要将'long'强制转换为'int'

时间:2022-09-01 10:18:32

Any idea how to get this warning to go away. The code runs fine, I just don't like warnings in my project. I have never come across this warning before so I am making a mountain out of a mole hill I think. Boxing syntax? Is that referring to the square brackets? This warning shows up when trying to modernize an old project in Objective-C using Xcode.

任何想法如何让这个警告消失。代码运行正常,我只是不喜欢我的项目中的警告。我之前从来没有遇到过这个警告,所以我想在一座鼹鼠山上建造一座山。拳击语法?这是指方括号吗?尝试使用Xcode在Objective-C中对旧项目进行现代化时会出现此警告。

for (int i = 0; i <= 6; i++) {

   [sequence addObject:[NSNumber numberWithInt:random()% 6]]; 

}

It throws an error stating:

它抛出一个错误说明:

Converting to boxing syntax requires casting 'long' to 'int'

转换为拳击语法需要将'long'转换为'int'

警告:转换为Boxing语法需要将'long'强制转换为'int'

1 个解决方案

#1


1  

"Boxing" refers to the new syntax for boxing C expressions, e.g.

“拳击”指拳击C表达式的新语法,例如

NSNumber *n = @(2*3+4)

instead of

代替

NSNumber *n = [NSNumber numberWithInt:(2*3+4)];

(see http://clang.llvm.org/docs/ObjectiveCLiterals.html for details).

(有关详细信息,请参阅http://clang.llvm.org/docs/ObjectiveCLiterals.html)。

In your case,

在你的情况下,

[NSNumber numberWithInt:random()% 6]

creates a number object containing an int, but

创建一个包含int的数字对象,但是

@(random()% 6)

would create a number object containing a long, because random() is declared as

会创建一个包含long的数字对象,因为random()被声明为

long random(void);

So to get exactly the same behavior as before the conversion, you would have to write

因此,为了获得与转换之前完全相同的行为,您必须编写

[NSNumber numberWithInt:(int)(random()% 6)]

which is then converted to

然后转换为

@((int)(random()% 6))

If you don't care which "flavor" of number object you get, then just convert that line manually to

如果你不关心你得到的数字对象的“味道”,那么只需手动将该行转换为

[sequence addObject:@(random()% 6)];

but Xcode cannot decide that for you.

但Xcode无法为您决定。

#1


1  

"Boxing" refers to the new syntax for boxing C expressions, e.g.

“拳击”指拳击C表达式的新语法,例如

NSNumber *n = @(2*3+4)

instead of

代替

NSNumber *n = [NSNumber numberWithInt:(2*3+4)];

(see http://clang.llvm.org/docs/ObjectiveCLiterals.html for details).

(有关详细信息,请参阅http://clang.llvm.org/docs/ObjectiveCLiterals.html)。

In your case,

在你的情况下,

[NSNumber numberWithInt:random()% 6]

creates a number object containing an int, but

创建一个包含int的数字对象,但是

@(random()% 6)

would create a number object containing a long, because random() is declared as

会创建一个包含long的数字对象,因为random()被声明为

long random(void);

So to get exactly the same behavior as before the conversion, you would have to write

因此,为了获得与转换之前完全相同的行为,您必须编写

[NSNumber numberWithInt:(int)(random()% 6)]

which is then converted to

然后转换为

@((int)(random()% 6))

If you don't care which "flavor" of number object you get, then just convert that line manually to

如果你不关心你得到的数字对象的“味道”,那么只需手动将该行转换为

[sequence addObject:@(random()% 6)];

but Xcode cannot decide that for you.

但Xcode无法为您决定。