Session 413 - Migrating to Modern Objective-C

download Session 413 - Migrating to Modern Objective-C

of 129

Transcript of Session 413 - Migrating to Modern Objective-C

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    1/129

    These are confidential sessionsplease refrain from streaming, blogging, or taking pictures

    Session 413

    Malcolm CrawfordDeveloper Publications

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    2/129

    Asleep since 2005

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    3/129

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    4/129

    Where did all the money go?

    Where can I get a haircut?

    Perhaps I should have usedthat maid service.

    What are all those little blackthings everyones tapping on?

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    5/129

    Where did all the money go?

    Where can I get a haircut?

    Perhaps I should have usedthat maid service.What happened to Objective-C?

    What are all those little blackthings everyones tapping on?

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    6/129

    Where did all the money go?

    Where can I get a haircut?

    Perhaps I should have usedthat maid service.What happened to Objective-C?

    What are all those little blackthings everyones tapping on?Why?

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    7/129

    Where did all the money go?

    Where can I get a haircut?

    Perhaps I should have usedthat maid service.What happened to Objective-C?

    What are all those little blackthings everyones tapping on?Why?

    How do I use it?

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    8/129

    Philosophy

    Headers

    Enums Accessor methods

    Memory management

    Properties

    Initializers and dealloc

    Protocols

    Collections and literals

    Blocks

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    9/129

    Historically: Objective-C was a thin layer atop C

    Today: Objective-C is still atop C

    But its not such a thin layer

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    10/129

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    11/129

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    12/129

    Simpler, safer through automation

    Object Oriented C

    Retain counting

    Properties

    Blocks

    ARC

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    13/129

    With new language features have come new patterns

    Conventions have evolved

    ImportantCocoa has strong conventions

    New features make code more compact, expressive, correct, efficient

    Remove unnecessary boilerplate

    Clarify API contracts

    Separate private from public

    Build conventions into the language So you dont have to think about them

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    14/129

    Alfred North Whitehead, 1861-1947

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    15/129

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    16/129

    #import #import

    //or @class NSString;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    17/129

    #import

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    18/129

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    19/129

    enum {

    NSOperationQueuePriorityVeryLow = -8,

    NSOperationQueuePriorityLow = -4,

    NSOperationQueuePriorityNormal = 0,

    NSOperationQueuePriorityHigh = 4,

    NSOperationQueuePriorityVeryHigh = 8

    };

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    20/129

    enum {

    NSOperationQueuePriorityVeryLow = -8,

    NSOperationQueuePriorityLow = -4,

    NSOperationQueuePriorityNormal = 0,

    NSOperationQueuePriorityHigh = 4,

    NSOperationQueuePriorityVeryHigh = 8

    };

    typedef NSInteger NSOperationQueuePriority;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    21/129

    typedef enum : NSInteger {

    NSOperationQueuePriorityVeryLow = -8,

    NSOperationQueuePriorityLow = -4,

    NSOperationQueuePriorityNormal = 0,

    NSOperationQueuePriorityHigh = 4,

    NSOperationQueuePriorityVeryHigh = 8

    } NSOperationQueuePriority;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    22/129

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    23/129

    - (void)myMethod {

    // ...

    [title release];

    title = [NSString stringWithFormat:@"Area: %1.2f", [self area]];

    [title retain];

    // ...

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    24/129

    - (void)myMethod {

    // ...

    [self setTitle:[NSString stringWithFormat:@"Area: %1.2f", [self area]]];

    // ...

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    25/129

    - (void)myMethod {

    // ...

    self.title = [NSString stringWithFormat:@"Area: %1.2f", self.area];

    // ...

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    26/129

    Always use accessor methods

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    27/129

    Always use accessor methods

    Except in initializer methods and dealloc

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    28/129

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    29/129

    NSMutableArray *array = [[NSMutableArray alloc] init];

    // Use the array

    [array release];

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    30/129

    NSMutableArray *array = [[[NSMutableArray alloc] init] autoelease];

    // Use the array

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    31/129

    NSMutableArray *array = [NSMutableArray array];

    // Use the array

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    32/129

    id heisenObject = [array objectAtIndex:n];

    [array removeObjectAtIndex:n];

    // ...

    [heisenObject doSomething];

    // ...

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    33/129

    id heisenObject = [[array objectAtIndex:n] retain];

    [array removeObjectAtIndex:n];

    // ...

    [heisenObject doSomething];

    [heisenObject release];

    // ...

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    34/129

    // Person.m

    - (void)takeLastNameFrom:(Person *)person

    {

    NSString *oldLastname = [self lastName];

    [self setLastName:[person lastName]];

    NSLog(@"Lastname changed from %@ to %@", oldLastname, [self lastName]);

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    35/129

    // Person.m

    - (void)takeLastNameFrom:(Person *)person

    {

    NSString *oldLastname = [[self lastName] retain];

    [self setLastName:[person lastName]];

    NSLog(@"Lastname changed from %@ to %@", oldLastname, [self lastName]);

    [oldLastName release];

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    36/129

    Adopting Automatic Reference Counting MarinaWednesday 11:30AM

    Adopting Automatic Reference Counting (Repeat) Nob HillFriday 11:30AM

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    37/129

    NSMutableArray *array = [[NSMutableArray alloc] init];

    // Use the array

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    38/129

    id heisenObject = [array objectAtIndex:n];

    [array removeObjectAtIndex:n];

    // ...

    [heisenObject doSomething];

    // ...

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    39/129

    // Person.m

    - (void)takeLastNameFrom:(Person *)person

    {

    NSString *oldLastname = [self lastName];

    [self setLastName:[person lastName]];

    NSLog(@"Lastname changed from %@ to %@", oldLastname, [self lastName]);

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    40/129

    CFUUIDRef cfUUID = CFUUIDCreate(NULL);

    NSString *noteUUID = (NSString *)

    CFUUIDCreateString(NULL, cfUUID);

    CFRelease(cfUUID);

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    41/129

    CFUUIDRef cfUUID = CFUUIDCreate(NULL);

    NSString *noteUUID = (__bridge_transfer NSString *)

    CFUUIDCreateString(NULL, cfUUID);

    CFRelease(cfUUID);

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    42/129

    NSString *noteUUID = [[[NSUUID alloc] init] UUIDString];

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    43/129

    Object 2

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    44/129

    Object 2

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    45/129

    Object 2

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    46/129

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    47/129

    @implementation Thing

    - (float)area {

    return M_PI * pow(radius, 2.0);

    }

    - (NSString *)title {

    return title;

    }

    - (void)setTitle:(NSString *)newTitle {

    if (title != newTitle) {

    [title release];

    title = [newTitle copy];

    }

    }

    // ....

    @interface Thing : NSObject {

    ! NSString *title;! float radius;! id delegate;}

    - (float)area;

    - (NSString *)title;

    - (void)setTitle:(NSString *)newTitle;

    - (float)radius;

    - (void)setRadius:(float)newValue;

    - (id)delegate;

    - (void)setDelegate:(id)newDelegate;

    @end

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    48/129

    @interface Thing : NSObject

    @property (copy) NSString *title;

    @property float radius;

    @property (weak) id delegate;

    @property(readonly, nonatomic) float area;

    @end

    @implementation Thing@implementation Thing

    - (float)area {

    return M_PI * pow(self.radius, 2.0);

    }

    @end

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    49/129

    @interface Thing : NSObject

    {

    ! NSString *title;! float radius;! id delegate;}

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    50/129

    @interface Thing : NSObject

    {

    ! NSString *title;! float radius;! id delegate;}

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    51/129

    @implementation Thing

    {

    ! NSString *title;! float radius;! id delegate;}

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    52/129

    @interface Thing : NSObject

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    53/129

    - (NSString *)title;

    - (void)setTitle:(NSString *)newTitle;

    - (float)radius;

    - (void)setRadius:(float)newValue;

    - (float)area;

    - (id)delegate;

    - (void)setDelegate:(id)newDelegate;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    54/129

    - (NSString *)title;

    - (void)setTitle:(NSString *)newTitle;

    - (float)radius;

    - (void)setRadius:(float)newValue;

    - (float)area;

    - (id)delegate;

    - (void)setDelegate:(id)newDelegate;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    55/129

    - (NSString *)title;

    - (void)setTitle:(NSString *)newTitle;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    56/129

    @property NSString *title;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    57/129

    @property (copy) NSString *title;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    58/129

    @property NSString *title;

    - (float)radius;

    - (void)setRadius:(float)newValue;

    - (float)area;

    - (id)delegate;

    - (void)setDelegate:(id)newDelegate;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    59/129

    @property NSString *title;

    - (float)radius;

    - (void)setRadius:(float)newValue;

    - (float)area;

    - (id)delegate;

    - (void)setDelegate:(id)newDelegate;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    60/129

    - (float)radius;

    - (void)setRadius:(float)newValue;

    - (float)area;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    61/129

    @property float radius;

    - (float)area;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    62/129

    @property float radius;

    @property (readonly) float area;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    63/129

    @property float radius;

    @property (readonly, nonatomic) float area;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    64/129

    @property NSString *title;

    @property float radius;

    @property (readonly, nonatomic) float area;

    - (id)delegate;

    - (void)setDelegate:(id)newDelegate;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    65/129

    @property NSString *title;

    @property float radius;

    @property (readonly, nonatomic) float area;

    - (id)delegate;

    - (void)setDelegate:(id)newDelegate;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    66/129

    - (id)delegate;

    - (void)setDelegate:(id)newDelegate;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    67/129

    @property (weak) id delegate;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    68/129

    @interface Thing : NSObject {

    ! NSString *title;! float radius;! id delegate;}

    - (float)area;- (NSString *)title;

    - (void)setTitle:(NSString *)newTitle;

    - (float)radius;

    - (void)setRadius:(float)newValue;

    - (id)delegate;

    - (void)setDelegate:(id)newDelegate;

    @end

    @implementation Thing

    - (float)area {

    return M_PI * pow(self.radius, 2.0);

    }

    - (NSString *)title {

    return title;}

    - (void)setTitle:(NSString *)newTitle {

    if (title != newTitle) {

    [title release];

    title = [newTitle copy];

    }

    }// ....

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    69/129

    @interface Thing : NSObject {

    ! NSString *title;! float radius;! id delegate;}

    - (float)area;- (NSString *)title;

    - (void)setTitle:(NSString *)newTitle;

    - (float)radius;

    - (void)setRadius:(float)newValue;

    - (id)delegate;

    - (void)setDelegate:(id)newDelegate;

    @end

    @implementation Thing

    - (float)area {

    return M_PI * pow(self.radius, 2.0);

    }

    - (NSString *)title {

    return title;}

    - (void)setTitle:(NSString *)newTitle {

    if (title != newTitle) {

    [title release];

    title = [newTitle copy];

    }

    }// ....

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    70/129

    - (NSString *)title {

    return title;

    }

    - (void)setTitle:(NSString *)newTitle

    {

    if (title != newTitle) {

    [title release];

    title = [newTitle copy];

    }

    }

    // ....

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    71/129

    @synthesize title;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    72/129

    @synthesize title = _title;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    73/129

    Autosynthesis

    @synthesize title = _title;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    74/129

    [ This slide intentionally left blank ]

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    75/129

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    76/129

    @interface Thing

    {

    BOOL privateTest;

    }

    // ...

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    77/129

    @interface Thing

    @property BOOL privateTest;

    // ...

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    78/129

    @implementation Thing

    {

    BOOL _privateTest;

    }

    // ...

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    79/129

    @interface Thing ()

    @property BOOL privateTest;

    @end

    // ...

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    80/129

    @interface Thing (PrivateMethods)

    - (void)doSomethingPrivate;

    - (void)doSomethingElsePrivate;

    @end

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    81/129

    @interface Thing ()

    - (void)doSomethingPrivate;

    - (void)doSomethingElsePrivate;

    @end

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    82/129

    [ This slide intentionally left blank ]

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    83/129

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    84/129

    @interface MyViewController : MySuperclass {

    IBOutlet ElementClass *uiElement;

    }

    @end

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    85/129

    @interface MyViewController : MySuperclass

    @property (weak) IBOutlet ElementClass *uiElement;

    @end

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    86/129

    @interface MyViewController ()

    @property (weak) IBOutlet ElementClass *uiElement;

    @end

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    87/129

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    88/129

    - (id)init

    {

    if (self = [super init]) {

    [self setTitle:@"default"];

    }

    return self;}

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    89/129

    - (id)init

    {

    if ((self = [super init])) {

    [self setTitle:@"default"];

    }

    return self;}

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    90/129

    - (id)init

    {

    self = [super init];

    if (self) {

    _title = @"default";

    }return self;

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    91/129

    - (void)dealloc

    {

    [self setTitle:nil];

    [super dealloc];

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    92/129

    - (void)dealloc

    {

    [_title release];

    [super dealloc];

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    93/129

    [ This slide intentionally left blank ]

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    94/129

    Formalize communication channels

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    95/129

    @ButlerProtocol

    - (void)makeTea;

    - (void)serveSandwiches;

    - (void)mowTheLawn;

    @end

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    96/129

    @ButlerProtocol

    - (void)makeTea;

    - (void)serveSandwiches;

    - (void)mowTheLawn;

    @end

    - (id )butler;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    97/129

    @interface NSObject (ButlerProtocol)

    - (void)makeTea;

    - (void)serveSandwiches;

    - (void)mowTheLawn;

    @end

    - (id)butler;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    98/129

    @protocol ButlerProtocol

    - (void)makeTea;

    - (void)serveSandwiches;

    @optional

    - (void)mowTheLawn;

    @end

    @property id butler;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    99/129

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    100/129

    NSNumber *aNumber = [NSNumber numberWithFloat:2.3];

    NSNumber *anotherNumber = [NSNumber numberWithFloat:x];

    NSArray *anArray = [NSArray arrayWithObjects:aThing, @"A String",

    [NSNumber numberWithFloat:3.14], nil];

    NSDictionary *aDictionary = [NSDictionary dictionaryWithObjectsAndKeys:

    value, @"Key",

    [NSNumber numberWithBOOL:YES], @"OtherKey",

    nil];

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    101/129

    NSNumber *aNumber = @2.3f;

    NSNumber *anotherNumber = @(x);

    NSArray *anArray = @[ aThing, @"A String", @3.14 ];

    NSDictionary *aDictionary = @{ @"Key" : value, @"OtherKey" : @YES };

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    102/129

    NSDictionary *distanceDict = [NSDictionary dictionaryWithObjectsAndKeys:

    [NSNumber numberWithDouble: 0.0], kCIAttributeMin,[NSNumber numberWithDouble: 1.0], kCIAttributeMax,[NSNumber numberWithDouble: 0.0], kCIAttributeSliderMin,[NSNumber numberWithDouble: 0.7], kCIAttributeSliderMax,

    [NSNumber numberWithDouble: 0.2], kCIAttributeDefault,[NSNumber numberWithDouble: 0.0], kCIAttributeIdentity,kCIAttributeTypeScalar, kCIAttributeType,nil];

    NSDictionary *slopeDict = [NSDictionary dictionaryWithObjectsAndKeys:

    [NSNumber numberWithDouble: -0.01], kCIAttributeSliderMin,[NSNumber numberWithDouble: 0.01], kCIAttributeSliderMax,

    [NSNumber numberWithDouble: 0.00], kCIAttributeDefault,[NSNumber numberWithDouble: 0.00], kCIAttributeIdentity,kCIAttributeTypeScalar, kCIAttributeType,nil];

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    103/129

    NSDictionary *distanceDict = @{

    kCIAttributeMin : @0.0,kCIAttributeMax : @1.0,kCIAttributeSliderMin : @0.0,kCIAttributeSliderMax : @0.7,

    kCIAttributeDefault : @0.2,kCIAttributeIdentity : @0.0,kCIAttributeType : kCIAttributeTypeScalar };

    NSDictionary *slopeDict = @{

    kCIAttributeSliderMin : @-0.01,kCIAttributeSliderMax : @0.01,

    kCIAttributeDefault : @0.00,kCIAttributeIdentity : @0.00,kCIAttributeType : kCIAttributeTypeScalar };

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    104/129

    id firstElement = [anArray objectAtIndex:0];

    [anArray replaceObjectAtIndex:0 withObject:newValue];

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    105/129

    id firstElement = anArray[0];

    anArray[0] = newValue;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    106/129

    id value = [aDictionary objectForKey:@"key"];

    [aDictionary setObject:newValue forKey:@"key"];

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    107/129

    id value = aDictionary[@"key"];

    aDictionary[@"key"] = newValue;

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    108/129

    NSArray *array = ;

    int i;

    for (i = 0; i < [array count]; i++) {

    id element = [array objectAtIndex:i];//

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    109/129

    NSArray *array = ;

    int i;

    for (i = 0; i < [array count]; i++) {

    id element = [array objectAtIndex:i];//

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    110/129

    NSArray *array = ;

    int i;

    for (i = 0; i < [array count]; i++) {

    id element = [array objectAtIndex:i];//

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    111/129

    NSArray *array = ;

    for (id element in array) {

    //

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    112/129

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    113/129

    NSArray *array = ...;

    NSArray *sortedArray;

    sortedArray = [array sortedArrayUsingFunction:MySort context:NULL];

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    114/129

    NSInteger MySort(id num1, id num2, void *context)

    {NSComparisonResult result;

    // Do comparison

    return result;

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    115/129

    NSArray *array = ...;

    BOOL reverse = ...;NSArray *sortedArray;

    sortedArray = [array sortedArrayUsingComparator:^(id num1, id num2) {

    NSComparisonResult result;

    // Do comparison

    return result;

    }];

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    116/129

    NSArray *array = ;

    for (id element in array) {

    //

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    117/129

    NSArray *array = ;

    [array enumerateObjectsUsingBlock:

    ^(id obj, NSUInteger idx, BOOL *stop) {

    // ...NSLog(@"Processing %@ at index %d, obj, idx);// ...

    }];

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    118/129

    NSDictionary *dictionary = ;

    for (NSString *key in dictionary) {

    id object = [dictionary objectForKey:key];

    // Do things with key and object.

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    119/129

    NSDictionary *dictionary = ;

    [dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {

    // Do things with key and object.

    }];

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    120/129

    - (void)registerForNotifications

    {

    NSNotificationCenter *center = ...

    [center addObserver:self

    selector:@selector(windowBecameKey:)

    name:NSWindowDidBecomeKeyNotification

    object:self.window];}

    // Different context

    // No queue information

    - (void)windowBecameKey:(NSNotification *)notification

    {

    // Get contextual information.

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    121/129

    - (void)registerForNotifications

    {

    NSNotificationCenter *center = ...

    [center addObserverForName:NSWindowDidBecomeKeyNotification

    object:self.window

    queue:[NSOperationQueue mainQueue]

    usingBlock:^(NSNotification *){// ...

    // Contextual information is already here

    // ...

    }];

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    122/129

    - (void)registerForNotifications

    {

    NSNotificationCenter *center = ...

    [center addObserverForName:NSWindowDidBecomeKeyNotification

    object:self.window

    queue:[NSOperationQueue mainQueue]

    usingBlock:^(NSNotification *) {// ...

    // Contextual information is already here

    // ...

    }];

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    123/129

    - (void)registerForNotifications

    {

    NSNotificationCenter *center = ...

    MyClass *__weak weakSelf = self;

    [center addObserverForName:NSWindowDidBecomeKeyNotification

    object:self.window

    queue:[NSOperationQueue mainQueue]usingBlock:^(NSNotification *) {

    // ...

    [weakSelf doSomething];

    // ...

    }];

    }

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    124/129

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    125/129

    Michael JurewitzDeveloper Tools and Performance [email protected]

    Apple Developer Forums

    http://devforums.apple.com

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    126/129

    Modern Objective-C PresidioWednesday 10:15AM

    Adopting Automatic Reference Counting MarinaWednesday 11:30AM

    Adopting Automatic Reference Counting (Repeat)

    Nob Hill

    Friday 11:30AM

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    127/129

    New document describes whatfeatures became available withwhich tools

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    128/129

    New features make code more compact, expressive, correct, efficient

    Adopt them

  • 7/28/2019 Session 413 - Migrating to Modern Objective-C

    129/129