Why are some properties and variables in Objective C initialized with a pointer while others are not?

StackOverflow https://stackoverflow.com/questions/23465485

  •  15-07-2023
  •  | 
  •  

Question

Coming from Java et al, I'm not clear on the difference between these two declarations:

@property (nonatomic, readwrite) NSInteger score;
@property (nonatomic, strong) NSMutableArray *cards;

Why is the pointer, *, not a requirement on both property declarations?

I've seen this a lot in local variables too:

- (void)viewDidLoad
{
    [super viewDidLoad];

    int foo = 1;
    NSString *bar = @"foo";
}

What's the difference between static allocation of primitive type int and NS types?

Was it helpful?

Solution 3

If you look at the definition of NSInteger you'll see that it is a typedef for a simple integer. Basically, all the non-object types are stored as simple values, while the types that are complex objects are typically pointer properties. There are a couple reasons why these more complex objects are stored as pointers:

  1. Using the value, itself, instead of the pointer would require copying (that is, if you use a pointer, you can put the object somewhere else and you only need to copy the much shorter address rather than all of the content that happens to be in that object, and hence it is more efficient that way).

  2. When using a non-pointer type, it is necessary to know the required storage space, which works if you know the exact type of the object, but fails in the case of inheritance (e.g. an NSMutableArray may add additional fields to NSArray, for example. If you were to use NSArray instead of NSArray*, then assigning from an NSMutableArray would be broken, because the system would only have set aside enough space for the fields in the base class and not for the derived class. When using a pointer, however, since the pointer size is the same for both the base and derived types, one can assign the pointer for a derived type to a pointer to the base type, and still have things work correctly).

Note that it is possible and safe to use a pointer type with these primitive types, as well; however, this is not done for efficiency reasons (it would create additional allocation and dereferencing where not necessary).

OTHER TIPS

Objective-C objects are always allocated on the heap, so you always access them through pointers. Variables of primitive (or struct) types can be, and typically are, allocated on the stack and accessed without pointers.

If you're familiar with Java, it's basically the same semantics. Primitive types are accessed and passed around by value, objects by reference. The difference is that ObjC has (by virtue of its C lineage) syntax explicitly marking that difference.

Type names that start with an uppercase prefix in Apple frameworks aren't all ObjC classes. NSInteger is a primitive type just like int, so you can and usually do use it without pointers.

pointer is always used for referring to something at the heap but not when you referring to something on the stack.But for some primitive types and simple structure which are accessed via the stack so you don't need to use pointer..

NSInteger is a primitive type, that means it will be stored locally on the stack. there is no need to use a pointer to access it, but if you want to use pointer then you can.

You can have a pointer to an NSInteger if you really want to with following way:

NSInteger *pointerToProcessID = &yourNsintegervar;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top