开始使用 instancetype 而不是 id 是否有益? [英] Would it be beneficial to begin using instancetype instead of id?

查看:17
本文介绍了开始使用 instancetype 而不是 id 是否有益?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Clang 添加了一个关键字 instancetype,尽我所能看,将 id 替换为 -allocinit 中的返回类型.

Clang adds a keyword instancetype that, as far as I can see, replaces id as a return type in -alloc and init.

使用 instancetype 代替 id 有什么好处吗?

Is there a benefit to using instancetype instead of id?

推荐答案

肯定有好处.当你使用 'id' 时,你基本上没有得到任何类型检查.使用 instancetype,编译器和 IDE 可以知道返回的是什么类型的东西,并且可以更好地检查您的代码并更好地自动完成.

There definitely is a benefit. When you use 'id', you get essentially no type checking at all. With instancetype, the compiler and IDE know what type of thing is being returned, and can check your code better and autocomplete better.

只在当然有意义的地方使用它(即返回该类实例的方法);id 还是有用的.

Only use it where it makes sense of course (i.e. a method that is returning an instance of that class); id is still useful.

这篇关于开始使用 instancetype 而不是 id 是否有益?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆