Mostly used for BOOL properties. Their getter’s name is changed like isXXX Example: @property(nonatomic, getter=isOn) BOOL on;
copy vs strong
Use copy for the objects implementing NSCopying protocol. For example NSMutableString. If you use retain and change the NSMutableString, the change is also reflected in the property marked with retain. strong is used mostly for objects if you want to retain that object.
assign vs weak vs unsafe_unretained
assign and unsafe_unretained is identical. iOS 4 or below uses unsafe_unretained. weak, assign and unsafe_unretained properties do not retain value. However weak also nilled out when the object pointed by it is deallocated. So it prevents crash because of dangling pointers.
NSInteger-int CGFloat-float
NSInteger and CGFloat selects the type according to architecture(32 bit or 64 bit.) NSInteger selects between int and long. CGFloat selects between float and double.
readwrite and readonly
Readwrite both getter and setter is available. Readonly only getter available. You can set a property as readonly in header file and in class extension you can change it to readwrite. By doing this, you mark property as private and can only be changed within its own class.
atomic vs nonatomic
Default is atomic for properties. Atomic is slower because it waits one thread to finish accessor method and then handle other accessor method. Nonatomic does not guarentee that and you can get unexpected behaviours in a multithread environment.
@synthesize and @dynamic
You do not have to write @synthesize for properties. You use it only when you want to change the ivar of the property. Compiler automatically assigns ivars for properties with a underscore preceding property name. If you create accessors of a property at run time and do not want compiler to automatically create for you,…
public protected private
They are used for instance variables. However in objective C accessors are preferred. public: can be accessed from other classes protected: can be accessed from subclasses private: can only accessed from same class.
switch for enums
If you switch the enums do not use default. This way, you can get a error if you do not add a new enum case into the switch statement so you can fix the bug easily.
NS_ENUM vs NS_OPTIONS
typedef NS_ENUM(NSUInteger, EnumState) { State1, State2, State3, }; typedef NS_OPTIONS(NSUInteger, EnumState) { State1 = 1 << 0, State2 = 1 << 1, State3 = 1 << 2, }; If you compile you code with Objective-C++, ORing the enumeration values of NS_OPTIONS causes error. So if you are going to OR enums, you use NS_OPTIONS.
Typed constants vs Preprocessor #define
#define ANIMATION_DURATION 0.4 This has no type information and ANIMATION_DURATION is replaced with 0.4 in every header file it is placed. Somebody can easily redefine same key by mistake. Instead of this, if you want to use constant in only one file: static const NSTimeInterval kAnimationDuration = 0.3; const means you can not change value….
Literal syntax examples
NSString *hasancan = @”This is how we create string literally”; NSNumber * num = @23; NSArray *arr = @[@”1″,@”2″,@”3″]; // Throws exception if one item is nil. Normal initalization does not throw exception but it only creates items until the nil one. NSString *first = arr[0]; NSDictionary *dic = @{ @”first”:”hasan” @”last”:”akgunduz”}; NSString *last =…
