ios_oc_id, NSObject, id<nsobject> difference

Source: Internet
Author: User

We often confuse the following three statements (which I have not noticed):
1. ID foo1;
2. NSObject *foo2;
3. id<nsobject> Foo3;

        The first is the most common, it simply states that pointers to objects are not given any type of information to the compiler, so the compiler does not do type checking. But also because of this, you can send any information to an object of type ID. This is why +alloc returns the ID type, but calling [[Foo alloc] init] does not produce a compilation error.

&NBSP;      Therefore, the ID type is the dynamic type at run time. The compiler cannot know its true type, even if you send a method with an ID type that does not have a compile warning.

&NBSP;       we know, The ID type is a Objective-c object, but it does not all point to objects that inherit from Nsojbect, even though the type and NSObject objects have many common methods, such as retain and release. To let the compiler know that this class inherits from NSObject, one solution is to use the NSObject static type, as in the 2nd, when you send a method that NSObject does not have, like length or count, the compiler gives a warning. This also means that you can safely use these methods like Retain,release,description.

        Therefore, declare that a generic NSObject object pointer is similar to what you do in other languages, like Java, but other languages have certain limitations, Not as flexible as objective-c. Not all Foundation/cocoa objects inherit nsobject, such as Nsproxy does not inherit from NSObject, so you cannot use nsobject* to point to this object, Even if the Nsproxy object has a generic method such as release and retain. To solve this problem, you need a pointer to the object that owns the NSObject method, which is the 3rd usage scenario.

the ID tells the compiler that you don't care what type of object it is, but it must comply with the NSObject Protocol (protocol), The compiler will ensure that all objects assigned to the ID type adhere to the NSObject Protocol (Protocol). Such pointers can point to any NSObject object because the NSObject object adheres to the NSObject Protocol (Protocol), and it can also be used to hold nsproxy objects because it also adheres to the NSObject Protocol (Protocol). This is very powerful, convenient and flexible, and you don't have to care about what type of object it is, but what it does.

now you know what type you're going to use, don't you?

if you do not need any type checking, use the ID, which is often used as the return type, and is also often applied to the declaration Agent (delegate) type. Because proxy types are typically run, they are checked for implementation of those methods.

If you really need a compiler check, then consider using the 2nd or 3rd type. It is rare to see nsobject* functioning correctly, but the ID does not work correctly. The advantage of using Protocol (protocol) is that it can point to Nsproxy objects, and more commonly, you just want to know what protocol an object adheres to, not what type it is.

ios_oc_id, NSObject, id<nsobject> difference

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.