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. 6.
  2. 8.
  3. 12.
  4. 13.

  5. 14.

    NS

  6. 15.
  7. 19.

    ?

  8. 21.
  9. 25.
  10. 27.
  11. 28.
  12. 40.

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

    asynchronously... dispatch_async(dispatch_get_main_queue(), ^{ // Return with result }); });
  13. 41.

    - (void)performActionWithObject:(id)object completionBlock:(void (^)(id result, NSError *error))block { // Do

    work asynchronously... if (block) { // Return with result block(result); } }
  14. 44.
  15. 46.

    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); }];
  16. 51.

    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); }];
  17. 54.
  18. 62.

    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); }];
  19. 67.

    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];
  20. 68.

    NSPredicate *bobPredicate = [NSPredicate predicateWithFormat:@"firstName = 'Bob'"]; NSPredicate *smithPredicate =

    [NSPredicate predicateWithFormat:@"lastName = %@", @"Smith"]; NSPredicate *thirtiesPredicate = [NSPredicate predicateWithFormat:@"age >= 30"];
  21. 69.

    NSExpression *expression = [NSExpression expressionWithFormat:@"4 + 5 - 2**3"]; id

    value = [expression expressionValueWithObject:nil context:nil]; // => 1
  22. 70.
  23. 78.
  24. 79.
  25. 81.
  26. 84.
  27. 85.
  28. 86.