开发者

When to and when not to use pointers in Objective-C

开发者 https://www.devze.com 2023-02-12 18:29 出处:网络
I know there are a lot of questions on pointers out there, particularly now for Objective-C. But I\'m looking for some higher level answers to help me understand the paradigms in Objective-C.

I know there are a lot of questions on pointers out there, particularly now for Objective-C. But I'm looking for some higher level answers to help me understand the paradigms in Objective-C.

I've heard so开发者_开发问答me people say that using pointers in Objective-C is a matter or experience, i.e. some classes demand that you use pointers, others don't. Is this true? And is that the extent of using pointers in Objective-C.

Basically, apart from when you want to explicitly pass reference variable to methods, what are the rules for pointers in Objective-C?


You use a pointer always when referring to something on the heap and sometimes, but usually not when referring to something on the stack.

Since Objective-C objects are always allocated on the heap (with the exception of Blocks, but that is orthogonal to this discussion), you always use pointers to Objective-C objects. Both the id and Class types are really pointers.

Where you don't use pointers are for certain primitive types and simple structures. NSPoint, NSRange, int, NSUInteger, etc... are all typically accessed via the stack and typically you do not use pointers.

As for Why the * in Objective-C?, you might find this question of interest.

0

精彩评论

暂无评论...
验证码 换一张
取 消