NSNumber 使用 setter 时发出警告,但代码似乎工作正常 - 传递参数 1 从指针生成整数而不进行强制转换

发布于 2024-09-11 00:32:11 字数 645 浏览 8 评论 0原文

我有一个对象 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神回复 2024-09-18 00:32:11

非常感谢所有的建议和后续问题。我觉得自己像个白痴,我犯了这样的错字,我声明的班级与我分配的班级不同。这些课程非常相似。

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 :).

暗地喜欢 2024-09-18 00:32:11

也许该属性引用的成员在声明中没有“*”?

编辑
“.m”文件中有 setSiteID: 方法吗?

Maybe the member that this property refers to has no '*' in the declaration?

EDIT:
Do you have setSiteID: method in ".m" file?

Bonjour°[大白 2024-09-18 00:32:11

错误消息告诉您编译器认为方法 -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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文