Skip to content

Instantly share code, notes, and snippets.

@dielsonsales
Last active February 18, 2017 16:04
Show Gist options
  • Save dielsonsales/72469bf00a494906b077060ddf38c5b2 to your computer and use it in GitHub Desktop.
Save dielsonsales/72469bf00a494906b077060ddf38c5b2 to your computer and use it in GitHub Desktop.
Objective-C Cheat Sheet

Properties

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]) { }

Singletons

+ (id)sharedInstance {
    static SomeManager *instance = nil;
    @synchronized(self) {
        if (instance == nil)
            instance = [[self alloc] init];
    }
    return instance;
}

Usage:

SomeManager *sharedInstance = [SomeManager sharedInstance];

ENUMS

typedef NS_ENUM(NSInteger, PlayerStateType) {
  PlayerStateOff,
  PlayerStatePlaying,
  PlayerStatePaused
};

NSNumber x NSInteger x int

  • 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 signed or unsigned char, short int, int, long int, long long int, float, or double or as a BOOL;
  • NSInteger: It's not an object. Instead they are a wrapper for integer primitive datatypes. When building 32-bit applications, NSInteger is a 32-bit integer (int). A 64-bit application treats NSInteger as a 64-bit integer (long long).
  • int: under the LP64 data model, as adopted by Apple, an int will always be 32-bits.

Passing blocks as parameter

- (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
}]

Checking iOS version

#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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment