Would it be beneficial to begin using instancetype instead of id?(开始使用 instancetype 而不是 id 是否有益?)
问题描述
Clang 添加了一个关键字 instancetype
,尽我所能看,将 id
替换为 -alloc
和 init
中的返回类型.
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 是否有益?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:开始使用 instancetype 而不是 id 是否有益?


基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01