5 Useful

Objective-c

tips & patterns

By @ErikRalston


#1: Singleton

A design pattern where a class can have only one instance

@interface Singleton : NSObject
 
+ (Singleton*)shared;
 
@end


Singleton implementation


@implementation Settings

+ (Singleton *)shared
{
static Singleton *instance = nil;
if(!instance)
instance = [[super allocWithZone:nil] init];
return instance;
}

+ (id)allocWithZone:(NSZone *)zone
{
return [self shared];
}
 
@end

#2: Typedef your blocks!


A block of code bound to a variable

The syntax for declaration and usage is very hairy:

// Declaring block inline, every time@property (nonatomic,assign) (void (^)(void)) voidBlockProperty;
// Declaring a block once

typedef void (^VoidBlock)();


// Clean declarations forevermore @property (nonatomic,assign) VoidBlock voidBlockProperty;

put them in a header file!


Be sure to include #ifndef!

#ifndef Blocks_h
#define Blocks_h
 
#import <Foundation/Foundation.h>
 
typedef void (^VoidBlock)();
 
typedef BOOL (^ReturnBoolBlock)();
 
typedef void (^DictionaryBlock)(NSDictionary *dictionary);
 
typedef void (^TableCellIndexPathBlock)(UITableViewCell *cell,NSIndexPath *indexPath);
 
#endif

#3: Operation queues 


Queues of tasks, consumed by a particular thread

A lot of components in your app, including UIViews and instances of NSManagedObjectContext, should only be accessed by the thread that created them

Operation Queues to work asynchronously, but keep important work on the main thread

Simple Operation Handling

NSOperationQueue* otherQueue = [[NSOperationQueue alloc] init];
 
[queue addOperationWithBlock:^{
// Do work off the main thread here
}];
 
NSOperationQueue* queue = [NSOperationQueue mainQueue];
 
[queue addOperationWithBlock:^{
// Do work back on the main thread here
}];

Complex operation handling

#4: Caching data in the sandbox


Good for storing bulky files that don't belong in CoreData


+(NSString *)cachesDirectoryName
{
static NSString *cachePath = nil;
if(!cachePath) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
cachePath = [paths objectAtIndex:0];
}
return cachePath;
}

saving to the cache


+(NSString *)pathForName:(NSString *)name
{
NSString *cachePath = [SimpleFilesCache cachesDirectoryName];

NSString *path = [cachePath stringByAppendingPathComponent:name];

return path;
}

+(void)saveToCacheDirectory:(NSData *)data withName:(NSString *)name
{
NSString *path = [SimpleFilesCache pathForName:name];

[data writeToFile:path atomically:YES];
}

Reading the cache


+(NSData *)cachedDataWithName:(NSString *)name
{
NSString *path = [SimpleFilesCache pathForName:name];

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:path];

if(fileExists) {
NSData *data = [NSData dataWithContentsOfFile:path];

return data;
} else {
return nil;
}
}

#5: automatic documentation


Appledoc offers automated generation of documentation
(Like Doxygen, Sandcastle, etc)

All you need to do is format your comments correctly on protocols, classes, and methods

Installing is easy using brew!
 brew install appledoc

running appledoc


Always run from the project code directory
(Do not try to document your Cocoapods)

Command-line documentation generator

  appledocs      --project-name Flex      --project-company Flex      --output ../appledocs/      .

appledocs results


Default behavior is to install into XCode Documentation System


Possible to get same docs as HTML

appledocs result

Integrated docs with the IDE


thank you!


5 Useful Objective-C Tips & Patterns

By Erik Ralston

5 Useful Objective-C Tips & Patterns

5 useful objective-c tips and patterns I've been using a lot lately. Varies between the universal and the specific.

  • 1,298