XCode 警告:初始化使指针从整数而不进行强制转换(尝试为 NSNumber 赋值时)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1502723/
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
XCode warning : Initialization makes pointer from integer without a cast (while trying to assign a value to a NSNumber)
提问by NSSec
I'm stuck with a warning in Xcode while trying to develop a small iPhone App. I've got this piece of code in Xcode :
我在尝试开发一个小型 iPhone 应用程序时遇到了 Xcode 中的警告。我在 Xcode 中有这段代码:
NSNumber *randomNumber = arc4random() % [array count];
NSNumber *randomNumber = arc4random() % [数组计数];
If I NSLog randomNumber, everything seems to work but Xcode keeps warning me, saying :
如果我 NSLog randomNumber,一切似乎都正常,但 Xcode 不断警告我,说:
Initialization makes pointer from integer without a cast
初始化使指针从整数而不进行强制转换
There must be something I didn't understand. Thanks in advance for helping me out :)
一定有什么我不明白的地方。在此先感谢您帮助我:)
回答by NSSec
NSNumber is an objc object, while an int or float isn't. You're now assigning an int/float to a pointer, which isn't quite right.
NSNumber 是一个 objc 对象,而 int 或 float 不是。您现在将一个 int/float 分配给一个指针,这不太正确。
Try:
尝试:
// if the result is an int:
NSNumber *randomNumber = [NSNumber numberWithInt: (arc4random() % [array count])];
// if the result is a float:
NSNumber *randomNumber = [NSNumber numberWithFloat: (arc4random() % [array count])];