Over the years, the Objective-C language has grown and evolved. Although the core concepts and practices remain the same, parts of the language have been through significant changes and improvements. These modernizations improve type safety, memory management, performance, and other aspects of Objective-C, making it easier for you to write correct code. It’s important to adopt these changes in your existing and future code to help it become more consistent, readable, and resilient.
Xcode provides a tool to help make some of these structural changes for you. But before you use this tool, you want to understand what changes it will offer to make to your code, and why. This document highlights some of the most significant and useful modernizations to adopt in your codebase.
多年以来,oc已经成长并且进化了。尽管核心思想和实践保持不变,但是语言的部分已经发生了重大的变化和改善。这些新的变化使得oc类型更加的安全,内存管理,性能以及其他的方面。这使得oc更加易于写出正确的代码。它是重要的适应这种变化在已写的代码和以后的代码中帮助它更加的一致,可读和有弹性。
Xcode提供了工具老帮助你进行这些变化。但是在你使用这些工具之前,你想要理解它对你的代码做了些什么。这篇文章着重强调了一些重大的和有用的现代语法变化来调整你打代码。
1.instancetype
Use the instancetype keyword as the return type of methods that return an instance of the class they are called on (or a subclass of that class). These methods include alloc,init, and class factory methods.
Using instancetype instead of id in appropriate places improves type safety in your Objective-C code.
用instancetype作为这些方法的返回值,这些方法包括alloc,init,工厂方法。用instancetype取代id来改善类型安全。
属性:
属性为我们提供了三部分:get方法,set方法,全局变量。大大简化了代码。
Enumeration Macros
The NS_ENUM and NS_OPTIONS macros provide a concise, simple way of defining enumerations and options in C-based languages. These macros improve code completion in Xcode and explicitly specify the type and size of your enumerations and options. Additionally, this syntax declares enums in a way that is evaluated correctly by older compilers, and by newer ones that can interpret the underlying type information.
NS_ENUM和NS_OPTIONS提供了一种简明,简单的方式来定义C语言的枚举。这种枚举改善了在Xcode中的代码,显示强调了枚举的类型和大小。总之,这种语法更好。
博客参考:https://developer.apple.com/library/content/releasenotes/ObjectiveC/ModernizationObjC/AdoptingModernObjective-C/AdoptingModernObjective-C.html