This code below is outdated, it is for reference only
For more accurate code snippets and helpers I made a lib
Check it out here https://github.com/jrmgx/swift

XCode tricks

Application and viewController life cycles

viewDidLoad is called exactly once, when the view controller is first loaded into memory. This is where you want to instantiate any instance variables and build any views that live for the entire lifecycle of this view controller. However, the view is usually not yet visible at this point.

viewDidAppear is called when the view is actually visible, and can be called multiple times during the lifecycle of a View Controller (for instance, when a Modal View Controller is dismissed and the view becomes visible again). This is where you want to perform any layout actions or do any drawing in the UI - for example, presenting a modal view controller. However, anything you do here should be repeatable. It’s best not to retain things here, or else you’ll get memory leaks if you don’t release them when the view disappears.

Instantiate a VC thru an id

YourViewControllerClass *viewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];

init

- (id) init {
    self = [super init];
    if (self) {
        // DO SOMETHING
    }

    return self;
}

Change the font of a button or label

[label setFont:[UIFont fontWithName:@"WillardSniffinScript-Light" size:20.0]];

List of fonts installed in the App

NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];

NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily = 0; indFamily < [familyNames count]; ++indFamily) {
    NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
    fontNames = [[NSArray alloc] initWithArray:
                    [UIFont fontNamesForFamilyName:
                    [familyNames objectAtIndex:indFamily]]
                ];
    for (indFont = 0; indFont < [fontNames count]; ++indFont) {
        NSLog(@"    Font name: %@", [fontNames objectAtIndex:indFont]);
    }
}

iterate over NSArray

for (NSString *mood in moods) {
    // mood will be a NSString
}

iterate over NSDictionary

for (NSString *key in self.selected /*NSDictionary*/) {
    NSString *uid = [self.selected objectForKey:key];
    if (uid) {
        self.cream.people = [self.cream.people stringByAppendingFormat:@"%i,", [uid integerValue]];
    }
}

Convert a NSString to NSData

NSString *string = @"string";
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
// opposite
[NSString stringWithUTF8String:[data bytes]]

Perform selector after a delay

[self performSelector:@selector(myMethod) withObject:nil afterDelay:delay];

Using a dispatch queue

dispatch_queue_t queue;
queue = dispatch_queue_create("com.example.MyQueue", NULL);
// dispatch_get_main_queue()

double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, queue, ^(void) {
	//code to be executed on the main queue after delay
});

Using a Timer

self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
[self.timer fire];

Animate a list of images

// Array to hold jpg images
NSMutableArray *imageArray = [[NSMutableArray alloc] initWithCapacity:12];

UIImageView *im = [[UIImageView alloc] initWithFrame:CGRectMake(1, 5, 100, 100)];

// Build array of images, cycling through image names
for (int i = 1; i <= 3; i++) {
    [imageArray addObject:[UIImageimageNamed: [NSStringstringWithFormat:@"%02d.png", i]]];
    NSLog(@"01-%02d.gif", i);
}

im.animationImages      = [NSArray arrayWithArray:imageArray];
im.animationDuration    = 0.3;
im.animationRepeatCount = 0;

[im startAnimating];

[self.mainViewaddSubview:im];

Files

Basic file creation

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingPathComponent:@"file-name"];
[data writeToFile:file atomically:YES];

Read a file to NSData

[NSData dataWithContentsOfFile:filePath];

Delete a file

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL success = [fileManager removeItemAtPath:self.fileName error:&error];
if (!success) {
    NSLog(@"Could not delete file -:%@ ", [error localizedDescription]);
}

edit this page