moop
moop

Reputation: 21

Objective-C: @property attribute for basic types

The common practice before arc was to use assign for basic types like int. What are we supposed to use for them when using ARC?

@property (nonatomic,assign) int foo;

Also: Why does Apple seem to encourage use of NSInteger instead of e.g. long?

Upvotes: 1

Views: 56

Answers (2)

luk2302
luk2302

Reputation: 57184

The following is enough since assign is the default anyway:

@property (nonatomic) int foo;

Regarding NSInteger vs int / long: NSInteger is preferred because it removes you having to worry about what bit-architecture you are on, als Leejay already mentioned.

I want to additionally mention that using NSInteger makes it a bit easier to transition to swift in the future, since NSInteger is simply bridged to Int, on of the most "native" data types. If you use int in Objective-C you have to deal with Int32 in swift, same goes for long (Int64). Is simply gets messy and can be avoided pretty easily by using NSInteger for all properties and method parameters or return values.

Upvotes: 2

Leejay Schmidt
Leejay Schmidt

Reputation: 1213

@property (nonatomic,assign) int foo;
is still used for primitives.

ARC stops you from using retain, like

@property (nonatomic,retain) NSObject *foo;
subbing it with weak or strong.

And you want to use NSInteger because it is a known number of bits no matter if you are running on 32- or 64-bit architecture. int or long can be architecture-dependent, so it just gives you extra predictability/piece of mind.

There's a great discussion about this in a previous question.

Upvotes: 1

Related Questions