这个 KVC 验证方法中的类型重要吗?
例如,在文档中,KVC 样式的验证方法是这样实现的:
-(BOOL)validateAge:(id *)ioValue error:(NSError **)outError
他们使用 id* 作为 ioValue 的类型。由于这不是方法签名的一部分,我想知道这样做是否会造成伤害:
-(BOOL)validateAge:(NSNumber *)ioValue error:(NSError **)outError
这对 KVC 来说还好吗?
For example, in the docs a KVC-style validation method is implemented like this:
-(BOOL)validateAge:(id *)ioValue error:(NSError **)outError
They used id* as the type for ioValue. Since that's not part of the method signature, I wonder if it would hurt to do something like:
-(BOOL)validateAge:(NSNumber *)ioValue error:(NSError **)outError
Is this still fine with KVC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那是行不通的,因为它们不一样。
id*
会更接近于NSNumber**
,因为该方法接受指向指针的指针。所以你的方法看起来像:但是这样做没有没有意义。
id
将完美地满足您需要执行的所有操作,如果您更改它,然后稍后调整该属性,您可能会在您的应用程序中引入一个微妙的错误。简短的回答,是的,您可以更改输入参数类型;但你真的不应该这样做。
That would not work because they are not the same.
id*
would be closer toNSNumber**
as the method accepts a pointer to a pointer. So your method would look like:But there is NO point in doing that.
id
will work perfectly fine for everything that you need to do and if you change it and then adjust that attribute later you can and would introduce a subtle error into your application.Short answer, yes you can change the input parameter type; but you really shouldn't.