Cocoa for Web Developers

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    2 Favorites

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

    + georgebrockgeorgebrock, 2 years ago

    custom

    2214 views, 2 favs, 3 embeds more stats

    My talk at BarCamp London 5: An introduction to Coc more

    More info about this document

    CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

    Go to text version

    • Total Views 2214
      • 2097 on SlideShare
      • 117 from embeds
    • Comments 0
    • Favorites 2
    • Downloads 22
    Most viewed embeds
    • 111 views on http://georgebrock.com
    • 5 views on http://www.georgebrock.com
    • 1 views on http://66.102.9.104

    more

    All embeds
    • 111 views on http://georgebrock.com
    • 5 views on http://www.georgebrock.com
    • 1 views on http://66.102.9.104

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories