Your SlideShare is downloading. ×
0
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Cocoa for Web Developers
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Cocoa for Web Developers

4,333

Published on

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

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
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,333
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
52
Comments
0
Likes
4
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Introduction to OS X development with Cocoa … and how web developers can cheat George Brocklehurst http://georgebrock.com @georgebrock on Twitter
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Demo
  • 40. I Can Haz Questions?
  • 41. K, Thx, Bai! <ul><li>George Brocklehurst </li></ul><ul><li>http://georgebrock.com </li></ul><ul><li>@georgebrock on Twitter </li></ul>

×