Skip to content

Instantly share code, notes, and snippets.

@nporteschaikin
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save nporteschaikin/6add09579987f4b6714b to your computer and use it in GitHub Desktop.

Select an option

Save nporteschaikin/6add09579987f4b6714b to your computer and use it in GitHub Desktop.
Objective-C notes

Obj-C/iOS dev notes

  • Use NS_DESIGNATED_INITIALIZER for designated initializers in header (>= iOS 8)

    // foo.h
    - (id)initWithFoo:(NSString *)foo NS_DESIGNATED_INITIALIZER;  
  • Use copy on NSString and `NSArray1 properties; (obviously) does not modify string at original pointer.

    @property (copy, nonatomic) NSString *foo;
    @property (copy, nonatomic) NSArray *bar;
  • Register table view reuse identifiers inside data sources.

    // TableViewCell.h
    extern NSString * const TableViewCellReuseIdentifier;
    
    // TabieViewCell.m
    NSString * const TableViewCellReuseIdentifier = @"TableViewCellReuseIdentifier";
    
    // TableViewDataSource.h
    - (void)registerReuseableCellsWithTableView:(UITableView *)tableView;
    
    // TableViewDataSource.m
    - (void)registerReuseableCellsWithTableView:(UITableView *)tableView {
      [tableView registerClass:[TableViewCell class]
        forCellReuseIdentifier:TableViewCellReuseIdentifier];
    }
    
    // TableViewController.m
    [self.dataSource registerReuseableCellsWithTableView:self.tableView];
  • Use instancetype as init type where possible (source).

  • To set UINavigationBar back button image:

    [[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"back.png"]];
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[[UIImage alloc] init]];
  • Use weak references to delegates in delegators; otherwise, the delegate will never de-allocate.

    @property (weak, nonatomic) id<FooDelegate> delegate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment