简体   繁体   中英

Objective-C use of pointers

When do you and when dont you need the * symbol (which is because in objective-c all variables like NSString are pointer variables)?

For example when do you need to do "NSString *" instead of just "NSString"?

In Objective-C, all object references are pointers, so you always need the pointer operator when you declare with an Objective-C object.

For other types, the use is exactly the same as in C. Use pointers when you want to pass data structures or primitive types by reference.

You use the asterisk for all Objective-C objects (such as NSDictionary, NSString, NSNumber).

For anything that is a primitive type (int, double, float) you don't need the asterisk. However, the NS prefix doesn't always mean that you must use an asterisk. Cocoa defines some structures (such as NSInteger, NSRect, NSPoint) that are are based on primitive types. You don't use the asterisk here either. An NSRect, for example, is just a structure of an NSPoint and NSSize, both of which are made up of 2 CGFloats (a primitive type).

You can pass a pointer to one of these primitive types or structures using the * notation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM