Slide 1

Slide 1 text

What’s new(ish) in ObjectiveC Kuba Suder @psionides • jsuder • http://psionides.eu • Lunar Logic

Slide 2

Slide 2 text

Confession: I’m a Rubyist

Slide 3

Slide 3 text

Ruby is awesome class Post belongs_to :user end Post.where('created_at > ?', date).order(‘title’)

Slide 4

Slide 4 text

ObjC sometimes makes me cry NSManagedObjectContext * context = [[NSApp delegate] managedObjectContext]; NSManagedObjectModel * model = [[NSApp delegate] managedObjectModel]; NSDictionary * entities = [model entitiesByName]; NSEntityDescription * entity = [entities valueForKey:@"Post"]; NSPredicate * predicate; predicate = [NSPredicate predicateWithFormat:@"creationDate > %@", date]; NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:@"title"]; NSArray * sortDescriptors = [NSArray arrayWithObject: sort]; NSFetchRequest * fetch = [[NSFetchRequest alloc] init]; [fetch setEntity: entity]; [fetch setPredicate: predicate]; [fetch setSortDescriptors: sortDescriptors]; NSArray * results = [context executeFetchRequest:fetch error:nil]; [sort release]; [fetch release];

Slide 5

Slide 5 text

ObjC sometimes makes me cry NSManagedObjectContext * context = [[NSApp delegate] managedObjectContext]; NSManagedObjectModel * model = [[NSApp delegate] managedObjectModel]; NSDictionary * entities = [model entitiesByName]; NSEntityDescription * entity = [entities valueForKey:@"Post"]; NSPredicate * predicate; predicate = [NSPredicate predicateWithFormat:@"creationDate > %@", date]; NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:@"title"]; NSArray * sortDescriptors = [NSArray arrayWithObject: sort]; NSFetchRequest * fetch = [[NSFetchRequest alloc] init]; [fetch setEntity: entity]; [fetch setPredicate: predicate]; [fetch setSortDescriptors: sortDescriptors]; NSArray * results = [context executeFetchRequest:fetch error:nil]; [sort release]; [fetch release];

Slide 6

Slide 6 text

LLVM to the rescue!

Slide 7

Slide 7 text

Instance variables for @property Requires: modern runtime(*)

Slide 8

Slide 8 text

(*) WTF is modern runtime?

Slide 9

Slide 9 text

(*) WTF is modern runtime? • “non-fragile variables”

Slide 10

Slide 10 text

(*) WTF is modern runtime? • “non-fragile variables” • iOS

Slide 11

Slide 11 text

(*) WTF is modern runtime? • “non-fragile variables” • iOS • OSX 10.5+ (64-bit)

Slide 12

Slide 12 text

Instance variables for @property Before: // MyKlass.h @interface MyKlass { NSMutableArray *list; } @property NSMutableArray *list; @end

Slide 13

Slide 13 text

Instance variables for @property After: // MyKlass.h @interface MyKlass @property NSMutableArray *list; @end

Slide 14

Slide 14 text

Blocks (closures) OSX 10.6 / iOS 4.0

Slide 15

Slide 15 text

Blocks // calling [list enumerateObjectsUsingBlock: ^(id obj, NSUInteger i, BOOL *stop) { NSLog(@”#%d = %@”, i, obj); }]; // declaring - (void) connectTo: (NSString *) url callback: (void (^)(NSString *)) callback { NSString *response = [connector requestTo: url]; callback(response); }

Slide 16

Slide 16 text

Private variables Xcode 4.2 + LLVM + modern runtime

Slide 17

Slide 17 text

Private variables Before - variables in a header file: // MyKlass.h @interface MyKlass { NSMutableArray *list; } @end (@protected by default)

Slide 18

Slide 18 text

Private variables Xcode 4 + LLVM - class extension: // MyKlass.m @interface MyKlass () { NSMutableArray *list; } @end

Slide 19

Slide 19 text

Private variables Xcode 4.2 + LLVM: // MyKlass.m @implementation MyKlass { NSMutableArray *list; } @end

Slide 20

Slide 20 text

Automatic Reference Counting Xcode 4.2 + OSX 10.6 64-bit / iOS 4.0

Slide 21

Slide 21 text

ARC Before: - (NSArray *) views { NSMutableArray *arr = [[NSMutableArray alloc] init]; UIView *view = [[UIView alloc] initWithFrame: frame]; [arr addObject: view]; [view release]; return [arr autorelease]; }

Slide 22

Slide 22 text

ARC After: - (NSArray *) views { NSMutableArray *arr = [[NSMutableArray alloc] init]; UIView *view = [[UIView alloc] initWithFrame: frame]; [arr addObject: view]; // view magically released return arr; // arr magically autoreleased }

Slide 23

Slide 23 text

ARC Before: - (void) dealloc { [window release]; [panel release]; [notifier unsubscribe: self]; [super dealloc]; }

Slide 24

Slide 24 text

ARC After: - (void) dealloc { // window and panel magically released [notifier unsubscribe: self]; // [super dealloc] is actually forbidden }

Slide 25

Slide 25 text

ARC Before: NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // ... [pool drain];

Slide 26

Slide 26 text

ARC After: @autoreleasepool { // ... }

Slide 27

Slide 27 text

ARC Before: @property (retain) NSWindow *window; After: @property (strong) NSWindow *window;

Slide 28

Slide 28 text

ARC Before: @property (assign) id delegate; After: @property (unsafe_unretained) id delegate; Or better (OSX 10.7 / iOS 5) - set to nil when object is released: @property (weak) id delegate;

Slide 29

Slide 29 text

ARC • calling dealloc, retain, release, autorelease or retainCount is verboten • you have to follow the rules • using CFCreateThingie() requires extra keywords/wrapping • can be enabled/disabled per file

Slide 30

Slide 30 text

GC: deprecated in OSX 10.8

Slide 31

Slide 31 text

Forward method declarations Xcode 4.3

Slide 32

Slide 32 text

Forward method declarations Problem: @implementation MyKlass - (void) bar { [self foo]; // <-- Xcode: wtf is this } - (void) foo { } @end

Slide 33

Slide 33 text

Forward method declarations Before: @interface MyKlass () - (void) foo; @end @implementation MyKlass - (void) bar { [self foo]; } // ...

Slide 34

Slide 34 text

Forward method declarations After: // yay, no @interface! @implementation MyKlass - (void) bar { [self foo]; } - (void) foo { } @end

Slide 35

Slide 35 text

Automatic @synthesize Xcode 4.4 + modern runtime

Slide 36

Slide 36 text

Automatic @synthesize Before: @interface MyKlass @property NSString *name; @end @implementation MyKlass @synthesize name; - (void) printName { NSLog(@”%@ %@”, name, self.name); } @end

Slide 37

Slide 37 text

Automatic @synthesize After: @interface MyKlass @property NSString *name; @end @implementation MyKlass - (void) printName { NSLog(@”%@ %@”, _name, self.name); } @end

Slide 38

Slide 38 text

ObjC literals Xcode 4.4

Slide 39

Slide 39 text

Literals Before: [NSNumber numberWithBool: YES] [NSNumber numberWithInt: 1] [NSNumber numberWithFloat: x] After: @1, @2.5, @YES, @(x)

Slide 40

Slide 40 text

Literals Before: [NSArray arrayWithObjects: @”twitter”, @”facebook”, @”github”, nil] [NSDictionary dictionaryWithObjectsAndKeys: @”Kuba”, @”firstName”, @”Suder”, @”lastName”, [NSNumber numberWithInt: 30], @”age”, nil]

Slide 41

Slide 41 text

Literals Before: [NSArray arrayWithObjects: @”twitter”, @”facebook”, @”github”, nil] [NSDictionary dictionaryWithObjectsAndKeys: @”Kuba”, @”firstName”, @”Suder”, @”lastName”, [NSNumber numberWithInt: 30], @”age”, nil]

Slide 42

Slide 42 text

Literals After: @[@”twitter”, @”facebook”, @”github”] @{ @”firstName”: @”Kuba”, @”lastName”: @”Suder”, @”age”: @30 }

Slide 43

Slide 43 text

Array & dictionary subscripting Xcode 4.4 + OSX 10.6 modern runtime / iOS 5

Slide 44

Slide 44 text

Subscripting Before: [array objectAtIndex: 2] [array replaceObjectAtIndex: 0 withObject: @”first”] After: array[2] array[0] = @”first”

Slide 45

Slide 45 text

Subscripting Before: [dictionary objectForKey: @”id”] [dictionary setObject: @”jsuder” forKey: @”author”] After: dictionary[@”id”] dictionary[@”author”] = @”jsuder”

Slide 46

Slide 46 text

Subscripting Make your own array! - (id) objectAtIndexedSubscript: (NSUInteger) i { return list[i]; } NSLog(@”record = %@”, collection[5]);

Slide 47

Slide 47 text

Subscripting Make your own dictionary! - (id) objectAtKeyedSubscript: (id) key { for (Node *node in tree) { if ([node.name isEqual: key]) return node; } return nil; } NSLog(@”node = %@”, xmlDocument[@”date”]);

Slide 48

Slide 48 text

Works on iOS 4.3 with libarclite http://petersteinberger.com/blog/2012/using-subscripting-with-Xcode-4_4-and-iOS-4_3/

Slide 49

Slide 49 text

Extras from Foundation

Slide 50

Slide 50 text

JSON parsing OSX 10.7 / iOS 5.0

Slide 51

Slide 51 text

JSON parsing Before: TouchJSON / YAJL / JSON Framework / JSONKit / ... After: [NSJSONSerialization JSONObjectWithData: data options: 0 error: &err];

Slide 52

Slide 52 text

Regular expressions OSX 10.7 / iOS 4.0

Slide 53

Slide 53 text

Regular expressions Before: RegexKit / RegexKitLite After: NSString *pattern = @”^\\w+@\\w+(\\.\\w+)+$”; NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern: pattern options: 0 error: &err]; [re matchesInString: email options: 0 range: NSMakeRange(0,20)];

Slide 54

Slide 54 text

Sources • https://developer.apple.com • http://stackoverflow.com • https://developer.apple.com/library/ios/#releasenotes/ ObjectiveC/ObjCAvailabilityIndex/ • Google :)