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

Mattt Thompson

April 28, 2014
Tweet

More Decks by Mattt Thompson

Other Decks in Programming

Transcript

  1. NS

  2. ?

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

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

    work asynchronously... if (block) { // Return with result block(result); } }
  5. 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); }];
  6. 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); }];
  7. 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); }];
  8. 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];
  9. NSPredicate *bobPredicate = [NSPredicate predicateWithFormat:@"firstName = 'Bob'"]; NSPredicate *smithPredicate =

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

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