Objective-C & iPhone for .NET Developers

2,990 views

Published on

I gave this talk at iPhone Dev Con in Boston, April 2011.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,990
On SlideShare
0
From Embeds
0
Number of Embeds
348
Actions
Shares
0
Downloads
62
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Objective-C & iPhone for .NET Developers

    1. 1. iPhone for .NET DevelopersBen ScheirmanDirector of Development - ChaiONE@subdigital
    2. 2. Slide 1 of 429
    3. 3. What you needA MacXcodeiPhone SDK (limited to Simulator)iPhone Developer Program ($99 /year)
    4. 4. Objective-C Based on C Object Oriented Dynamic A little weird Powerful
    5. 5. Objective-C Primer Calling methods
    6. 6. Objective-C Primer Calling methods[someObject someMethod];
    7. 7. Objective-C Primer Calling methods[someObject someMethod];[someObject someMethodWithInput:5];
    8. 8. Objective-C Primer Calling methods[someObject someMethod];[someObject someMethodWithInput:5];[dictionary setObject:obj forKey:key];
    9. 9. Objective-C Primer Nesting method calls
    10. 10. Objective-C Primer Nesting method calls[NSString stringWithFormat: [prefs format]];
    11. 11. Objective-C Primer Instantiating classes
    12. 12. Objective-C Primer Instantiating classesUIView *view = [[UIView alloc] init];
    13. 13. Objective-C Primer Instantiating classesUIView *view = [[UIView alloc] init];NSDate *date = [NSDate date];
    14. 14. Objective-C Primer Defining Classes
    15. 15. Objective-C Primer Defining Classes//Person.h@interface Person { //instance variables}//properties & methods@end
    16. 16. Objective-C Primer Defining Classes
    17. 17. Objective-C Primer Defining Classes//Person.m#import "Person.h"@implementation Person//implement properties & methods@end
    18. 18. Objective-C Primer Defining Methods
    19. 19. Objective-C Primer Defining Methods-(void)showLoadingText:(NSString *)text animated:(BOOL)animated;
    20. 20. Objective-C Primer Defining Methods Method name (selector)-(void)showLoadingText:(NSString *)text animated:(BOOL)animated;
    21. 21. Objective-C Primer Defining Methods Method name (selector)-(void)showLoadingText:(NSString *)text animated:(BOOL)animated; Return Type
    22. 22. Objective-C Primer Defining Methods Method name (selector) -(void)showLoadingText:(NSString *)text animated:(BOOL)animated; Return TypeInstance method
    23. 23. Objective-C Primer Defining Methods Method name (selector) -(void)showLoadingText:(NSString *)text animated:(BOOL)animated; Return Type ParametersInstance method
    24. 24. Other interesting Things nil is a no-op Foo *foo = nil; [foo doSomething]; //nothing happens NSArray *array = nil; [array count]; //returns 0
    25. 25. Other Interesting Things Mix-in C style method definitions// I put this in Macros.hstatic inline BOOL isEmpty(id thing) { return thing == nil || ([thing respondsToSelector:@selector(length)] && [(NSData *)thing length] == 0) || ([thing respondsToSelector:@selector(count)] && [(NSArray *)thing count] == 0);} this method comes from Wil Shipley
    26. 26. Other Interesting Things Dynamic Programming achieved through Key-Value Coding NSArray *items = [NSArray arrayWithObject:foo]; [items valueForKey:@"count"]; //returns 1
    27. 27. Other Interesting Things Can achieve "Extension Methods" or "Mix-ins" via Objective-C Categories //NSString+MyAdditions.h @interface NSString (MyAdditions) -(NSString *)stringInReverse; @end //NSString+MyAdditions.m @implementation NSString (MyAdditions) -(NSString *)stringInReverse { ... } @end //usage #import "NSString+MyAdditions.h" [@"foxy" stringInReverse]; //returns @"yxof"
    28. 28. Memory ManagementNo garbage collection on the iPhoneRetain / Release
    29. 29. Memory ManagementRetain / Release Dance
    30. 30. Memory Management Retain / Release Dance 1Foo *foo = [[Foo alloc] init];
    31. 31. Memory Management Retain / Release Dance 1Foo *foo = [[Foo alloc] init]; 2[foo retain];
    32. 32. Memory Management Retain / Release Dance 1Foo *foo = [[Foo alloc] init]; 2[foo retain]; 1[foo release];
    33. 33. Memory Management Retain / Release Dance 1Foo *foo = [[Foo alloc] init]; 2[foo retain]; 1[foo release];[foo release]; 0
    34. 34. Memory Management Retain / Release Dance 1Foo *foo = [[Foo alloc] init]; 2[foo retain]; 1[foo release];[foo release]; foo is deallocated 0
    35. 35. Forgetting to release (over-retaining)YourClass
    36. 36. Forgetting to release (over-retaining) 1 alloc/initYour ObjectClass
    37. 37. Forgetting to release (over-retaining) 2 1 alloc/init retainYour ObjectClass
    38. 38. Forgetting to release (over-retaining) 2 1 alloc/init retainYour doSomething ObjectClass
    39. 39. Forgetting to release (over-retaining) 1 alloc/init retainYour doSomething ObjectClass release
    40. 40. Forgetting to release (over-retaining) 1 Object
    41. 41. Forgetting to release (over-retaining) 1 Object Object is leaked App doesnt crash
    42. 42. Over-releasingYourClass
    43. 43. Over-releasing 1 alloc/initYour ObjectClass
    44. 44. Over-releasing alloc/init releaseYour GarbageClass Memory
    45. 45. Over-releasing alloc/init releaseYour GarbageClass doSomething Memory App Crashes
    46. 46. Getters / Setters
    47. 47. Getters / Setters[foo setBar:@"baz"];
    48. 48. Getters / Setters[foo setBar:@"baz"];[foo bar]; //returns @"baz"
    49. 49. Getters / Setters[foo setBar:@"baz"];[foo bar]; //returns @"baz"foo.bar = @"gruul";
    50. 50. Getters / Setters[foo setBar:@"baz"];[foo bar]; //returns @"baz"foo.bar = @"gruul";foo.bar //returns @"gruul"
    51. 51. Implementing setters
    52. 52. Implementing setters-(void)setBar:(id)value {
    53. 53. Implementing setters-(void)setBar:(id)value { if(bar == value) return;
    54. 54. Implementing setters-(void)setBar:(id)value { if(bar == value) return; [bar release];
    55. 55. Implementing setters-(void)setBar:(id)value { if(bar == value) return; [bar release]; bar = nil;
    56. 56. Implementing setters-(void)setBar:(id)value { if(bar == value) return; [bar release]; bar = nil; if(value != nil)
    57. 57. Implementing setters-(void)setBar:(id)value { if(bar == value) return; [bar release]; bar = nil; if(value != nil) bar = [value retain];
    58. 58. Implementing setters-(void)setBar:(id)value { if(bar == value) return; [bar release]; bar = nil; if(value != nil) bar = [value retain];}
    59. 59. No Thanks
    60. 60. Properties
    61. 61. Properties//Foo.h
    62. 62. Properties//Foo.h@property (nonatomic, retain) UIImage *image;
    63. 63. Properties//Foo.h@property (nonatomic, retain) UIImage *image;@property (nonatomic, copy) NSString *message;
    64. 64. Properties//Foo.h@property (nonatomic, retain) UIImage *image;@property (nonatomic, copy) NSString *message;//Foo.m
    65. 65. Properties//Foo.h@property (nonatomic, retain) UIImage *image;@property (nonatomic, copy) NSString *message;//Foo.m@synthesize image, message;
    66. 66. Properties//Foo.h@property (nonatomic, retain) UIImage *image;@property (nonatomic, copy) NSString *message;//Foo.m@synthesize image, message;-(void)dealloc {
    67. 67. Properties//Foo.h@property (nonatomic, retain) UIImage *image;@property (nonatomic, copy) NSString *message;//Foo.m@synthesize image, message;-(void)dealloc { [image release];
    68. 68. Properties//Foo.h@property (nonatomic, retain) UIImage *image;@property (nonatomic, copy) NSString *message;//Foo.m@synthesize image, message;-(void)dealloc { [image release]; [message release];
    69. 69. 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];
    70. 70. 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];}
    71. 71. Dot Syntax Dogma Use dot syntax if you like it Just be aware of what its hiding
    72. 72. XcodeYour IDECode completionInteractive DebuggerLacks good refactoring tools
    73. 73. Interface Builder Now Part of Xcode 4 Drag-n-drop UI building Layouts are defined in XIBs (XML representation). Usually called "Nibs" "Make connections" with classes defined in Xcode variables --> UI components UI events --> methods
    74. 74. Instruments Find Memory Leaks Analyze Memory Usage Track down slow code
    75. 75. iOS SDK Accelerate CoreMotion AddressBook CoreTelephony AudioToolbox CoreText Your app AVFoundation CoreVideo UIKit CoreAudio GameKit CoreFoundation CoreData iAd CoreGraphics CoreLocation MapKit CFNetwork StoreKit
    76. 76. Model View Controller Model View Controller
    77. 77. The View Controller Handles setup logic for a screen Handles user input Interacts with the model Contains 1 or more views
    78. 78. The ViewVisual representationDrawingLaying out subviews (autorotation)May Handle touch events
    79. 79. Lifecycle of an App main.m
    80. 80. Lifecycle of an App main.m UIApplication
    81. 81. Lifecycle of an App main.m UIApplication
    82. 82. Lifecycle of an App main.m MainWindow.xib UIApplication
    83. 83. Lifecycle of an App main.m MainWindow.xib UIApplication
    84. 84. Lifecycle of an App main.m MainWindow.xib UIApplication YourAppDelegate UIWindow Root View Controller
    85. 85. Lifecycle of an App main.m MainWindow.xib UIApplication YourAppDelegate applicationDidFinishLaunching UIWindow Root View Controller
    86. 86. Time to code!

    ×