Objective-C: difference between id and void *
来源:互联网 发布:淘宝购物车怎么分享 编辑:程序博客网 时间:2024/04/28 05:01
void *
means "a reference to some random chunk o' memory with untyped/unknown contents"
id
means "a reference to some random Objective-C object of unknown class"
There are additional semantic differences:
Under GC Only or GC Supported modes, the compiler will emit write barriers for references of type
id
, but not for typevoid *
. When declaring structures, this can be a critical difference. Declaring iVars likevoid *_superPrivateDoNotTouch;
will cause premature reaping of objects if_superPrivateDoNotTouch
is actually an object. Don't do that.attempting to invoke a method on a reference of
void *
type will barf up a compiler warning.attempting to invoke a method on an
id
type will only warn if the method being called has not been declared in any of the@interface
declarations seen by the compiler.
Thus, one should never refer to an object as a void *
. Similarly, one should avoid using an id
typed variable to refer to an object. Use the most specific class typed reference you can. Even NSObject *
is better than id
because the compiler can, at the least, provide better validation of method invocations against that reference.
The one common and valid use of void *
is as an opaque data reference that is passed through some other API.
Consider the sortedArrayUsingFunction: context:
method of NSArray
:
- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context;
The sorting function would be declared as:
NSInteger mySortFunc(id left, id right, void *context) { ...; }
In this case, the NSArray merely passes whatever you pass in as the context
argument to the method through as the context
argument. It is an opaque hunk of pointer sized data, as far as NSArray is concerned, and you are free to use it for whatever purpose you want.
Without a closure type feature in the language, this is the only way to carry along a hunk of data with a function. Example; if you wanted mySortFunc() to conditionally sort as case sensitive or case insensitive, while also still being thread-safe, you would pass the is-case-sensitive indicator in the context, likely casting on the way in and way out.
Fragile and error prone, but the only way.
Blocks solve this -- Blocks are closures for C. They are available in Clang -- http://llvm.org/ and are pervasive in Snow Leopard (http://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/GCD_libdispatch_Ref.pdf).
From http://stackoverflow.com/questions/1304176/objective-c-difference-between-id-and-void
- Objective-C: difference between id and void *
- Difference between release and dealloc in objective-c
- Objective-C’s Roots: Difference Between messaging structure and function calling; and the Runtime
- What is the difference between #import and #include in Objective-C?
- 26.Difference between protocol in objective c and interfaces in java?
- the difference between ClientID and ID of web control
- The difference between declaring a variable “id” and “NSObject *”
- The difference between (()) and ().
- difference between "<" and "<<"
- What is the difference between XML and C or C ?
- An interesting difference between C# and C++/CLI
- more effective c++:the difference between pointer and reference
- What's the difference between gcc and g++/gcc-c++?
- What is the difference between fprintf and vfprintf in C++?
- what is the difference between definition and declaration in c
- Difference between 'struct' and 'typedef struct' in C++?
- c++: Difference between member and non member functions
- The difference between Go and C-like language
- android学习日志02__HelloWorld 和 程序目录结构
- 如何使用Log4j?
- DEDE全局变量再次覆盖研究
- 4Sum
- 美轮美奂的phonegap(七)---系统事件的注册
- Objective-C: difference between id and void *
- 【练习题】编写打印出一个单链表的所有元素的程序【链表】
- SQL 数据库中的存储过程的参数问题(case when)
- ZooKeeper典型应用场景一览
- 使用Android application测试API
- u-boot编写记录(第一阶段)
- Please use the /MD switch for _AFXDLL builds的错误
- Android实现通用的ActivityGroup(效果类似Android微博客户端主界面),强烈建议不要再使用TabActivity
- 旺才日记