Modern Objective-C
    Giuseppe Arici


     Pragma Night @ Talent Garden
It’s All About ...

Syntactic Sugar


                       Pragma Night
Unordered Method
  Declarations

               Pragma Night
Public & Private Method Ordering

  @interface SongPlayer : NSObject
  - (void)playSong:(Song *)song;

  @end

  @implementation SongPlayer
  - (void)playSong:(Song *)song {
      NSError *error;
      [self startAudio:&error];
      /* ... */
  }

 - (void)startAudio:(NSError **)error { /* ... */ }
 Warning:
 @end
 instance method '-startAudio:' not found (return type defaults to 'id')

                                                                   Pragma Night
Wrong Workaround
In the public interface

 @interface SongPlayer : NSObject
 - (void)playSong:(Song *)song;
 - (void)startAudio:(NSError **)error;
 @end

 @implementation SongPlayer
 - (void)playSong:(Song *)song {
     NSError *error;
     [self startAudio:&error];
     /* ... */
 }

 - (void)startAudio:(NSError **)error { /* ... */ }
 @end



                                                      Pragma Night
Okay Workaround 1
In a class extension

 @interface SongPlayer ()

 - (void)startAudio:(NSError **)error;
 @end

 @implementation SongPlayer
 - (void)playSong:(Song *)song {
     NSError *error;
     [self startAudio:&error];
     /* ... */
 }

 - (void)startAudio:(NSError **)error { /* ... */ }
 @end



                                                      Pragma Night
Okay Workaround 2
Reorder methods

 @interface SongPlayer : NSObject
 - (void)playSong:(Song *)song;

 @end

 @implementation SongPlayer
 - (void)startAudio:(NSError **)error { /* ... */ }
 - (void)playSong:(Song *)song {
     NSError *error;
     [self startAudio:&error];
     /* ... */
 }

 @end



                                                      Pragma Night
Best Solution
Parse the @implementation declarations then bodies

 @interface SongPlayer : NSObject
 - (void)playSong:(Song *)song;

 @end

 @implementation SongPlayer
 - (void)playSong:(Song *)song {
     NSError *error;
     [self startAudio:&error];                  Xcode 4.4+
     /* ... */
 }

 - (void)startAudio:(NSError **)error { /* ... */ }
 @end



                                                      Pragma Night
Enum with Fixed
Underlying Type

                  Pragma Night
Enum with Indeterminate Type
Before OS X v10.5 and iOS
 typedef enum {
     NSNumberFormatterNoStyle,
     NSNumberFormatterDecimalStyle,
     NSNumberFormatterCurrencyStyle,
     NSNumberFormatterPercentStyle,
     NSNumberFormatterScientificStyle,
     NSNumberFormatterSpellOutStyle
 } NSNumberFormatterStyle;

 //typedef int NSNumberFormatterStyle;




                                         Pragma Night
Enum with Explicit Type
After OS X v10.5 and iOS
 enum {
     NSNumberFormatterNoStyle,
     NSNumberFormatterDecimalStyle,
     NSNumberFormatterCurrencyStyle,
     NSNumberFormatterPercentStyle,
     NSNumberFormatterScientificStyle,
     NSNumberFormatterSpellOutStyle
 };

 typedef NSUInteger NSNumberFormatterStyle;


 •   Pro: 32-bit and 64-bit portability

 •   Con: no formal relationship between type and enum constants

                                                             Pragma Night
Enum with Fixed Underlying Type
LLVM 4.2+ Compiler
 typedef enum NSNumberFormatterStyle : NSUInteger {
     NSNumberFormatterNoStyle,
     NSNumberFormatterDecimalStyle,
     NSNumberFormatterCurrencyStyle,
     NSNumberFormatterPercentStyle,
     NSNumberFormatterScientificStyle,
     NSNumberFormatterSpellOutStyle
 } NSNumberFormatterStyle;
                                                Xcode 4.4+

 •   Stronger type checking

 •   Better code completion

                                                      Pragma Night
Enum with Fixed Underlying Type
NS_ENUM macro
 typedef NS_ENUM(NSUInteger, NSNumberFormatterStyle) {
     NSNumberFormatterNoStyle,
     NSNumberFormatterDecimalStyle,
     NSNumberFormatterCurrencyStyle,
     NSNumberFormatterPercentStyle,
     NSNumberFormatterScientificStyle,
     NSNumberFormatterSpellOutStyle
 };
                                                Xcode 4.4+

 •   Foundation declares like this



                                                         Pragma Night
Enum with Fixed Underlying Type
Stronger type checking (-Wenum-conversion)


 NSNumberFormatterStyle style = NSNumberFormatterRoundUp; // 3




warning:
implicit conversion from enumeration type 'enum
NSNumberFormatterRoundingMode' to different enumeration type
'NSNumberFormatterStyle' (aka 'enum NSNumberFormatterStyle')

                                                         Pragma Night
Enum with Fixed Underlying Type
Handling all enum values (-Wswitch)
 - (void) printStyle:(NSNumberFormatterStyle) style{
     switch (style) {
         case NSNumberFormatterNoStyle:
             break;
         case NSNumberFormatterSpellOutStyle:
             break;
     }
 }


warning:
4 enumeration values not handled in switch:
'NSNumberFormatterDecimalStyle',
'NSNumberFormatterCurrencyStyle',
'NSNumberFormatterPercentStyle'...
                                                       Pragma Night
@Synthesize by
   Default

                 Pragma Night
Properties Simplify Classes
@interface instance variables

 @interface Person : NSObject {
     NSString *_name;
 }
 @property(strong) NSString *name;
 @end

 @implementation Person




 @synthesize name = _name;

 @end



                                     Pragma Night
Properties Simplify Classes
@implementation instance variables

 @interface Person : NSObject


 @property(strong) NSString *name;
 @end

 @implementation Person {
     NSString *_name;
 }


 @synthesize name = _name;

 @end



                                     Pragma Night
Properties Simplify Classes
Synthesized instance variables

 @interface Person : NSObject


 @property(strong) NSString *name;
 @end

 @implementation Person




 @synthesize name = _name;

 @end



                                     Pragma Night
@Synthesize by Default
LLVM 4.2+ Compiler

 @interface Person : NSObject


 @property(strong) NSString *name;
 @end

 @implementation Person
                                     Xcode 4.4+


 @end



                                           Pragma Night
Instance Variable Name !?
Instance variables now prefixed with “_”

 @interface Person : NSObject


 @property(strong) NSString *name;
 @end

 @implementation Person
 - (NSString *)description {
     return _name;                              Xcode 4.4+
 }
 /* as if you'd written: @synthesize name = _name; */


 @end



                                                        Pragma Night
Backward Compatibility !?
Be careful, when in doubt be fully explicit

 @interface Person : NSObject


 @property(strong) NSString *name;
 @end

 @implementation Person




 @synthesize name;
 /* as if you'd written: @synthesize name = name; */
 @end



                                                       Pragma Night
To        @Synthesize by Default
• Warning: @Synthesize by Default will not
  synthesize a property declared in a protocol
• If you use custom instance variable naming
  convention, enable this warning
  ( -Wobjc-missing-property-synthesis )




                                                 Pragma Night
Core Data NSManagedObject
Opts out of synthesize by default
 /* NSManagedObject.h */

 NS_REQUIRES_PROPERTY_DEFINITIONS
 @interface NSManagedObject : NSObject {



 •   NSManagedObject synthesizes properties

 •   Continue to use @property to declare typed accessors

 •   Continue to use @dynamic to inhibit warnings




                                                            Pragma Night
NSNumbers Literals


                 Pragma Night
NSNumber Creation

NSNumber *value;

value = [NSNumber numberWithChar:'X'];

value = [NSNumber numberWithInt:42];

value = [NSNumber numberWithUnsignedLong:42ul];

value = [NSNumber numberWithLongLong:42ll];

value = [NSNumber numberWithFloat:0.42f];

value = [NSNumber numberWithDouble:0.42];

value = [NSNumber numberWithBool:YES];




                                                  Pragma Night
NSNumber Creation

NSNumber *value;

value = @'X';

value = @42;

value = @42ul;

value = @42ll;

value = @0.42f;
                        Xcode 4.4+
value = @0.42;

value = @YES;




                              Pragma Night
Backward Compatibility !?
#define YES      (BOOL)1     // Before iOS 6, OSX 10.8


#define YES      ((BOOL)1) // After iOS 6, OSX 10.8


Workarounds
@(YES)                       // Use parentheses around BOOL Macros


#if __IPHONE_OS_VERSION_MAX_ALLOWED < 60000
#if __has_feature(objc_bool)
#undef YES
#undef NO                       // Redefine   BOOL Macros
#define YES __objc_yes
#define NO __objc_no
#endif
#endif



                                                             Pragma Night
Boxed Expression
    Literals

                   Pragma Night
Boxed Expression Literals

NSNumber *orientation =
    [NSNumber numberWithInt:UIDeviceOrientationPortrait];

NSNumber *piOverSixteen =
    [NSNumber numberWithDouble:( M_PI / 16 )];

NSNumber *parityDigit =
    [NSNumber numberWithChar:"EO"[i % 2]];

NSString *path =
    [NSString stringWithUTF8String:getenv("PATH")];

NSNumber *usesCompass =
    [NSNumber numberWithBool:
        [CLLocationManager headingAvailable]];




                                                        Pragma Night
Boxed Expression Literals

NSNumber *orientation =
    @( UIDeviceOrientationPortrait );

NSNumber *piOverSixteen =
    @( M_PI / 16 );

NSNumber *parityDigit =
    @( "OE"[i % 2] );

NSString *path =
    @( getenv("PATH") );
                                                 Xcode 4.4+
NSNumber *usesCompass =
    @( [CLLocationManager headingAvailable] );




                                                       Pragma Night
Array Literals


                 Pragma Night
Array Creation
More choices, and more chances for errors


 NSArray *array;

 array = [NSArray array];

 array = [NSArray arrayWithObject:a];

 array = [NSArray arrayWithObjects:a, b, c, nil];

 id objects[] = { a, b, c };
 NSUInteger count = sizeof(objects) / sizeof(id);
 array = [NSArray arrayWithObjects:objects count:count];




                                                           Pragma Night
Nil Termination
Inconsistent behavior
 // if you write:
 id a = nil, b = @"hello", c = @42;
 NSArray *array
     = [NSArray arrayWithObjects:a, b, c, nil];

Array will be empty

 // if you write:
 id objects[] = { nil, @"hello", @42 };
 NSUInteger count = sizeof(objects)/ sizeof(id);
 NSArray *array
     = [NSArray arrayWithObjects:objects count:count];


Exception: attempt to insert nil object from objects[0]

                                                          Pragma Night
Array Creation

NSArray *array;

array = [NSArray array];

array = [NSArray arrayWithObject:a];

array = [NSArray arrayWithObjects:a, b, c, nil];

id objects[] = { a, b, c };
NSUInteger count = sizeof(objects) / sizeof(id);
array = [NSArray arrayWithObjects:objects count:count];




                                                          Pragma Night
Array Creation

NSArray *array;

array = @[];

array = @[ a ];

array = @[ a, b, c ];
                                   Xcode 4.4+
array = @[ a, b, c ];




                                         Pragma Night
How Array Literals Work
// when you write this:

NSArray *array = @[ a, b, c ];




// compiler generates:

id objects[] = { a, b, c };
NSUInteger count = sizeof(objects)/ sizeof(id);
NSArray *array
    = [NSArray arrayWithObjects:objects count:count];




                                                        Pragma Night
Dictionary Literals


                      Pragma Night
Dictionary Creation
More choices, and more chances for errors

 NSDictionary *dict;

 dict = [NSDictionary dictionary];

 dict = [NSDictionary dictionaryWithObject:o1 forKey:k1];

 dict = [NSDictionary dictionaryWithObjectsAndKeys:
     o1, k1, o2, k2, o3, k3, nil];

 id objects[] = { o1, o2, o3 };
 id keys[] = { k1, k2, k3 };
 NSUInteger count = sizeof(objects) / sizeof(id);
 dict = [NSDictionary dictionaryWithObjects:objects
                                    forKeys:keys
                                      count:count];


                                                            Pragma Night
Dictionary Creation

NSDictionary *dict;

dict = [NSDictionary dictionary];

dict = [NSDictionary dictionaryWithObject:o1 forKey:k1];

dict = [NSDictionary dictionaryWithObjectsAndKeys:
    o1, k1, o2, k2, o3, k3, nil];

id objects[] = { o1, o2, o3 };
id keys[] = { k1, k2, k3 };
NSUInteger count = sizeof(objects) / sizeof(id);
dict = [NSDictionary dictionaryWithObjects:objects
                                   forKeys:keys
                                     count:count];




                                                           Pragma Night
Dictionary Creation

NSDictionary *dict;

dict = @{};

dict = @{ k1 : o1 }; // key before object

dict = @{ k1 : o1, k2 : o2, k3 : o3 };



                                            Xcode 4.4+
dict = @{ k1 : o1, k2 : o2, k3 : o3 };




                                                  Pragma Night
How Dictionary Literals Work
// when you write this:

NSDictionary *dict = @{ k1 : o1, k2 : o2, k3 : o3 };




// compiler generates:

id objects[] = { o1, o2, o3 };
id keys[] = { k1, k2, k3 };
NSUInteger count = sizeof(objects) / sizeof(id);
NSDictionary *dict =
    [NSDictionary dictionaryWithObjects:objects
                                 orKeys:keys
                                  count:count];




                                                       Pragma Night
Container Literals Restriction
All containers are immutable, mutable use: -mutableCopy
   NSMutableArray *mutablePragmers =
   [@[ @"Fra", @"Giu", @"Mat", @"Max", @"Ste" ] mutableCopy];



For constant containers, simply implement +initialize
 static NSArray *thePragmers;

 + (void)initialize {
     if (self == [MyClass class]) {
         thePragmers =
             @[ @"Fra", @"Giu", @"Mat", @"Max", @"Ste" ];
     }
 }


                                                            Pragma Night
Object Subscripting


                  Pragma Night
Array Subscripting
New syntax to access object at index: nsarray[index]

 @implementation SongList
 {
     NSMutableArray *_songs;
 }

 - (Song *)replaceSong:(Song *)newSong
                atIndex:(NSUInteger)idx
 {
     Song *oldSong = [_songs objectAtIndex:idx];
      [_songs replaceObjectAtIndex:idx withObject:newSong];
     return oldSong;
 }
 @end



                                                          Pragma Night
Array Subscripting
New syntax to access object at index: nsarray[index]

 @implementation SongList
 {
     NSMutableArray *_songs;
 }

 - (Song *)replaceSong:(Song *)newSong
               atIndex:(NSUInteger)idx
 {                                       Xcode 4.4+
     Song *oldSong = _songs[idx];
     _songs[idx] = newSong;
     return oldSong;
 }
 @end



                                                Pragma Night
Dictionary Subscripting
New syntax to access object by key: nsdictionary[key]

 @implementation Database
 {
     NSMutableDictionary *_storage;
 }

 - (id)replaceObject:(id)newObject
               forKey:(id <NSCopying>)key
 {
     id oldObject = [_storage objectForKey:key];
      [_storage setObject:newObject forKey:key];
     return oldObject;
 }
 @end



                                                   Pragma Night
Dictionary Subscripting
New syntax to access object by key: nsdictionary[key]

 @implementation Database
 {
     NSMutableDictionary *_storage;
 }

 - (id)replaceObject:(id)newObject
              forKey:(id <NSCopying>)key
 {                                         Xcode 4.4+
     id oldObject = _storage[key];
     _storage[key] = newObject;
     return oldObject;
 }
 @end



                                                 Pragma Night
How Subscripting Works
                                                              iOS 6
Array Style: Indexed subscripting methods                    OSX 10.8

 - (elementType)objectAtIndexedSubscript:(indexType)idx

 - (void)setObject:(elementType)obj
 atIndexedSubscript:(indexType)idx;

elementType must be an object pointer, indexType must be integral

                                                              iOS 6
Dictionary Style: Keyed subscripting methods                 OSX 10.8


 - (elementType)objectForKeyedSubscript:(keyType)key;

 - (void)setObject:(elementType)obj
 forKeyedSubscript:(keyType)key;

elementType and keyType must be an object pointer
                                                              Pragma Night
Indexed Subscripting
setObject:atIndexedSubscript:

 @implementation SongList
 {
     NSMutableArray *_songs;
 }

 - (Song *)replaceSong:(Song *)newSong
               atIndex:(NSUInteger)idx
 {
     Song *oldSong = _songs[idx];
     _songs[idx] = newSong;
     return oldSong;
 }
 @end



                                         Pragma Night
Indexed Subscripting
setObject:atIndexedSubscript:

 @implementation SongList
 {
     NSMutableArray *_songs;
 }

 - (Song *)replaceSong:(Song *)newSong
               atIndex:(NSUInteger)idx
 {
     Song *oldSong = _songs[idx];
     [_songs setObject:newSong atIndexedSubscript:idx];
     return oldSong;
 }
 @end



                                                          Pragma Night
Keyed Subscripting
setObject:atIndexedSubscript:

 @implementation Database
 {
     NSMutableDictionary *_storage;
 }

 - (id)replaceObject:(id)newObject
              forKey:(id <NSCopying>)key
 {
     id oldObject = _storage[key];
     _storage[key] = newObject;
     return oldObject;
 }
 @end



                                           Pragma Night
Keyed Subscripting
setObject:atIndexedSubscript:

 @implementation Database
 {
     NSMutableDictionary *_storage;
 }

 - (id)replaceObject:(id)newObject
              forKey:(id <NSCopying>)key
 {
     id oldObject = _storage[key];
     [_storage setObject:newObject forKey:key];
     return oldObject;
 }
 @end



                                                  Pragma Night
Backward Compatibility !?
To deploy back to iOS 5 and iOS 4 you need ARCLite:
use ARC or set explicit linker flag: “-fobjc-arc”

To make compiler happy, you should add 4 categories:
 #if __IPHONE_OS_VERSION_MAX_ALLOWED < 60000
 @interface NSDictionary(BCSubscripting)
 - (id)objectForKeyedSubscript:(id)key;
 @end

 @interface NSMutableDictionary(BCSubscripting)
 - (void)setObject:(id)obj forKeyedSubscript:(id )key;
 @end

 @interface NSArray(BCSubscripting)
 - (id)objectAtIndexedSubscript:(NSUInteger)idx;
 @end

 @interface NSMutableArray(BCSubscripting)
 - (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;
 @end
 #endif



                                                                 Pragma Night
Your Classes Can Be Subscriptable
 @interface SongList : NSObject
 - (Song *)objectAtIndexedSubscript:(NSUInteger)idx;
 - (void)      setObject:(Song *)song
      atIndexedSubscript:(NSUInteger)idx;
 @end

 @implementation SongList {
     NSMutableArray *_songs;
 }
 - (Song *)objectAtIndexedSubscript:(NSUInteger)idx {
     return (Song *)_songs[idx];
 }
 - (void)      setObject:(Song *)song
      atIndexedSubscript:(NSUInteger)idx {
     _songs[idx] = song;
 }
 @end



                                                        Pragma Night
Summary


          Pragma Night
Availability
                        Feature     Xcode 4.4+   iOS 6 OSX 10.8
 Unordered Method Declarations          ✓
Enum With Fixed Underlying Type         ✓
         @Synthesize by Default         ✓
            NSNumbers Literals          ✓
       Boxed Expression Literals        ✓
                   Array Literals       ✓
              Dictionary Literals       ✓
             Object Subscripting        ✓              ✓*

                     * Partially Required
                                                         Pragma Night
Migration
Apple provides a migration tool which is build into
Xcode: Edit Refactor Convert to Modern ...




                                                 Pragma Night
Demo


       Pragma Night
Modern Objective-C References


•   Clang: Objective-C Literals

•   Apple: Programming with Objective-C

•   WWDC 2012 – Session 405 – Modern Objective-C

•   Mike Ash: Friday Q&A 2012-06-22: Objective-C Literals




                                                            Pragma Night
Modern Objective-C Books




                       Pragma Night
NSLog(@”Thank you!”);




  giuseppe.arici@pragmamark.org

                                  Pragma Night

Modern Objective-C @ Pragma Night

  • 1.
    Modern Objective-C Giuseppe Arici Pragma Night @ Talent Garden
  • 2.
    It’s All About... Syntactic Sugar Pragma Night
  • 3.
    Unordered Method Declarations Pragma Night
  • 4.
    Public & PrivateMethod Ordering @interface SongPlayer : NSObject - (void)playSong:(Song *)song; @end @implementation SongPlayer - (void)playSong:(Song *)song { NSError *error; [self startAudio:&error]; /* ... */ } - (void)startAudio:(NSError **)error { /* ... */ } Warning: @end instance method '-startAudio:' not found (return type defaults to 'id') Pragma Night
  • 5.
    Wrong Workaround In thepublic interface @interface SongPlayer : NSObject - (void)playSong:(Song *)song; - (void)startAudio:(NSError **)error; @end @implementation SongPlayer - (void)playSong:(Song *)song { NSError *error; [self startAudio:&error]; /* ... */ } - (void)startAudio:(NSError **)error { /* ... */ } @end Pragma Night
  • 6.
    Okay Workaround 1 Ina class extension @interface SongPlayer () - (void)startAudio:(NSError **)error; @end @implementation SongPlayer - (void)playSong:(Song *)song { NSError *error; [self startAudio:&error]; /* ... */ } - (void)startAudio:(NSError **)error { /* ... */ } @end Pragma Night
  • 7.
    Okay Workaround 2 Reordermethods @interface SongPlayer : NSObject - (void)playSong:(Song *)song; @end @implementation SongPlayer - (void)startAudio:(NSError **)error { /* ... */ } - (void)playSong:(Song *)song { NSError *error; [self startAudio:&error]; /* ... */ } @end Pragma Night
  • 8.
    Best Solution Parse the@implementation declarations then bodies @interface SongPlayer : NSObject - (void)playSong:(Song *)song; @end @implementation SongPlayer - (void)playSong:(Song *)song { NSError *error; [self startAudio:&error]; Xcode 4.4+ /* ... */ } - (void)startAudio:(NSError **)error { /* ... */ } @end Pragma Night
  • 9.
    Enum with Fixed UnderlyingType Pragma Night
  • 10.
    Enum with IndeterminateType Before OS X v10.5 and iOS typedef enum { NSNumberFormatterNoStyle, NSNumberFormatterDecimalStyle, NSNumberFormatterCurrencyStyle, NSNumberFormatterPercentStyle, NSNumberFormatterScientificStyle, NSNumberFormatterSpellOutStyle } NSNumberFormatterStyle; //typedef int NSNumberFormatterStyle; Pragma Night
  • 11.
    Enum with ExplicitType After OS X v10.5 and iOS enum { NSNumberFormatterNoStyle, NSNumberFormatterDecimalStyle, NSNumberFormatterCurrencyStyle, NSNumberFormatterPercentStyle, NSNumberFormatterScientificStyle, NSNumberFormatterSpellOutStyle }; typedef NSUInteger NSNumberFormatterStyle; • Pro: 32-bit and 64-bit portability • Con: no formal relationship between type and enum constants Pragma Night
  • 12.
    Enum with FixedUnderlying Type LLVM 4.2+ Compiler typedef enum NSNumberFormatterStyle : NSUInteger { NSNumberFormatterNoStyle, NSNumberFormatterDecimalStyle, NSNumberFormatterCurrencyStyle, NSNumberFormatterPercentStyle, NSNumberFormatterScientificStyle, NSNumberFormatterSpellOutStyle } NSNumberFormatterStyle; Xcode 4.4+ • Stronger type checking • Better code completion Pragma Night
  • 13.
    Enum with FixedUnderlying Type NS_ENUM macro typedef NS_ENUM(NSUInteger, NSNumberFormatterStyle) { NSNumberFormatterNoStyle, NSNumberFormatterDecimalStyle, NSNumberFormatterCurrencyStyle, NSNumberFormatterPercentStyle, NSNumberFormatterScientificStyle, NSNumberFormatterSpellOutStyle }; Xcode 4.4+ • Foundation declares like this Pragma Night
  • 14.
    Enum with FixedUnderlying Type Stronger type checking (-Wenum-conversion) NSNumberFormatterStyle style = NSNumberFormatterRoundUp; // 3 warning: implicit conversion from enumeration type 'enum NSNumberFormatterRoundingMode' to different enumeration type 'NSNumberFormatterStyle' (aka 'enum NSNumberFormatterStyle') Pragma Night
  • 15.
    Enum with FixedUnderlying Type Handling all enum values (-Wswitch) - (void) printStyle:(NSNumberFormatterStyle) style{ switch (style) { case NSNumberFormatterNoStyle: break; case NSNumberFormatterSpellOutStyle: break; } } warning: 4 enumeration values not handled in switch: 'NSNumberFormatterDecimalStyle', 'NSNumberFormatterCurrencyStyle', 'NSNumberFormatterPercentStyle'... Pragma Night
  • 16.
    @Synthesize by Default Pragma Night
  • 17.
    Properties Simplify Classes @interfaceinstance variables @interface Person : NSObject { NSString *_name; } @property(strong) NSString *name; @end @implementation Person @synthesize name = _name; @end Pragma Night
  • 18.
    Properties Simplify Classes @implementationinstance variables @interface Person : NSObject @property(strong) NSString *name; @end @implementation Person { NSString *_name; } @synthesize name = _name; @end Pragma Night
  • 19.
    Properties Simplify Classes Synthesizedinstance variables @interface Person : NSObject @property(strong) NSString *name; @end @implementation Person @synthesize name = _name; @end Pragma Night
  • 20.
    @Synthesize by Default LLVM4.2+ Compiler @interface Person : NSObject @property(strong) NSString *name; @end @implementation Person Xcode 4.4+ @end Pragma Night
  • 21.
    Instance Variable Name!? Instance variables now prefixed with “_” @interface Person : NSObject @property(strong) NSString *name; @end @implementation Person - (NSString *)description { return _name; Xcode 4.4+ } /* as if you'd written: @synthesize name = _name; */ @end Pragma Night
  • 22.
    Backward Compatibility !? Becareful, when in doubt be fully explicit @interface Person : NSObject @property(strong) NSString *name; @end @implementation Person @synthesize name; /* as if you'd written: @synthesize name = name; */ @end Pragma Night
  • 23.
    To @Synthesize by Default • Warning: @Synthesize by Default will not synthesize a property declared in a protocol • If you use custom instance variable naming convention, enable this warning ( -Wobjc-missing-property-synthesis ) Pragma Night
  • 24.
    Core Data NSManagedObject Optsout of synthesize by default /* NSManagedObject.h */ NS_REQUIRES_PROPERTY_DEFINITIONS @interface NSManagedObject : NSObject { • NSManagedObject synthesizes properties • Continue to use @property to declare typed accessors • Continue to use @dynamic to inhibit warnings Pragma Night
  • 25.
    NSNumbers Literals Pragma Night
  • 26.
    NSNumber Creation NSNumber *value; value= [NSNumber numberWithChar:'X']; value = [NSNumber numberWithInt:42]; value = [NSNumber numberWithUnsignedLong:42ul]; value = [NSNumber numberWithLongLong:42ll]; value = [NSNumber numberWithFloat:0.42f]; value = [NSNumber numberWithDouble:0.42]; value = [NSNumber numberWithBool:YES]; Pragma Night
  • 27.
    NSNumber Creation NSNumber *value; value= @'X'; value = @42; value = @42ul; value = @42ll; value = @0.42f; Xcode 4.4+ value = @0.42; value = @YES; Pragma Night
  • 28.
    Backward Compatibility !? #defineYES (BOOL)1 // Before iOS 6, OSX 10.8 #define YES ((BOOL)1) // After iOS 6, OSX 10.8 Workarounds @(YES) // Use parentheses around BOOL Macros #if __IPHONE_OS_VERSION_MAX_ALLOWED < 60000 #if __has_feature(objc_bool) #undef YES #undef NO // Redefine BOOL Macros #define YES __objc_yes #define NO __objc_no #endif #endif Pragma Night
  • 29.
    Boxed Expression Literals Pragma Night
  • 30.
    Boxed Expression Literals NSNumber*orientation = [NSNumber numberWithInt:UIDeviceOrientationPortrait]; NSNumber *piOverSixteen = [NSNumber numberWithDouble:( M_PI / 16 )]; NSNumber *parityDigit = [NSNumber numberWithChar:"EO"[i % 2]]; NSString *path = [NSString stringWithUTF8String:getenv("PATH")]; NSNumber *usesCompass = [NSNumber numberWithBool: [CLLocationManager headingAvailable]]; Pragma Night
  • 31.
    Boxed Expression Literals NSNumber*orientation = @( UIDeviceOrientationPortrait ); NSNumber *piOverSixteen = @( M_PI / 16 ); NSNumber *parityDigit = @( "OE"[i % 2] ); NSString *path = @( getenv("PATH") ); Xcode 4.4+ NSNumber *usesCompass = @( [CLLocationManager headingAvailable] ); Pragma Night
  • 32.
    Array Literals Pragma Night
  • 33.
    Array Creation More choices,and more chances for errors NSArray *array; array = [NSArray array]; array = [NSArray arrayWithObject:a]; array = [NSArray arrayWithObjects:a, b, c, nil]; id objects[] = { a, b, c }; NSUInteger count = sizeof(objects) / sizeof(id); array = [NSArray arrayWithObjects:objects count:count]; Pragma Night
  • 34.
    Nil Termination Inconsistent behavior // if you write: id a = nil, b = @"hello", c = @42; NSArray *array = [NSArray arrayWithObjects:a, b, c, nil]; Array will be empty // if you write: id objects[] = { nil, @"hello", @42 }; NSUInteger count = sizeof(objects)/ sizeof(id); NSArray *array = [NSArray arrayWithObjects:objects count:count]; Exception: attempt to insert nil object from objects[0] Pragma Night
  • 35.
    Array Creation NSArray *array; array= [NSArray array]; array = [NSArray arrayWithObject:a]; array = [NSArray arrayWithObjects:a, b, c, nil]; id objects[] = { a, b, c }; NSUInteger count = sizeof(objects) / sizeof(id); array = [NSArray arrayWithObjects:objects count:count]; Pragma Night
  • 36.
    Array Creation NSArray *array; array= @[]; array = @[ a ]; array = @[ a, b, c ]; Xcode 4.4+ array = @[ a, b, c ]; Pragma Night
  • 37.
    How Array LiteralsWork // when you write this: NSArray *array = @[ a, b, c ]; // compiler generates: id objects[] = { a, b, c }; NSUInteger count = sizeof(objects)/ sizeof(id); NSArray *array = [NSArray arrayWithObjects:objects count:count]; Pragma Night
  • 38.
  • 39.
    Dictionary Creation More choices,and more chances for errors NSDictionary *dict; dict = [NSDictionary dictionary]; dict = [NSDictionary dictionaryWithObject:o1 forKey:k1]; dict = [NSDictionary dictionaryWithObjectsAndKeys: o1, k1, o2, k2, o3, k3, nil]; id objects[] = { o1, o2, o3 }; id keys[] = { k1, k2, k3 }; NSUInteger count = sizeof(objects) / sizeof(id); dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys count:count]; Pragma Night
  • 40.
    Dictionary Creation NSDictionary *dict; dict= [NSDictionary dictionary]; dict = [NSDictionary dictionaryWithObject:o1 forKey:k1]; dict = [NSDictionary dictionaryWithObjectsAndKeys: o1, k1, o2, k2, o3, k3, nil]; id objects[] = { o1, o2, o3 }; id keys[] = { k1, k2, k3 }; NSUInteger count = sizeof(objects) / sizeof(id); dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys count:count]; Pragma Night
  • 41.
    Dictionary Creation NSDictionary *dict; dict= @{}; dict = @{ k1 : o1 }; // key before object dict = @{ k1 : o1, k2 : o2, k3 : o3 }; Xcode 4.4+ dict = @{ k1 : o1, k2 : o2, k3 : o3 }; Pragma Night
  • 42.
    How Dictionary LiteralsWork // when you write this: NSDictionary *dict = @{ k1 : o1, k2 : o2, k3 : o3 }; // compiler generates: id objects[] = { o1, o2, o3 }; id keys[] = { k1, k2, k3 }; NSUInteger count = sizeof(objects) / sizeof(id); NSDictionary *dict = [NSDictionary dictionaryWithObjects:objects orKeys:keys count:count]; Pragma Night
  • 43.
    Container Literals Restriction Allcontainers are immutable, mutable use: -mutableCopy NSMutableArray *mutablePragmers = [@[ @"Fra", @"Giu", @"Mat", @"Max", @"Ste" ] mutableCopy]; For constant containers, simply implement +initialize static NSArray *thePragmers; + (void)initialize { if (self == [MyClass class]) { thePragmers = @[ @"Fra", @"Giu", @"Mat", @"Max", @"Ste" ]; } } Pragma Night
  • 44.
  • 45.
    Array Subscripting New syntaxto access object at index: nsarray[index] @implementation SongList { NSMutableArray *_songs; } - (Song *)replaceSong:(Song *)newSong atIndex:(NSUInteger)idx { Song *oldSong = [_songs objectAtIndex:idx]; [_songs replaceObjectAtIndex:idx withObject:newSong]; return oldSong; } @end Pragma Night
  • 46.
    Array Subscripting New syntaxto access object at index: nsarray[index] @implementation SongList { NSMutableArray *_songs; } - (Song *)replaceSong:(Song *)newSong atIndex:(NSUInteger)idx { Xcode 4.4+ Song *oldSong = _songs[idx]; _songs[idx] = newSong; return oldSong; } @end Pragma Night
  • 47.
    Dictionary Subscripting New syntaxto access object by key: nsdictionary[key] @implementation Database { NSMutableDictionary *_storage; } - (id)replaceObject:(id)newObject forKey:(id <NSCopying>)key { id oldObject = [_storage objectForKey:key]; [_storage setObject:newObject forKey:key]; return oldObject; } @end Pragma Night
  • 48.
    Dictionary Subscripting New syntaxto access object by key: nsdictionary[key] @implementation Database { NSMutableDictionary *_storage; } - (id)replaceObject:(id)newObject forKey:(id <NSCopying>)key { Xcode 4.4+ id oldObject = _storage[key]; _storage[key] = newObject; return oldObject; } @end Pragma Night
  • 49.
    How Subscripting Works iOS 6 Array Style: Indexed subscripting methods OSX 10.8 - (elementType)objectAtIndexedSubscript:(indexType)idx - (void)setObject:(elementType)obj atIndexedSubscript:(indexType)idx; elementType must be an object pointer, indexType must be integral iOS 6 Dictionary Style: Keyed subscripting methods OSX 10.8 - (elementType)objectForKeyedSubscript:(keyType)key; - (void)setObject:(elementType)obj forKeyedSubscript:(keyType)key; elementType and keyType must be an object pointer Pragma Night
  • 50.
    Indexed Subscripting setObject:atIndexedSubscript: @implementationSongList { NSMutableArray *_songs; } - (Song *)replaceSong:(Song *)newSong atIndex:(NSUInteger)idx { Song *oldSong = _songs[idx]; _songs[idx] = newSong; return oldSong; } @end Pragma Night
  • 51.
    Indexed Subscripting setObject:atIndexedSubscript: @implementationSongList { NSMutableArray *_songs; } - (Song *)replaceSong:(Song *)newSong atIndex:(NSUInteger)idx { Song *oldSong = _songs[idx]; [_songs setObject:newSong atIndexedSubscript:idx]; return oldSong; } @end Pragma Night
  • 52.
    Keyed Subscripting setObject:atIndexedSubscript: @implementationDatabase { NSMutableDictionary *_storage; } - (id)replaceObject:(id)newObject forKey:(id <NSCopying>)key { id oldObject = _storage[key]; _storage[key] = newObject; return oldObject; } @end Pragma Night
  • 53.
    Keyed Subscripting setObject:atIndexedSubscript: @implementationDatabase { NSMutableDictionary *_storage; } - (id)replaceObject:(id)newObject forKey:(id <NSCopying>)key { id oldObject = _storage[key]; [_storage setObject:newObject forKey:key]; return oldObject; } @end Pragma Night
  • 54.
    Backward Compatibility !? Todeploy back to iOS 5 and iOS 4 you need ARCLite: use ARC or set explicit linker flag: “-fobjc-arc” To make compiler happy, you should add 4 categories: #if __IPHONE_OS_VERSION_MAX_ALLOWED < 60000 @interface NSDictionary(BCSubscripting) - (id)objectForKeyedSubscript:(id)key; @end @interface NSMutableDictionary(BCSubscripting) - (void)setObject:(id)obj forKeyedSubscript:(id )key; @end @interface NSArray(BCSubscripting) - (id)objectAtIndexedSubscript:(NSUInteger)idx; @end @interface NSMutableArray(BCSubscripting) - (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx; @end #endif Pragma Night
  • 55.
    Your Classes CanBe Subscriptable @interface SongList : NSObject - (Song *)objectAtIndexedSubscript:(NSUInteger)idx; - (void) setObject:(Song *)song atIndexedSubscript:(NSUInteger)idx; @end @implementation SongList { NSMutableArray *_songs; } - (Song *)objectAtIndexedSubscript:(NSUInteger)idx { return (Song *)_songs[idx]; } - (void) setObject:(Song *)song atIndexedSubscript:(NSUInteger)idx { _songs[idx] = song; } @end Pragma Night
  • 56.
    Summary Pragma Night
  • 57.
    Availability Feature Xcode 4.4+ iOS 6 OSX 10.8 Unordered Method Declarations ✓ Enum With Fixed Underlying Type ✓ @Synthesize by Default ✓ NSNumbers Literals ✓ Boxed Expression Literals ✓ Array Literals ✓ Dictionary Literals ✓ Object Subscripting ✓ ✓* * Partially Required Pragma Night
  • 58.
    Migration Apple provides amigration tool which is build into Xcode: Edit Refactor Convert to Modern ... Pragma Night
  • 59.
    Demo Pragma Night
  • 60.
    Modern Objective-C References • Clang: Objective-C Literals • Apple: Programming with Objective-C • WWDC 2012 – Session 405 – Modern Objective-C • Mike Ash: Friday Q&A 2012-06-22: Objective-C Literals Pragma Night
  • 61.
  • 62.
    NSLog(@”Thank you!”); giuseppe.arici@pragmamark.org Pragma Night