理解Objective C 中id

什么是id,与void *的区别this

id在Objective C中是一个类型,一个complier所承认的Objective C类型,跟void *是不同的,好比一个 id userName, 和void *pUserName,[userName print] 是能够的,但[pUserName print] 在编译时就会报错,由于ObjeciveC的编译器看到id,会假定它能够接受任何message,虽然在runtime时可能并非这样的,但pUserName并非Objective C类型,编译器就会报错,可是void *有可能时能够接收print message的。.net

/**
 * Type for Objective-C objects.
 */
typedef struct objc_object
{
	/**
	 * Pointer to this object's class.  Accessing this directly is STRONGLY
	 * discouraged.  You are recommended to use object_getClass() instead.
	 */
#ifndef __OBJC_RUNTIME_INTERNAL__
	__attribute__((deprecated))
#endif
	Class isa;
} *id;

id 与 NSObject *的区别对象

 

id与instanceTypeblog

 

何时应该用idtoken

  1. 当须要建立一个Collection类或者方法,这些类或方法接收各类不一样类型时,好比NSArray中便可以接收NSString,又能够NSObject,还能够NSProxy,就须要用id;
  2. 当须要把某些信息做为context或者token 对象传出给public API使用,而caller不能改变所传出的对象时,就能够使用id做为类型;

 

init 方法的实现ci

convience constructor get

 

http://tewha.net/2013/02/why-you-should-use-instancetype-instead-of-id/编译器

http://stackoverflow.com/questions/7903954/why-use-id-when-we-can-just-use-nsobjectit

相关文章
相关标签/搜索