重写方法时的严格类型与宽松类型
我有一个名为 AddressCard 的类,来自“Objective C 编程”中的示例,并且我正在实现 isEqual: 方法。
NSObject 中此方法的签名对参数使用松散类型:
- (BOOL)isEqual:(id)anObject
OTOH,书中的示例代码使用严格类型:
- (BOOL) isEqual:(AddressCard *) aCard
我不确定我是否完全理解编译器在这种情况下所做的事情。我尝试将 AddressCard 与 NSString ([aCard isEqual: @"Foo"]
) 进行比较,期望出现运行时错误(如果系统使用我的方法)或者系统将调用 NSObject 的 IsEqual 版本。
相反,当我的 IsEqual: 尝试调用特定于 AddressCard: 的方法时,我的方法被调用(即使参数是 NSString 而不是 AddressCard)并引发异常:
- (BOOL) isEqual:(AddressCard *) aCard {
if ([name isEqualToString: [aCard name]] && /*here I get the error*/
[email isEqualToString:[aCard email]]) {
return YES;
}else {
return NO;
}
}
发生了什么事? NSString 究竟是如何传递给需要其他内容的方法的?重写方法时更改方法的签名是否可以?
I have a class called AddressCard from an example in "Programming in Objective C", and I'm implementing a isEqual: method.
The signature of this method in NSObject uses loose typing for the parameter:
- (BOOL)isEqual:(id)anObject
OTOH, the sample code in the book uses strict typing:
- (BOOL) isEqual:(AddressCard *) aCard
I'm not sure I fully understand what the compiler does in this case. I tried comparing an AddressCard to a NSString ([aCard isEqual: @"Foo"]
) expecting either a runtime error (if the system uses my method) or that the system would call NSObject's version of IsEqual.
Instead, my method was called (even though the parameter was a NSString and not an AddressCard) and raised an exception when my IsEqual: tried to call a a method specific to AddressCard:
- (BOOL) isEqual:(AddressCard *) aCard {
if ([name isEqualToString: [aCard name]] && /*here I get the error*/
[email isEqualToString:[aCard email]]) {
return YES;
}else {
return NO;
}
}
What's going on? How on Earth is an NSString being passed to a method that expects something else? Is changing the signature of a method OK when overriding it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
运行时通过消息的选择器来区分消息。所有具有相同名称的方法都具有相同的选择器。方法参数对选择器没有影响。在您的情况下,选择器是
isEqual:
。这是来自 Apple 的“The Objective-C 编程语言”(强调我的):
换句话说:更改现有方法的签名不是好的形式(IMO),但只要您静态键入这些方法的接收者(在您的情况下,这意味着
aCard
必须声明为AddressCard *
)。对于运行时来说,这没有问题。不幸的是,您没有提及编译器是否会向您发出警告,因为您在需要
AddressCard *
的地方传递了NSString *
。我希望它会这样做。The runtime distinguishes messages by their selector. All methods with the same name have the same selector. Method arguments have no influence on the selector. In your case, the selector is
isEqual:
.This is from Apple's "The Objective-C Programming Language" (emphasis mine):
In other words: changing the signature of an existing method is not good form (IMO) but it's fine as long as you statically type the receivers of these methods (in your case, that means
aCard
must be declared asAddressCard *
). For the runtime, this is no problem.Unfortunately, you are not mentioning whether the compiler gives you a warning because you are passing an
NSString *
where it expects anAddressCard *
. I would expect it to do so.我最好的猜测:编译器看到的只是一个期望使用指针参数调用指针的方法。编译器没有问题。
My best guess: All the compiler sees is a method that expects a pointer gets called with a pointer parameter. No problems for the compiler.