$30 off During Our Annual Pro Sale. View Details »

Objective-C for C# Devs

Objective-C for C# Devs

My evolved talk on intro to Obj-C and iOS for .NET savvy people.

Mark Wilkinson

March 18, 2013
Tweet

More Decks by Mark Wilkinson

Other Decks in Technology

Transcript

  1. Obj-C for C# devs
    try to think different

    View Slide

  2. Me
    Mark Wilkinson
    UH grad in EE
    Former DotNetter, now full-
    time mobile and rails developer.
    Head of the C#UG for the last
    2 years.

    View Slide

  3. My Apps

    View Slide

  4. Requirements

    View Slide

  5. #1 A mac

    View Slide

  6. It’s free.

    View Slide

  7. free to sign up and start coding to the
    simulator, $99/yr to deploy an app

    View Slide

  8. Objective-C
    Created in 1986 by Tom Love and Brad Cox
    Marriage of C with Smalltalk
    Dynamic language, runs natively not on/in a virtual
    machine.
    Simply put, it’s a superset of C, with OO and
    dynamic features.
    Steve Jobs at Next adopted it, later Mac OSX was
    based on it.

    View Slide

  9. Language Basics
    It is C, so you can use all the features and libraries available
    to C.
    Basic building blocks:
    Classes, defined by interfaces
    Value types (Structs, primitives, etc.)
    Pointers are needed when referring to objects.
    Function pointers are achieved through Blocks
    pointers...

    View Slide

  10. really a message
    Calling a method

    View Slide

  11. Messaging?
    Yes, in the dynamic world you’re sending a message
    to an object, and at runtime the system will try to
    find a method (or property) with that matching
    name and params to then execute.
    A good analogy is to think of a class as a
    dictionary, with a key-value system.

    View Slide

  12. Method signature
    return type method name
    method type
    param 1
    param 2
    method body

    View Slide

  13. reads as, Add num1
    with num2

    View Slide

  14. Fluent reading built in

    View Slide

  15. Making a class
    Start with an interface file
    The @ symbol
    Declare properties, methods and instance
    vars (optional)

    View Slide

  16. Make an implementation file
    Notice the #import statements, there
    are no namespaces in Obj-C.
    What we’re used to. Remember, no
    need for interface in C# to create a
    class

    View Slide

  17. must be defined in
    the interface
    Implementation
    follows standard
    practice of setting
    self to result of base
    init
    must return self
    Constructors are called Initializers in
    Obj-C

    View Slide

  18. Allocating and Initializing a Class
    must call alloc first
    on the class
    then call init with
    the result of alloc
    custom initializer
    id can be used to
    represent any type

    View Slide

  19. What would a OO language be without
    inheritance?
    Syntax is the same
    as C#
    goes in the interface
    section not
    implementation

    View Slide

  20. Notes on Obj-C inheritance
    It’s like (if not the same) as Ruby.
    if you want to override a base method, simply name
    it the same, have the same return type and params.
    No need to mark things virtual, or use the new
    keyword, override, etc.
    Inheritance is everywhere in Objective-C/Cocoa,
    where as in C# interfaces and Composition are
    encouraged.

    View Slide

  21. Back to methods
    - indicates the method is an instance method
    + indicates a “class” method, static in the C# lingo.
    All methods and properties in the interface section
    are visible to the caller or subclass.
    @private at the top of the implementation section can
    list methods you want to be private.

    View Slide

  22. Properties
    Here’s the equivalent of auto implemented
    properties in C#.
    tells compiler to
    create getter and
    setters
    attributes type variable name

    View Slide

  23. Extending a class
    How is it done in C#?
    Objective-C calls them Categories, they work in the
    same way extending in Ruby works (almost).
    ( catName ) tells
    compiler it’s a
    category
    will add DoFoo to
    any instance of Base

    View Slide

  24. We love interfaces in C#

    View Slide

  25. Protocols in Obj-C

    View Slide

  26. can inherit another
    protocol
    Can “conform” to
    many protocols
    Can set adoption to
    optional or required

    View Slide

  27. If @required or @optional are not marked above methods in the protocol (the
    2 can be mixed), then it’s required by default.
    if the method was marked @optional, then the adopter/conformer doesn’t
    have to implement the method
    But caller of the class needs to check if the method is implemented
    use reflection to test
    conformity and implementation
    of an optional method

    View Slide

  28. There is no garbage
    collection on iOS

    View Slide

  29. Memory mgt. at 1000ft
    Pre-ARC:
    you had to release an obj when
    you were finished
    retain it to increase the retain
    count so it won’t be deallocated

    View Slide

  30. You were in charge of managing objects on your
    own with the pre-defined rules on when to retain,
    release and/or set to nil.
    Doing this incorrectly could cause memory leaks.
    iOS 5 brought ARC, or Automatic Reference
    Counting.
    Simply put, the compiler adds the appropriate
    release, retains where they’re needed.

    View Slide

  31. Cool things in
    Obj-C

    View Slide

  32. C# eventually had the dynamic keyword, Obj-C has
    this by nature, it’s called id

    View Slide

  33. Message
    Forwarding

    View Slide

  34. nil is special
    desc will now be nil, no exception is
    thrown.
    can’t do this in C#, unless it was a
    bool
    if statements fail if condition is nil
    or null (works on value and object
    types)

    View Slide

  35. KeyValueObserving
    This is a feature added by the Foundation
    framework, but it’s cool enough to be on the list
    *”KVO provides a mechanism that allows objects to
    be notified of changes to specific properties of other
    objects.” - Apple documentation

    View Slide

  36. call addObserver method
    from NSObject to setup the
    KVO
    implement observeValueForKeyPath
    to complete the functionality
    method will catch all changes of
    any properties, so set logic for
    change you’re interested in

    View Slide

  37. Blocks => Lambdas
    In Obj-C we have blocks to create anonymous
    functions, used in the same way anon/lambdas are
    used in C#.

    View Slide

  38. C# Obj-C
    interface protocol
    ext. method category
    method call message send
    TRUE YES
    FALSE NO
    null nil (for objects) null (for values)
    this self
    static method class method
    Anonymous method Block

    View Slide

  39. There’s not an equivalent to Generics in C#. Mainly
    because of the dynamic nature, much like Ruby.
    Obj-C discourages method chaining, because it
    becomes more unreadable unlike other languages.
    Less hand holding than what VS and the C#
    compiler offers.
    Like Rails, CocoaTouch has MVC baked in, it’s the
    preferred way to build applications.
    Because iOS apps generally aren’t enterprise, you
    won’t see complex DDD layers and architectures.

    View Slide

  40. Cocoapods is the NuGet/Gems equivalent.
    The standard Unit Test framework that ships is
    called OCUnit. Much like NUnit, JUnit, etc.
    There’s also plenty of BDD, Specification testing
    options as well.
    Apple documentation is great, much better than
    MSDN.

    View Slide

  41. View Slide