When is a pointer NOT used in Objective-C?
SomeObject *obj = [[SomeObject alloc] init]
[obj someMethod]
obj.someProperty
Both开发者_StackOverflow中文版 [] and . automatically dereference the pointer, correct? So anytime you are working with objects, you will always use pointers to reference those objects.
Will a variable ever be declared in Objective-C that does not have a pointer type?
With one exception, all objects in Objective-C live exclusively on the heap, which means you will always be dealing with object references.
The one exception is blocks, which can be created on the stack and then subsequently moved to the heap using Block_copy()
. For example:
dispatch_block_t myBlock = ^{
NSLog(@"This is a block that lives on the stack");
};
And due to how they're implemented, blocks can be treated as objects (ie, you can use them anywhere you could use an id
).
Of course, any non-Objective-C objects can be created on the stack. So if you're using Objective-C++, then you can create C++ objects on the stack (like normal), which means you'll be dealing with the object itself and not a reference. Also, any primitive can be created on the stack (int
, char*
, a structure, etc).
So in a nutshell, if you're dealing with objects, then 99.999% of the time you'll be dealing with object references, and never the object itself. Anything else is entirely up to you.
Sure - NSInteger
, NSFloat
, CGPoint
, etc. There are many data types in Objective-C that are not referenced by pointers.
精彩评论