Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Secrets of Objective-C

Secrets of Objective-C

Objective-C is a fascinating language, bridging the object-oriented ideas of Smalltalk with the procedural underpinnings of C. This talk will explore how this curious intersection of programming paradigms influences the design and structure of modern OS X and iOS applications, drawing examples from some of the more obscure corners of Apple’s system frameworks.

Presented at the SendHub Tech Talk on 6/28/2014.
Video available here: https://www.youtube.com/watch?v=2KK_ay_mpGQ&list=PLC8Wr4QyrgQQpL7LkDd_GyvWFbXHM3rzd

D29bb4d2d2f2ba2c2fb5a329e1e4651f?s=128

Mattt Thompson

April 28, 2014
Tweet

Transcript

  1. SECRETS OF OBJECTIVE-C

  2. __bridge BETWEEN WORLDS

  3. SMALLTALK & C

  4. SMALLTALK 1972 ALAN KAY, DAN INGALLS, & ADELE GOLDBERG OBJECT-ORIENTED,

    LISP INSPIRED, MESSAGE PASSING
  5. [object message:parameter]

  6. None
  7. C 1972 DENNIS RITCHIE PROCEDURAL, ALGOL INSPIRED

  8. UNIX

  9. CHOCOLATE + PEANUT BUTTER

  10. PHILOSOPHER + A BAZOOKA

  11. OBJECTIVE-C 1983 BRAD COX & TOM LOVE STATIC, DYNAMIC, WEAK

  12. None
  13. NS

  14. None
  15. STATIC, DYNAMIC, WEAK

  16. id object = @"foo";

  17. [object performSelector:@selector(bar)];

  18. ?

  19. nil, Nil, NULL

  20. None
  21. OBJECTIVE-C IS GETTING BETTER FASTER THAN ALMOST ANY OTHER LANGUAGE

  22. ARC AUTOMATIC REFERENCE COUNTING

  23. TIOBE "PROGRAMMING LANGUAGE OF THE YEAR: 2012"

  24. None
  25. OBJECTIVE-C: THE GOOD PARTS

  26. None
  27. None
  28. KVC KEY-VALUE CODING

  29. [person valueForKey:@"name"]

  30. [person valueForKeyPath:@"spouse.name"]

  31. [people valueForKeyPath:@"name"]

  32. [people valueForKeyPath:@"@avg.salary"]

  33. ▸ @count ▸ @sum ▸ @avg ▸ @max ▸ @min

  34. objc/runtime.h

  35. METHOD SWIZZLING ASSOCIATED OBJECTS DYNAMIC CLASS DEFINITION

  36. @compatibility_alias

  37. FRAMEWORKS

  38. GCD GRAND CENTRAL DISPATCH

  39. dispatch_queue_t queue = dispatch_queue_create("queue", NULL); dispatch_async(queue, ^{ // Do work

    asynchronously... dispatch_async(dispatch_get_main_queue(), ^{ // Return with result }); });
  40. - (void)performActionWithObject:(id)object completionBlock:(void (^)(id result, NSError *error))block { // Do

    work asynchronously... if (block) { // Return with result block(result); } }
  41. FOUNDATION THE STANDARD LIBRARY LANGUAGES WOULD kill FOR

  42. LINGUISTICS APIS

  43. NSSTRING

  44. NSLINGUISTICTAGGER ▸ Token ▸ Lexical Class ▸ Name ▸ Lemma

    ▸ Language & Script
  45. NSString *question = @"What is the weather in San Francisco?";

    NSLinguisticTaggerOptions options = NSLinguisticTaggerOmitWhitespace | NSLinguisticTaggerOmitPunctuation | NSLinguisticTaggerJoinNames; NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes: [NSLinguisticTagger availableTagSchemesForLanguage:@"en"] options:options]; tagger.string = question; [tagger enumerateTagsInRange:NSMakeRange(0, [question length]) scheme:NSLinguisticTagSchemeNameTypeOrLexicalClass options:options usingBlock: ^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) { NSString *token = [question substringWithRange:tokenRange]; NSLog(@"%@: %@", token, tag); }];
  46. WHAT: PRONOUN IS: VERB THE: DETERMINER WEATHER: NOUN IN: PREPOSITION

    SAN FRANCISCO: PLACENAME
  47. LANGUAGE: ENGLISH

  48. NSDATADETECTOR

  49. ▸ Date ▸ Address ▸ Link ▸ Phone Number ▸

    Transit Information
  50. NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeAddress | NSTextCheckingTypePhoneNumber error:nil]; NSString *string

    = @"123 Main St. / (555) 555-5555"; [detector enumerateMatchesInString:string options:kNilOptions range:NSMakeRange(0, [string length]) usingBlock: ^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { NSLog(@"Match: %@", result); }];
  51. CFSTRINGTRANSFORM API FOR ICU TRANSFORMS

  52. Énġlišh långuãge lẳcks iñterêßţing diaçrïtičş

  53. None
  54. {PIG FACE}

  55. HELLO! ͜Μʹͪ͸! สวัสดี! م ر ح ب ا ! 䓟޷!

  56. Hello! kon'nichiha! s̄wạs̄dī! mrḥbạ! nín hǎo!

  57. Hello! kon'nichiha! swasdi! mrhba! nin hao!

  58. (hello, kon'nichiha, swasdi, mrhba, nin, hao)

  59. DATE & TIME PROGRAMMING

  60. NSCALENDAR GREGORIAN, BUDDHIST, CHINESE, HEBREW, ISLAMIC, ISLAMIC CIVIL, JAPANESE, REPUBLIC

    OF CHINA, PERSIAN, INDIAN
  61. NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *dateComponents = [[NSDateComponents alloc]

    init]; dateComponents.weekday = 0; dateComponents.hour = 2; [calendar enumerateDatesStartingAfterDate:[NSDate date] matchingComponents:dateComponents options:NSCalendarMatchNextTime usingBlock: ^(NSDate *date, BOOL exactMatch, BOOL *stop) { NSLog(@"%@", date); }];
  62. URL LOADING SYSTEM

  63. NSURLREQUEST, NSURLRESPONSE, NSURLCONNECTION, NSURLSESSION, NSURLCACHE, NSHTTPCOOKIE, NSURLPROTOCOL, NSURLAUTHENTICATIONCHALLENGE

  64. NSURL & NSURLCOMPONENTS RFC1808, RFC 2396, RFC3986

  65. SORT DESCRIPTORS, PREDICATES, & EXPRESSIONS

  66. NSSortDescriptor *firstNameSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES selector:@selector(localizedStandardCompare:)]; NSSortDescriptor *lastNameSortDescriptor =

    [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES selector:@selector(localizedStandardCompare:)]; NSSortDescriptor *ageSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
  67. NSPredicate *bobPredicate = [NSPredicate predicateWithFormat:@"firstName = 'Bob'"]; NSPredicate *smithPredicate =

    [NSPredicate predicateWithFormat:@"lastName = %@", @"Smith"]; NSPredicate *thirtiesPredicate = [NSPredicate predicateWithFormat:@"age >= 30"];
  68. NSExpression *expression = [NSExpression expressionWithFormat:@"4 + 5 - 2**3"]; id

    value = [expression expressionValueWithObject:nil context:nil]; // => 1
  69. NSERROR

  70. @try{} @catch(NSException){} @finally{}

  71. error:(NSError * __autoreleasing *)error

  72. LOCALIZATION

  73. INTERNATIONALIZATION

  74. ACCESSIBILITY

  75. ε-(´ɾʆ)

  76. Dozens OF SYSTEM FRAMEWORKS

  77. None
  78. COMMUNITY

  79. COCOAPODS DEPENDENCY MANAGER FOR OBJECTIVE-C cocoapods.org

  80. 4638 PODS

  81. ALCATRAZ XCODE PLUGIN MANAGER alcatraz.io

  82. 116 PLUGINS

  83. NS—

  84. @MATTT

  85. Thanks!