Successfully reported this slideshow.

Cocoa for Web Developers

4,772 views

Published on

My talk at BarCamp London 5: An introduction to Cocoa development and how web developers can cheat.

Source code to go with this presentation is available here: http://georgebrock.com/conferences/barcamplondon5

Published in: Technology, Business
  • Be the first to comment

Cocoa for Web Developers

  1. 1. Introduction to OS X development with Cocoa … and how web developers can cheat George Brocklehurst http://georgebrock.com @georgebrock on Twitter
  2. 2. Before we start… <ul><li>Have you done any… </li></ul><ul><ul><li>HTML? </li></ul></ul><ul><ul><li>Javascript? </li></ul></ul><ul><ul><li>PHP? </li></ul></ul><ul><ul><li>Any object oriented programming? </li></ul></ul><ul><ul><li>C? </li></ul></ul>
  3. 3. What is Cocoa? <ul><li>Framework for Mac OS X and iPhone </li></ul><ul><li>Heavily MVC focused </li></ul><ul><li>Usually written in Objective-C </li></ul><ul><li>Can also be written in Python or Ruby </li></ul>
  4. 4. Getting Started <ul><li>You will need: Apple’s Xcode Tools </li></ul><ul><li>Available from: </li></ul><ul><ul><li>http://developer.apple.com/technology/xcode.html </li></ul></ul><ul><ul><li>Your OS X install DVDs </li></ul></ul>
  5. 5. Objective C Syntax: Classes <ul><li>Declaring a class interface (.h file) </li></ul><ul><li>@interface MyClass : NSObject </li></ul><ul><li>{ </li></ul><ul><li>int anInteger; </li></ul><ul><li>} </li></ul><ul><li>- (void)doStuff; </li></ul><ul><li>@end </li></ul>
  6. 6. Objective C Syntax: Classes <ul><li>Declaring a class interface (.h file) </li></ul><ul><li>@interface MyClass : NSObject </li></ul><ul><li>{ </li></ul><ul><li>int anInteger; </li></ul><ul><li>} </li></ul><ul><li>- (void)doStuff; </li></ul><ul><li>@end </li></ul>Interface only here!
  7. 7. Objective C Syntax: Classes <ul><li>Declaring a class interface (.h file) </li></ul><ul><li>@interface MyClass : NSObject </li></ul><ul><li>{ </li></ul><ul><li>int anInteger; </li></ul><ul><li>} </li></ul><ul><li>- (void)doStuff; </li></ul><ul><li>@end </li></ul>Class name
  8. 8. Objective C Syntax: Classes <ul><li>Declaring a class interface (.h file) </li></ul><ul><li>@interface MyClass : NSObject </li></ul><ul><li>{ </li></ul><ul><li>int anInteger; </li></ul><ul><li>} </li></ul><ul><li>- (void)doStuff; </li></ul><ul><li>@end </li></ul>Parent class
  9. 9. Objective C Syntax: Classes <ul><li>Declaring a class interface (.h file) </li></ul><ul><li>@interface MyClass : NSObject </li></ul><ul><li>{ </li></ul><ul><li>int anInteger; </li></ul><ul><li>} </li></ul><ul><li>- (void)doStuff; </li></ul><ul><li>@end </li></ul>Members
  10. 10. Objective C Syntax: Classes <ul><li>Declaring a class interface (.h file) </li></ul><ul><li>@interface MyClass : NSObject </li></ul><ul><li>{ </li></ul><ul><li>int anInteger; </li></ul><ul><li>} </li></ul><ul><li>- (void)doStuff; </li></ul><ul><li>@end </li></ul>Methods
  11. 11. Objective C Syntax: Classes <ul><li>Implementing a class (.m file) </li></ul><ul><li>#import “MyClass.h” </li></ul><ul><li>@implementation MyClass </li></ul><ul><li>(void)doStuff </li></ul><ul><li>{ </li></ul><ul><li>// Do stuff here </li></ul><ul><li>} </li></ul><ul><li>@end </li></ul>
  12. 12. Objective C Syntax: Classes <ul><li>Implementing a class (.m file) </li></ul><ul><li>#import “MyClass.h” </li></ul><ul><li>@implementation MyClass </li></ul><ul><li>(void)doStuff </li></ul><ul><li>{ </li></ul><ul><li>// Do stuff here </li></ul><ul><li>} </li></ul><ul><li>@end </li></ul>Interface
  13. 13. Objective C Syntax: Classes <ul><li>Implementing a class (.m file) </li></ul><ul><li>#import “MyClass.h” </li></ul><ul><li>@implementation MyClass </li></ul><ul><li>(void)doStuff </li></ul><ul><li>{ </li></ul><ul><li>// Do stuff here </li></ul><ul><li>} </li></ul><ul><li>@end </li></ul>Start class implementation
  14. 14. Objective C Syntax: Classes <ul><li>Implementing a class (.m file) </li></ul><ul><li>#import “MyClass.h” </li></ul><ul><li>@implementation MyClass </li></ul><ul><li>- (void)doStuff </li></ul><ul><li>{ </li></ul><ul><li>// Do stuff here </li></ul><ul><li>} </li></ul><ul><li>@end </li></ul>Methods with bodies
  15. 15. Objective C Syntax: Methods <ul><li>-(void)myMethod:(int)arg </li></ul><ul><li>In other languages, might be: </li></ul><ul><li>function myMethod(arg) </li></ul>
  16. 16. Objective C Syntax: Methods <ul><li>- (void)myMethod:(int)arg </li></ul><ul><li>Can be either: </li></ul><ul><li>+ for a class method </li></ul><ul><li>- for an instance method </li></ul>Method scope
  17. 17. Objective C Syntax: Methods <ul><li>-( void )myMethod:(int)arg </li></ul><ul><li>Can be any valid data type, including: </li></ul><ul><li>void returns nothing </li></ul><ul><li>id a pointer to an object of any class </li></ul><ul><li>NSString* a pointer to an NSString </li></ul><ul><li>BOOL a boolean ( TRUE , FALSE , YES or NO ) </li></ul>Return type
  18. 18. Objective C Syntax: Methods <ul><li>-(void) myMethod: (int)arg </li></ul><ul><li>Colons precede arguments, but are considered part of the method name </li></ul>Method name
  19. 19. Objective C Syntax: Methods <ul><li>-(void)myMethod: (int)arg </li></ul><ul><li>Come after or within the method name </li></ul><ul><li>For multiple arguments: </li></ul><ul><li>-(void)myMethod: (int)arg andAlso: (int)arg2 </li></ul><ul><li>(Method name is “myMethod:andAlso:”) </li></ul>Argument type Argument name
  20. 20. Objective C Syntax: Methods <ul><li>-(void)myMethod:(int)arg1 </li></ul><ul><li>andAlso:(int)arg2; </li></ul><ul><li>How to call this method: </li></ul><ul><li>[myObject myMethod:10 andAlso:20]; </li></ul><ul><li>In other languages this might be: </li></ul><ul><li>myObject->myMethod(10, 20); //or </li></ul><ul><li>myObject.myMethod(10, 20); </li></ul>
  21. 21. Objective C Syntax: Properties <ul><li>New short hand in Objective-C 2.0 (Xcode 3 / Leopard) </li></ul><ul><li>Access class members without writing getters and setters </li></ul><ul><li>Convenient, but nasty difficult to remember syntax </li></ul>
  22. 22. Objective C Syntax: Properties <ul><li>In the class interface (with methods): </li></ul><ul><li>@property(copy, readwrite) NSString *propertyName; </li></ul><ul><li>In the class implementation: </li></ul><ul><li>@synthesize propertyName; </li></ul><ul><li>To access: </li></ul><ul><li>myInstance.propertyName = @”Foo”; </li></ul>
  23. 23. Objective C Syntax: Properties <ul><li>In the class interface (with methods): </li></ul><ul><li>@property( copy , readwrite) NSString *propertyName; </li></ul><ul><li>In the class implementation: </li></ul><ul><li>@synthesize propertyName; </li></ul><ul><li>To access: </li></ul><ul><li>myInstance.propertyName = @”Foo”; </li></ul>Storage method: assign , retain or copy
  24. 24. Objective C Syntax: Properties <ul><li>In the class interface (with methods): </li></ul><ul><li>@property(copy, readwrite ) NSString *propertyName; </li></ul><ul><li>In the class implementation: </li></ul><ul><li>@synthesize propertyName; </li></ul><ul><li>To access: </li></ul><ul><li>myInstance.propertyName = @”Foo”; </li></ul>Access permissions: readwrite or readonly
  25. 25. Objective C Syntax: Properties <ul><li>In the class interface (with methods): </li></ul><ul><li>@property(copy, readwrite) NSString *propertyName; </li></ul><ul><li>In the class implementation: </li></ul><ul><li>@synthesize propertyName; </li></ul><ul><li>To access: </li></ul><ul><li>myInstance.propertyName = @”Foo”; </li></ul>Can add other things: nonatomic , getter=… and setter=…
  26. 26. Objective C Syntax: Properties <ul><li>In the class interface (with methods): </li></ul><ul><li>@property(copy, readwrite) NSString *propertyName ; </li></ul><ul><li>In the class implementation: </li></ul><ul><li>@synthesize propertyName; </li></ul><ul><li>To access: </li></ul><ul><li>myInstance.propertyName = @”Foo”; </li></ul>Property variable declaration, must also be declared as a class member
  27. 27. Objective C Syntax: Properties <ul><li>In the class interface (with methods): </li></ul><ul><li>@property(copy, readwrite) NSString *propertyName; </li></ul><ul><li>In the class implementation: </li></ul><ul><li>@synthesize propertyName; </li></ul><ul><li>To access: </li></ul><ul><li>myInstance.propertyName = @”Foo”; </li></ul>Tell Objective-C precompiler to make getter & setter
  28. 28. Objective C Syntax: Properties <ul><li>In the class interface (with methods): </li></ul><ul><li>@property(copy, readwrite) NSString *propertyName; </li></ul><ul><li>In the class implementation: </li></ul><ul><li>@synthesize propertyName; </li></ul><ul><li>To access: </li></ul><ul><li>myInstance.propertyName = @”Foo”; </li></ul>
  29. 29. Objective C Syntax: Properties <ul><li>In the class interface (with methods): </li></ul><ul><li>@property(copy, readwrite) NSString *propertyName; </li></ul><ul><li>In the class implementation: </li></ul><ul><li>@synthesize propertyName; </li></ul><ul><li>To access: </li></ul><ul><li>myInstance . propertyName = @”Foo”; </li></ul>Sensible syntax! Yay!
  30. 30. Objective C Syntax: Selectors <ul><li>SEL callback = NULL; </li></ul><ul><li>callback = @selector(myMethod:andAlso:); </li></ul><ul><li>Like function pointers </li></ul><ul><li>Useful for callback type behavior </li></ul>
  31. 31. Objective C Syntax: Selectors <ul><li>SEL callback = NULL; </li></ul><ul><li>callback = @selector(myMethod:andAlso:); </li></ul><ul><li>Like function pointers </li></ul><ul><li>Useful for callback type behavior </li></ul>Data type for selectors
  32. 32. Objective C Syntax: Selectors <ul><li>SEL callback = NULL; </li></ul><ul><li>callback = @selector( myMethod:andAlso: ) ; </li></ul><ul><li>Like function pointers </li></ul><ul><li>Useful for callback type behavior </li></ul>Macro to create selector
  33. 33. Objective C Syntax: Selectors <ul><li>SEL callback = NULL; </li></ul><ul><li>callback = @selector(myMethod:andAlso:); </li></ul><ul><li>Like function pointers </li></ul><ul><li>Useful for callback type behavior </li></ul>
  34. 34. Objective C Syntax: Strings <ul><li>NSString *myString; </li></ul><ul><li>myString = @”This is a string” ; </li></ul><ul><li>Note the @ prefix </li></ul><ul><li>Tells the Objective-C precompiler to instantiate an NSString instead of just creating a C string (aka. A nasty character array) </li></ul>
  35. 35. Managing Memory <ul><li>Easier than vanilla C, honest! </li></ul><ul><li>Reference counting </li></ul><ul><li>Increment with retain , decrement with release </li></ul><ul><li>Rule of thumb: release anything you alloc , copy or retain </li></ul>
  36. 36. Managing Memory: Example <ul><li>NSString *str; </li></ul><ul><li>str = [NSString alloc ]; //1 </li></ul><ul><li>[str retain ]; //2 </li></ul><ul><li>[str release ]; //1 </li></ul><ul><li>[str release ], str = nil; //0 </li></ul>
  37. 37. Managing Memory: Autorelease <ul><li>[str autorelease]; </li></ul><ul><li>This will automatically release an object when it’s finished with </li></ul><ul><li>Another rule of thumb: </li></ul><ul><li>[NSThing thingWith:…] </li></ul><ul><li>returns an autoreleased instance </li></ul><ul><li>[[NSThing alloc] initWith:…] </li></ul><ul><li>needs manually releasing </li></ul>
  38. 38. Wasn’t there something about cheating?! <ul><li>WebKit is part of Cocoa </li></ul><ul><li>Can build your UI with HTML, CSS and Javascript and only use Objective-C when you really need to </li></ul>
  39. 39. Demo
  40. 40. I Can Haz Questions?
  41. 41. K, Thx, Bai! <ul><li>George Brocklehurst </li></ul><ul><li>http://georgebrock.com </li></ul><ul><li>@georgebrock on Twitter </li></ul>

×