Using properties instead of instance variables in as many places as possible provides many benefits:
- By default getter and setter methods are created for you;
- Properties provide the potential for declaration of attributes like assign (vs copy), weak, atomic (vs nonatomic), and so on;
@property (readonly, getter=isBlue) BOOL blue;Can be used as:
if (color.blue) { }
if (color.isBlue) { }
if ([color isBlue]) { }+ (id)sharedInstance {
static SomeManager *instance = nil;
@synchronized(self) {
if (instance == nil)
instance = [[self alloc] init];
}
return instance;
}Usage:
SomeManager *sharedInstance = [SomeManager sharedInstance];typedef NS_ENUM(NSInteger, PlayerStateType) {
PlayerStateOff,
PlayerStatePlaying,
PlayerStatePaused
};- NSNumber: It's a subclass of NSValue that offers a value as any C scalar (numeric) type. It defines a set of methods specifically for setting and accessing the value as a
signedorunsigned char,short int,int,long int,long long int,float, ordoubleor as aBOOL; - NSInteger: It's not an object. Instead they are a wrapper for integer primitive datatypes. When building 32-bit applications,
NSIntegeris a 32-bit integer (int). A 64-bit application treatsNSIntegeras a 64-bit integer (long long). - int: under the LP64 data model, as adopted by Apple, an int will always be 32-bits.
- (void)asyncMethodWithBlock:(void (^)(void))completion {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
// Backgroundn code
dispatch_async(dispatch_get_main_queue(), ^(void) {
completion();
});
});
}
[myObject asyncMethodWithBlock: ^{
// code to do execute after the async operation
}]#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)