NSNumber 使用 setter 时发出警告,但代码似乎工作正常 - 传递参数 1 从指针生成整数而不进行强制转换
我有一个对象 Foo,它有一个 NSNumber 属性,声明如下:
@property (retain) NSNumber *siteID;
和 @synthesized。
当我做这样的事情时:
Foo *myFoo = [[Foo alloc] init];
NSNumber *newNumber = [[NSNumber alloc] initWithInt:42];
myFoo.siteID = newNumber;
[newNumber release];
第 3 行上通过合成设置器的分配似乎工作得很好 - myFoo.siteID 具有我期望的值,我可以继续我的业务。然而,我收到警告:
Passing argument 1 of 'setSiteID: makes integer from pointer without a cast'
我担心我做错了什么并且错误地完成了这项任务,尽管一切似乎都正常。
我知道 NSNumbers 是不可变的,并且我已经阅读了一些有关无法分配该值的其他问题。如果有一个现有的主题涵盖了这种情况,或者如果我只是遗漏了属性声明的一些基本内容,我深表歉意。
感谢您的任何提示。
I have an object Foo that has an NSNumber property which is declared like this:
@property (retain) NSNumber *siteID;
and @synthesized.
When I I do something like this:
Foo *myFoo = [[Foo alloc] init];
NSNumber *newNumber = [[NSNumber alloc] initWithInt:42];
myFoo.siteID = newNumber;
[newNumber release];
The assignment on line 3 that goes through the synthesized setter appears to work just fine- myFoo.siteID has the value I'm expecting I'm able to go about my business. However, I get a warning:
Passing argument 1 of 'setSiteID: makes integer from pointer without a cast'
I'm concerned I'm doing something wrong and approaching this assignment incorrectly even though everything appears to be functioning OK.
I understand that NSNumbers are immutable and I've read some other questions about not being able to assign the value. Apologies if there is an existing topic that covers this case or if I'm just missing something basic with the property declaration.
Thanks for any tips.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
非常感谢所有的建议和后续问题。我觉得自己像个白痴,我犯了这样的错字,我声明的班级与我分配的班级不同。这些课程非常相似。
oldFoo *myFoo = [[Foo alloc] init];
使用实际非常长的类名更难发现打字错误 - 自动完成功能不发布有问题的确切代码并保留旧代码的危险。
该类的旧版本还有一个 siteID 属性。事物正在与新对象一起分配并与旧对象一起声明。再次 - 今天早上感觉很傻,但当我试图找出这个问题时,真的很感谢大家的帮助。至少我有良好的本能,总是留意警告:)。
Really appreciate all the suggestions and followup questions. I feel like an idiot I had a typo like this where I was declaring with a different class than I was allocing with. Those classes were very similar.
oldFoo *myFoo = [[Foo alloc] init];
Typo was harder to spot with the actual very long class name - the perils of autocomplete not posting with the exact code in question and keeping old code around.
The old version of the class also had a siteID property. Things were were getting alloced with the new object and declared with the old. Again - feeling silly this morning but truly appreciate everybody's help as I tried to track this down. At least I have the good instinct to always heed warnings :).
也许该属性引用的成员在声明中没有“*”?
编辑:
“.m”文件中有
setSiteID:
方法吗?Maybe the member that this property refers to has no '*' in the declaration?
EDIT:
Do you have
setSiteID:
method in ".m" file?错误消息告诉您编译器认为方法 -setSiteId: 的参数是整数,而不是 NSNumber*。这可能意味着它在使用该属性时看不到该属性的声明。反过来,这可能意味着您省略了导入 Foo 类的头文件。
The error message tells you that the compiler thinks the parameter to the method -setSiteId: is an integer, not an NSNumber*. That probably means that it cannot see the declaration of the property at the point where it is using it. This, in turn, probably means you have omitted to import the header file for the Foo class.