Adopting modern Objective-c translations

Source: Internet
Author: User




Over the years, OBJECTIVE-C has been developing and evolving. Although the core concepts and practices have remained unchanged, it has undergone great changes in many areas and made great progress. These changes improve features such as type safety, memory management, performance, and so on, making it easier to write objective-c.     Adapting to these changes will be important in order to make your current and future code more robust, reliable, and resilient. Xcode has prepared a tool for you to adapt to these structural changes. But before you use it, you'll wonder what changes these changes will make to your code and why you're making those changes. This document focuses on the most important and useful changes to take place in your code. Instancetype uses the Instancetype keyword as the return value type of the class initialization method.     These methods include Alloc,init and class factory methods. Using instancetype instead of ID in the right place in your OC code really improves type safety. For example, consider the following code:
 
 
1 @interface MyObject : NSObject
 2 + (instancetype)factoryMethodA;
 3 + (id)factoryMethodB;
 4 @end
 5  
 6 @implementation MyObject
 7 + (instancetype)factoryMethodA { return [[[self class] alloc] init]; }
 8 + (id)factoryMethodB { return [[[self class] alloc] init]; }
 9 @end
10  
11 void doSomething() {
12    NSUInteger x, y;
13  
14     x = [[MyObject factoryMethodA] count]; // Return type of +factoryMethodA is taken to be "MyObject *"
15     y = [[MyObject factoryMethodB] count]; // Return type of +factoryMethodB is "id"
16 }
because the return value type of +factorymethoda is instancetype, the delivery message is expressed as a myobject * type. MyObject does not-count this method, so the compiler will give the following warning:MAIN.M: ' MyObject ' may isn't respond to ' count '
However, because the return value type of +FACTORYMETHODB is an ID type, the compiler will not give a corresponding warning.      Because an object of the ID type can be any class, at the same time, the-count method may exist with a class, so for the compiler, the +factorymethodb return value is determined by the possibility to implement the-count method. To ensure that the Instancetype factory method has the correct subclass representation, be sure to use [self class] instead of [Class name class] When initializing the class instance. This ensures that the compiler correctly infers the subclass type. For example, what happens if you do the following in the MyObject subclass of the code above?
 
 
1 @interface MyObjectSubclass : MyObjet
2 @end
3  
4 void doSomethingElse() {
5         NSString *aString = [MyObjectSubclass factoryMethodA];
6 }
The compiler will give the following warning:
1 main.m:incompatible pointer types initializing ' NSString * ' with an expression of type ' myobjectsubclass * '





Adopting modern objective-c translations


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.