What's the difference between void* and id? [duplicate]
开发者_运维知识库Possible Duplicate:
objective c difference between id and void *
For example, an UIView animation delegate callback method has this argument:
context:(void*)context
Why not simply id? I always pass nil if I don't care about an context. But when I want one, I pass the object. What's the difference? Why did they prefer void* over id?
id is a pointer to a struct which has isa
pointer (which denotes to ObjectiveC runtime that this "thing" is object). void*
is simply a pointer to possibly anything (probably another id
).
It's to allow you to pass a pointer to anything you like. Can be an obj-c object, a pointer to some array, a struct, whatever. The void *
type is just the most general, "anything goes" type.
If you're just passing nil, don't worry about it. You could pass NULL instead if it makes you feel better.
I think you're asking something more than what the difference between id and void* is. It seems you are asking why is it void* in this case.
This is void* because this is your pointer. No one but you will use this. This pointer will not be retained or otherwise used. When the animation calls your callback method it will return this pointer to you untouched. What you do with it depends on what you put in the context: in the first place.
Being a void* lets you do a lot of things. You could pass in a standard objective-c object. You could pass a simple C style data structure. You could pass in a function pointer. You could pass nil and do nothing.
精彩评论