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

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

问题描述

Clang添加了一个关键字 instancetype ,据我所知,它替换了作为-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是否有好处?

推荐答案

肯定有好处.当您使用'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天全站免登陆