What is the meaning of an identifier?

I am (trying) to learn Objective-C, and I continue to come across a phrase like:

-(id) init; 

And I understand that id is the Objective-C keyword, but what does it mean to say: "the compiler specifically treats id in terms of pointer type conversion rules"?

Does the id object point to its right as a pointer?

+67
types objective-c
Nov 02 2018-11-11T00:
source share
5 answers

id is a pointer to any type, but unlike void * it always points to an Objective-C object. For example, you can add something like id to NSArray, but these objects should respond to retain and release .

The compiler is completely satisfied that you are implicitly throwing any object into id , and in order to use id for any object. This is not like any other implicit casting in Objective-C and is the basis of most container types in Cocoa.

+86
Nov 02 '11 at 20:52
source share

id is a pointer to any Objective-C object ( objc_object ). This is not just a pointer to emptiness, and you should not treat it like that. It refers to an object that must have a valid isa pointer. The values ​​that can be stored in id are also not limited to NSObject and its descendants, who begin to understand the existence of the NSObject protocol, as well as NSProxy , which does not even inherit from NSObject . The compiler will allow you to assign the object referenced by type id to any type of object, assign any type of object id , and send it any message (that the compiler saw) without warning.

+26
Nov 02 2018-11-11T00:
source share

id is a generic type. This means that the compiler will expect any type of object there and will not apply restrictions. This can be useful if you expect to use more than one class of objects; you can use introspection to find out what class it is. id automatically takes a pointer, since all objects in Objective-C are passed as pointers / links.

Some additional resources:

id vs NSObject vs id *
Objective-C Programming (Wikibooks)
Introspection
Dynamic input

+18
Nov 02 '11 at 20:50
source share

Yes and no. It is true that if id x denotes x as a pointer, but says that the pointer type conversion rules are incorrect, because "id" has special type conversion rules. For example, with a void * pointer, you cannot do this:

 void *x; char *y = x; // error, this needs an explicit cast 

Conversely, this is possible with id:

 id x; NSString *y = x; 

Learn more about using the id type in object examples c .

Also, for a "modern" C target, it is preferable to use instancetype instead of "id" in the "init" methods. There's even an automatic conversion tool in Xcode to change it. Read about instancetype: Would it be useful to start using instancetype instead of id?

+4
Apr 29 '16 at 23:29
source share
  • id is the data type of object identifiers in Objective-C, which can be used for an object of any type, regardless of which class it has. id is the last super-type of all objects.

In java or C # we use this:

  Object data = someValue; String name =(Object)data; 

but in lens c

 id data= someValue; NSString *name= data; 
+4
Feb 28 '17 at 11:03 on
source share



All Articles