为什么 Xcode 会认为实例方法是类方法?
我正在尝试编译一些代码,其中有一个名为 Card 的类。它有一个名为
-(void)setSuit: (NSString *)suit
的方法,它是一个实例方法,但是当我说 [Card setSuit:@"Diamonds"]
Xcode 说: 警告:“Card”可能无法响应方法 +setSuit
而且我的程序无法运行。我认为 Xcode 认为 setSuit 是一个类方法,正如警告中所说,那么我如何告诉它我正在谈论一个实例方法?
或者也许这根本不是问题,我真的不知道,因为我以前从未遇到过这种情况。
I am trying to compile some code where I have a class called Card. It has a method called
-(void)setSuit: (NSString *)suit
It is an instance method, but when I say[Card setSuit:@"Diamonds"]
Xcode says: warning: "Card" may not respond to method +setSuit
And my program doesn't work. I think that Xcode thinks setSuit is a class method, as it says in the warning, so how do I tell it that I am talking about an instance method?
Or maybe that isn't the problem at all, I don't really know as I have never encountered this before.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题就在这里:
如果
Card
是类,那么上面的行将尝试调用类上的方法,而不是实例。您需要改为调用实例上的方法,例如:The problem is here:
If
Card
is the class, then the above line will try to call a method on the class, not an instance. You'll need to call a method on an instance instead, say:您正在尝试将
-setSuit:
发送到Card
类。您可能希望将该消息发送到Card
的实例,而不是类。You're trying to send
-setSuit:
to theCard
class. You probably want to send that message to an instance ofCard
, not the class.