这个 KVC 验证方法中的类型重要吗?

发布于 2024-09-05 06:36:42 字数 295 浏览 1 评论 0原文

例如,在文档中,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 技术交流群。

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

发布评论

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

评论(1

请止步禁区 2024-09-12 06:36:42

那是行不通的,因为它们不一样。 id* 会更接近于 NSNumber**,因为该方法接受指向指针的指针。所以你的方法看起来像:

-(void)validateAge:(NSNumber**)ioValue error:(NSError**)outError

但是这样做没有没有意义。 id 将完美地满足您需要执行的所有操作,如果您更改它,然后稍后调整该属性,您可能会在您的应用程序中引入一个微妙的错误。

简短的回答,是的,您可以更改输入参数类型;但你真的不应该这样做。

That would not work because they are not the same. id* would be closer to NSNumber** as the method accepts a pointer to a pointer. So your method would look like:

-(void)validateAge:(NSNumber**)ioValue error:(NSError**)outError

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.

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