ݺߣ

ݺߣShare a Scribd company logo
iPhone for .NET Developers
       Ben Scheirman
       Director of Development - ChaiONE
       @subdigital




Tuesday, September 28, 2010
What you need

           A Mac
           Xcode
           iPhone SDK (limited to Simulator)
           iPhone Developer Program ($99 /year)




Tuesday, September 28, 2010
Objective-C

                Based on C
                Object Oriented
                Dynamic
                A little weird
                Powerful




Tuesday, September 28, 2010
Objective-C Primer
           Calling methods




Tuesday, September 28, 2010
Objective-C Primer
           Calling methods


       [someObject someMethod];




Tuesday, September 28, 2010
Objective-C Primer
           Calling methods


       [someObject someMethod];

       [someObject someMethodWithInput:5];




Tuesday, September 28, 2010
Objective-C Primer
           Calling methods


       [someObject someMethod];

       [someObject someMethodWithInput:5];

       [dictionary setObject:obj
                      forKey:key];


Tuesday, September 28, 2010
Objective-C Primer
           Nesting method calls




Tuesday, September 28, 2010
Objective-C Primer
           Nesting method calls

       [NSString stringWithFormat:
           [prefs format]];




Tuesday, September 28, 2010
Objective-C Primer
           Instantiating classes




Tuesday, September 28, 2010
Objective-C Primer
           Instantiating classes


       UIView *view = [[UIView alloc] init];




Tuesday, September 28, 2010
Objective-C Primer
           Instantiating classes


       UIView *view = [[UIView alloc] init];


       NSDate *date = [NSDate date];




Tuesday, September 28, 2010
Objective-C Primer
           De?ning Classes




Tuesday, September 28, 2010
Objective-C Primer
           De?ning Classes

        //Person.h
        @interface Person {
          //instance variables
        }

        //properties & methods

        @end


Tuesday, September 28, 2010
Objective-C Primer
           De?ning Classes




Tuesday, September 28, 2010
Objective-C Primer
           De?ning Classes

        //Person.m
        #import "Person.h"

        @implementation Person

        //implement properties & methods

        @end


Tuesday, September 28, 2010
Objective-C Primer
           De?ning Methods




Tuesday, September 28, 2010
Objective-C Primer
           De?ning Methods



        -(void)showLoadingText:(NSString *)text animated:(BOOL)animated;




Tuesday, September 28, 2010
Objective-C Primer
           De?ning Methods
                                    Method name (selector)




        -(void)showLoadingText:(NSString *)text animated:(BOOL)animated;




Tuesday, September 28, 2010
Objective-C Primer
           De?ning Methods
                                    Method name (selector)




        -(void)showLoadingText:(NSString *)text animated:(BOOL)animated;




               Return Type




Tuesday, September 28, 2010
Objective-C Primer
           De?ning Methods
                                    Method name (selector)




        -(void)showLoadingText:(NSString *)text animated:(BOOL)animated;




               Return Type


 Instance method

Tuesday, September 28, 2010
Objective-C Primer
           De?ning Methods
                                    Method name (selector)




        -(void)showLoadingText:(NSString *)text animated:(BOOL)animated;




               Return Type
                                                  Parameters

 Instance method

Tuesday, September 28, 2010
Memory Management


           No garbage collection on the iPhone
           Retain / Release




Tuesday, September 28, 2010
Memory Management
           Retain / Release Dance




Tuesday, September 28, 2010
Memory Management
           Retain / Release Dance

                                        1
       Foo *foo = [[Foo alloc] init];




Tuesday, September 28, 2010
Memory Management
           Retain / Release Dance

                                        1
       Foo *foo = [[Foo alloc] init];
                                        2
       [foo retain];




Tuesday, September 28, 2010
Memory Management
           Retain / Release Dance

                                        1
       Foo *foo = [[Foo alloc] init];
                                        2
       [foo retain];
                                        1
       [foo release];


Tuesday, September 28, 2010
Memory Management
           Retain / Release Dance

                                        1
       Foo *foo = [[Foo alloc] init];
                                        2
       [foo retain];
                                        1
       [foo release];

       [foo release];                   0

Tuesday, September 28, 2010
Memory Management
           Retain / Release Dance

                                                         1
       Foo *foo = [[Foo alloc] init];
                                                         2
       [foo retain];
                                                         1
       [foo release];

       [foo release];               foo is deallocated   0

Tuesday, September 28, 2010
Getters / Setters




Tuesday, September 28, 2010
Getters / Setters

        [foo setBar:@"baz"];




Tuesday, September 28, 2010
Getters / Setters

        [foo setBar:@"baz"];

        [foo bar]; //returns @"baz"




Tuesday, September 28, 2010
Getters / Setters

        [foo setBar:@"baz"];

        [foo bar]; //returns @"baz"


        foo.bar = @"gruul";



Tuesday, September 28, 2010
Getters / Setters

        [foo setBar:@"baz"];

        [foo bar]; //returns @"baz"


        foo.bar = @"gruul";

        foo.bar //returns @"gruul"
Tuesday, September 28, 2010
Implementing setters




Tuesday, September 28, 2010
Implementing setters
     -(void)setBar:(id)value {




Tuesday, September 28, 2010
Implementing setters
     -(void)setBar:(id)value {
       if(bar == value) return;




Tuesday, September 28, 2010
Implementing setters
     -(void)setBar:(id)value {
       if(bar == value) return;
       if(bar != nil) {




Tuesday, September 28, 2010
Implementing setters
     -(void)setBar:(id)value {
       if(bar == value) return;
       if(bar != nil) {
         [bar release];




Tuesday, September 28, 2010
Implementing setters
     -(void)setBar:(id)value {
       if(bar == value) return;
       if(bar != nil) {
         [bar release];
         bar = nil;




Tuesday, September 28, 2010
Implementing setters
     -(void)setBar:(id)value {
       if(bar == value) return;
       if(bar != nil) {
         [bar release];
         bar = nil;
       }




Tuesday, September 28, 2010
Implementing setters
     -(void)setBar:(id)value {
       if(bar == value) return;
       if(bar != nil) {
         [bar release];
         bar = nil;
       }
       if(value != nil)



Tuesday, September 28, 2010
Implementing setters
     -(void)setBar:(id)value {
       if(bar == value) return;
       if(bar != nil) {
         [bar release];
         bar = nil;
       }
       if(value != nil)
         bar = [value retain];

Tuesday, September 28, 2010
Implementing setters
     -(void)setBar:(id)value {
       if(bar == value) return;
       if(bar != nil) {
         [bar release];
         bar = nil;
       }
       if(value != nil)
         bar = [value retain];
     }
Tuesday, September 28, 2010
No Thanks




Tuesday, September 28, 2010
Properties




Tuesday, September 28, 2010
Properties
     //Foo.h




Tuesday, September 28, 2010
Properties
     //Foo.h
     @property (nonatomic, retain) UIImage *image;




Tuesday, September 28, 2010
Properties
     //Foo.h
     @property (nonatomic, retain) UIImage *image;
     @property (nonatomic, copy) NSString *message;




Tuesday, September 28, 2010
Properties
     //Foo.h
     @property (nonatomic, retain) UIImage *image;
     @property (nonatomic, copy) NSString *message;




     //Foo.m




Tuesday, September 28, 2010
Properties
     //Foo.h
     @property (nonatomic, retain) UIImage *image;
     @property (nonatomic, copy) NSString *message;




     //Foo.m
     @synthesize image, message;




Tuesday, September 28, 2010
Properties
     //Foo.h
     @property (nonatomic, retain) UIImage *image;
     @property (nonatomic, copy) NSString *message;




     //Foo.m
     @synthesize image, message;

     -(void)dealloc {




Tuesday, September 28, 2010
Properties
     //Foo.h
     @property (nonatomic, retain) UIImage *image;
     @property (nonatomic, copy) NSString *message;




     //Foo.m
     @synthesize image, message;

     -(void)dealloc {

        [image release];




Tuesday, September 28, 2010
Properties
     //Foo.h
     @property (nonatomic, retain) UIImage *image;
     @property (nonatomic, copy) NSString *message;




     //Foo.m
     @synthesize image, message;

     -(void)dealloc {

        [image release];
        [message release];




Tuesday, September 28, 2010
Properties
     //Foo.h
     @property (nonatomic, retain) UIImage *image;
     @property (nonatomic, copy) NSString *message;




     //Foo.m
     @synthesize image, message;

     -(void)dealloc {

        [image release];
        [message release];

        [super dealloc];



Tuesday, September 28, 2010
Properties
     //Foo.h
     @property (nonatomic, retain) UIImage *image;
     @property (nonatomic, copy) NSString *message;




     //Foo.m
     @synthesize image, message;

     -(void)dealloc {

         [image release];
         [message release];

         [super dealloc];
     }


Tuesday, September 28, 2010
Dot Syntax Dogma


                                Use dot syntax if you like it

                              Just be aware of what it's hiding




Tuesday, September 28, 2010
Xcode

           Your IDE
           Code completion
           Interactive Debugger


           Lacks good refactoring tools




Tuesday, September 28, 2010
Interface Builder


           Drag-n-drop UI building
           Layouts are de?ned in XIBs (XML representation).
           Usually called "Nibs"


           "Make connections" with classes de?ned in Xcode
                variables --> UI components
                UI events --> methods
Tuesday, September 28, 2010
Instruments


           Find Memory Leaks
           Analyze Memory Usage
           Track down slow code




Tuesday, September 28, 2010
iOS SDK
                                       Accelerate     CoreMotion

                                      AddressBook    CoreTelephony

                                      AudioToolbox     CoreText
              Your app
                                      AVFoundation    CoreVideo

                              UIKit    CoreAudio       GameKit

                   CoreFoundation      CoreData           iAd

                     CoreGraphics     CoreLocation      MapKit

                                       CFNetwork        StoreKit



Tuesday, September 28, 2010
Model View Controller


                              Model                View




                                      Controller



Tuesday, September 28, 2010
The View Controller

           Handles setup logic for a screen
           Handles user input
           Interacts with the model
           Contains 1 or more views




Tuesday, September 28, 2010
The View

           Visual representation
           Drawing
           Laying out subviews (autorotation)
           May Handle touch events




Tuesday, September 28, 2010
Lifecycle of an App

                main.m




Tuesday, September 28, 2010
Lifecycle of an App

                main.m




           UIApplication




Tuesday, September 28, 2010
Lifecycle of an App

                main.m




           UIApplication




Tuesday, September 28, 2010
Lifecycle of an App

                main.m        MainWindow.xib




           UIApplication




Tuesday, September 28, 2010
Lifecycle of an App

                main.m        MainWindow.xib




           UIApplication




Tuesday, September 28, 2010
Lifecycle of an App

                main.m        MainWindow.xib




           UIApplication                         YourAppDelegate



                                               UIWindow   Root View Controller




Tuesday, September 28, 2010
Lifecycle of an App

                main.m                 MainWindow.xib




           UIApplication                                        YourAppDelegate
                              applicationDidFinishLaunching




                                                              UIWindow   Root View Controller




Tuesday, September 28, 2010
Time to code!




Tuesday, September 28, 2010

More Related Content

iPhone for .NET Developers

  • 1. iPhone for .NET Developers Ben Scheirman Director of Development - ChaiONE @subdigital Tuesday, September 28, 2010
  • 2. What you need A Mac Xcode iPhone SDK (limited to Simulator) iPhone Developer Program ($99 /year) Tuesday, September 28, 2010
  • 3. Objective-C Based on C Object Oriented Dynamic A little weird Powerful Tuesday, September 28, 2010
  • 4. Objective-C Primer Calling methods Tuesday, September 28, 2010
  • 5. Objective-C Primer Calling methods [someObject someMethod]; Tuesday, September 28, 2010
  • 6. Objective-C Primer Calling methods [someObject someMethod]; [someObject someMethodWithInput:5]; Tuesday, September 28, 2010
  • 7. Objective-C Primer Calling methods [someObject someMethod]; [someObject someMethodWithInput:5]; [dictionary setObject:obj forKey:key]; Tuesday, September 28, 2010
  • 8. Objective-C Primer Nesting method calls Tuesday, September 28, 2010
  • 9. Objective-C Primer Nesting method calls [NSString stringWithFormat: [prefs format]]; Tuesday, September 28, 2010
  • 10. Objective-C Primer Instantiating classes Tuesday, September 28, 2010
  • 11. Objective-C Primer Instantiating classes UIView *view = [[UIView alloc] init]; Tuesday, September 28, 2010
  • 12. Objective-C Primer Instantiating classes UIView *view = [[UIView alloc] init]; NSDate *date = [NSDate date]; Tuesday, September 28, 2010
  • 13. Objective-C Primer De?ning Classes Tuesday, September 28, 2010
  • 14. Objective-C Primer De?ning Classes //Person.h @interface Person { //instance variables } //properties & methods @end Tuesday, September 28, 2010
  • 15. Objective-C Primer De?ning Classes Tuesday, September 28, 2010
  • 16. Objective-C Primer De?ning Classes //Person.m #import "Person.h" @implementation Person //implement properties & methods @end Tuesday, September 28, 2010
  • 17. Objective-C Primer De?ning Methods Tuesday, September 28, 2010
  • 18. Objective-C Primer De?ning Methods -(void)showLoadingText:(NSString *)text animated:(BOOL)animated; Tuesday, September 28, 2010
  • 19. Objective-C Primer De?ning Methods Method name (selector) -(void)showLoadingText:(NSString *)text animated:(BOOL)animated; Tuesday, September 28, 2010
  • 20. Objective-C Primer De?ning Methods Method name (selector) -(void)showLoadingText:(NSString *)text animated:(BOOL)animated; Return Type Tuesday, September 28, 2010
  • 21. Objective-C Primer De?ning Methods Method name (selector) -(void)showLoadingText:(NSString *)text animated:(BOOL)animated; Return Type Instance method Tuesday, September 28, 2010
  • 22. Objective-C Primer De?ning Methods Method name (selector) -(void)showLoadingText:(NSString *)text animated:(BOOL)animated; Return Type Parameters Instance method Tuesday, September 28, 2010
  • 23. Memory Management No garbage collection on the iPhone Retain / Release Tuesday, September 28, 2010
  • 24. Memory Management Retain / Release Dance Tuesday, September 28, 2010
  • 25. Memory Management Retain / Release Dance 1 Foo *foo = [[Foo alloc] init]; Tuesday, September 28, 2010
  • 26. Memory Management Retain / Release Dance 1 Foo *foo = [[Foo alloc] init]; 2 [foo retain]; Tuesday, September 28, 2010
  • 27. Memory Management Retain / Release Dance 1 Foo *foo = [[Foo alloc] init]; 2 [foo retain]; 1 [foo release]; Tuesday, September 28, 2010
  • 28. Memory Management Retain / Release Dance 1 Foo *foo = [[Foo alloc] init]; 2 [foo retain]; 1 [foo release]; [foo release]; 0 Tuesday, September 28, 2010
  • 29. Memory Management Retain / Release Dance 1 Foo *foo = [[Foo alloc] init]; 2 [foo retain]; 1 [foo release]; [foo release]; foo is deallocated 0 Tuesday, September 28, 2010
  • 30. Getters / Setters Tuesday, September 28, 2010
  • 31. Getters / Setters [foo setBar:@"baz"]; Tuesday, September 28, 2010
  • 32. Getters / Setters [foo setBar:@"baz"]; [foo bar]; //returns @"baz" Tuesday, September 28, 2010
  • 33. Getters / Setters [foo setBar:@"baz"]; [foo bar]; //returns @"baz" foo.bar = @"gruul"; Tuesday, September 28, 2010
  • 34. Getters / Setters [foo setBar:@"baz"]; [foo bar]; //returns @"baz" foo.bar = @"gruul"; foo.bar //returns @"gruul" Tuesday, September 28, 2010
  • 36. Implementing setters -(void)setBar:(id)value { Tuesday, September 28, 2010
  • 37. Implementing setters -(void)setBar:(id)value { if(bar == value) return; Tuesday, September 28, 2010
  • 38. Implementing setters -(void)setBar:(id)value { if(bar == value) return; if(bar != nil) { Tuesday, September 28, 2010
  • 39. Implementing setters -(void)setBar:(id)value { if(bar == value) return; if(bar != nil) { [bar release]; Tuesday, September 28, 2010
  • 40. Implementing setters -(void)setBar:(id)value { if(bar == value) return; if(bar != nil) { [bar release]; bar = nil; Tuesday, September 28, 2010
  • 41. Implementing setters -(void)setBar:(id)value { if(bar == value) return; if(bar != nil) { [bar release]; bar = nil; } Tuesday, September 28, 2010
  • 42. Implementing setters -(void)setBar:(id)value { if(bar == value) return; if(bar != nil) { [bar release]; bar = nil; } if(value != nil) Tuesday, September 28, 2010
  • 43. Implementing setters -(void)setBar:(id)value { if(bar == value) return; if(bar != nil) { [bar release]; bar = nil; } if(value != nil) bar = [value retain]; Tuesday, September 28, 2010
  • 44. Implementing setters -(void)setBar:(id)value { if(bar == value) return; if(bar != nil) { [bar release]; bar = nil; } if(value != nil) bar = [value retain]; } Tuesday, September 28, 2010
  • 47. Properties //Foo.h Tuesday, September 28, 2010
  • 48. Properties //Foo.h @property (nonatomic, retain) UIImage *image; Tuesday, September 28, 2010
  • 49. Properties //Foo.h @property (nonatomic, retain) UIImage *image; @property (nonatomic, copy) NSString *message; Tuesday, September 28, 2010
  • 50. Properties //Foo.h @property (nonatomic, retain) UIImage *image; @property (nonatomic, copy) NSString *message; //Foo.m Tuesday, September 28, 2010
  • 51. Properties //Foo.h @property (nonatomic, retain) UIImage *image; @property (nonatomic, copy) NSString *message; //Foo.m @synthesize image, message; Tuesday, September 28, 2010
  • 52. Properties //Foo.h @property (nonatomic, retain) UIImage *image; @property (nonatomic, copy) NSString *message; //Foo.m @synthesize image, message; -(void)dealloc { Tuesday, September 28, 2010
  • 53. Properties //Foo.h @property (nonatomic, retain) UIImage *image; @property (nonatomic, copy) NSString *message; //Foo.m @synthesize image, message; -(void)dealloc { [image release]; Tuesday, September 28, 2010
  • 54. Properties //Foo.h @property (nonatomic, retain) UIImage *image; @property (nonatomic, copy) NSString *message; //Foo.m @synthesize image, message; -(void)dealloc { [image release]; [message release]; Tuesday, September 28, 2010
  • 55. Properties //Foo.h @property (nonatomic, retain) UIImage *image; @property (nonatomic, copy) NSString *message; //Foo.m @synthesize image, message; -(void)dealloc { [image release]; [message release]; [super dealloc]; Tuesday, September 28, 2010
  • 56. Properties //Foo.h @property (nonatomic, retain) UIImage *image; @property (nonatomic, copy) NSString *message; //Foo.m @synthesize image, message; -(void)dealloc { [image release]; [message release]; [super dealloc]; } Tuesday, September 28, 2010
  • 57. Dot Syntax Dogma Use dot syntax if you like it Just be aware of what it's hiding Tuesday, September 28, 2010
  • 58. Xcode Your IDE Code completion Interactive Debugger Lacks good refactoring tools Tuesday, September 28, 2010
  • 59. Interface Builder Drag-n-drop UI building Layouts are de?ned in XIBs (XML representation). Usually called "Nibs" "Make connections" with classes de?ned in Xcode variables --> UI components UI events --> methods Tuesday, September 28, 2010
  • 60. Instruments Find Memory Leaks Analyze Memory Usage Track down slow code Tuesday, September 28, 2010
  • 61. iOS SDK Accelerate CoreMotion AddressBook CoreTelephony AudioToolbox CoreText Your app AVFoundation CoreVideo UIKit CoreAudio GameKit CoreFoundation CoreData iAd CoreGraphics CoreLocation MapKit CFNetwork StoreKit Tuesday, September 28, 2010
  • 62. Model View Controller Model View Controller Tuesday, September 28, 2010
  • 63. The View Controller Handles setup logic for a screen Handles user input Interacts with the model Contains 1 or more views Tuesday, September 28, 2010
  • 64. The View Visual representation Drawing Laying out subviews (autorotation) May Handle touch events Tuesday, September 28, 2010
  • 65. Lifecycle of an App main.m Tuesday, September 28, 2010
  • 66. Lifecycle of an App main.m UIApplication Tuesday, September 28, 2010
  • 67. Lifecycle of an App main.m UIApplication Tuesday, September 28, 2010
  • 68. Lifecycle of an App main.m MainWindow.xib UIApplication Tuesday, September 28, 2010
  • 69. Lifecycle of an App main.m MainWindow.xib UIApplication Tuesday, September 28, 2010
  • 70. Lifecycle of an App main.m MainWindow.xib UIApplication YourAppDelegate UIWindow Root View Controller Tuesday, September 28, 2010
  • 71. Lifecycle of an App main.m MainWindow.xib UIApplication YourAppDelegate applicationDidFinishLaunching UIWindow Root View Controller Tuesday, September 28, 2010
  • 72. Time to code! Tuesday, September 28, 2010