ID, NSObject *, id<nsobject> difference

Source: Internet
Author: User

We often confuse the following three types of statements:  
  1. ID foo1;
  2. nsobject *foo2;
  3. id<nsobject> Foo3;

  The first is the most commonly used, which 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.

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

We know that the ID type is a Objective-c object, but not all point to an object that inherits from Nsojbect, even though the type and NSObject object have many common methods, like 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.

So, make a statement that a generic NSObject object pointer is similar to what you do in other languages, like Java, but other languages have certain limitations and are 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.

Id<nsobject> tells the compiler that you don't care what type of object it is, but that it has to comply with the NSObject Protocol (Protocol) and that the compiler can guarantee all assignments to id<nsobject> Objects of the type are subject 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 properly, but id<nsobject> does not work properly. 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.

Ps:

The ID can also represent the underlying type, but it cannot represent the float,double type
NSObject *foo2; If you use this form, you need to force the type conversion when you use it, which can be cumbersome

transferred from:http://www.cocoachina.com/bbs/read.php?tid=151376

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.