Objective-C Sins

Objective-C Sins

I have a confession to make. I've written some terrible Objective-C code. I guess you can say I've sinned. In this talk, I discuss common anti-patterns and how to avoid them.

This talk was originally presented to students of the iOS class at The Flatiron School. Without the actual talking to go with it, these slides are probably missing a lot of context. If you're confused about something, send me a message on twitter (@mb).

23ad4e437c3ece1b7a04696321036c30?s=128

Matthew Bischoff

October 29, 2013
Tweet

Transcript

  1. 1.
  2. 3.

    sin 1 |sin| noun an immoral act considered to be

    a transgression against divine law: a sin in the eyes of God | the human capacity for sin. ! • an act regarded as a serious or regrettable fault, offense, or omission: he committed the unforgivable sin of refusing to give interviews | humorous : with air like this, it's a sin not to go out.
  3. 4.

    sin 1 |sin| noun an immoral act considered to be

    a transgression against divine law: a sin in the eyes of God | the human capacity for sin. ! • an act regarded as a serious or regrettable fault, offense, or omission: he committed the unforgivable sin of refusing to give interviews | humorous : with air like this, it's a sin not to go out.
  4. 5.

    @mb

  5. 7.
  6. 8.
  7. 10.
  8. 11.
  9. 12.
  10. 14.
  11. 15.
  12. 16.

    Object retainCount? Actual retainCount [NSNumber numberWithInt:1] 1 2 @"Matt" 1

    1152921504606846975 [NSString stringWithString:@"Matt"] 1 1152921504606846975
  13. 18.

    Key-Value Observing has the worst API in all of Cocoa.

    It’s awkward, verbose, and confusing. Mattt Thompson
  14. 25.

    What’s the obvious thing to do, the easiest way to

    agree? Just do what the headers say. If it’s a property, treat it as such. If it’s not, don’t. ! And when in doubt, look it up. Brent Simmons
  15. 30.
  16. 45.
  17. 48.

    Every piece of knowledge must have a single, unambiguous, authoritative

    representation within a system. Andy Hunt and Dave Thomas
  18. 50.

    id

  19. 59.
  20. 64.

    #pragma mark - NSObject ! - (void)dealloc {} ! #pragma

    mark - UIViewController ! - (void)viewDidLoad {} ! #pragma mark - LCKDocumentsTableViewController ! - (void)setupRefreshControl {}
  21. 65.
  22. 70.

    Two-letter prefixes…are reserved by Apple for use in framework classes.

    ! Your own classes should use three letter prefixes.
  23. 72.
  24. 74.

    - (instancetype)initWithPerson:(LCKPerson *)person { self = [super initWithStyle:UITableViewStylePlain]; if (self)

    { _person = person; _dataStore = [NSMutableDictionary dictionary]; } return self; }
  25. 75.

    - (id)initWithStyle:(UITableViewStyle)style { return [self initWithPerson:nil]; } ! - (instancetype)initWithPerson:(LCKPerson

    *)person { self = [super initWithStyle:UITableViewStylePlain]; if (self) { _person = person; _dataStore = [NSMutableDictionary dictionary]; } return self; }
  26. 81.
  27. 82.

    Property Description Example accessibilityLabel Identifies the accessibility element, but does

    not include the type of the control or view “Share” accessibilityValue The value is a localized string that contains the current value of an element “35 %” accessibilityHint Description of the result of performing an action on the element. “Selects the message”
  28. 84.
  29. 90.
  30. 93.
  31. 101.
  32. 102.
  33. 105.

    Symbol Value Meaning NULL (void *)0 literal null value for

    C pointers nil (id)0 literal null value for Objective-C objects Nil (Class)0 literal null value for Objective-C classes NSNull [NSNull null] singleton object used to represent null
  34. 107.
  35. 109.
  36. 110.