Objective-C Programming Conventions

Discussion in 'Mac Programming' started by coolbeans777, Mar 18, 2011.

  1. coolbeans777 macrumors newbie

    Joined:
    Mar 18, 2011
    #1
    Do you think it is a bad idea to completely defy Objective-C getter conventions by defining a getter method such as origin as getOrigin instead? I really don't like the idea of just writing "origin" for a getter method because it makes it look too much like an instance/class variable and I feel like it makes much more sense to do it my way.
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Use properties, then you don't need to write an accessor yourself, @synthesize will handle it. If you must you can then write a getX method that uses the accessor (or I guess accesses the property directly). If you choose only to implement getX accessors you won't be key-value compliant, you won't be able to use dot-property syntax, and other people using your class will be confused. Why do you want to type more and be specifically unusual compared to existing code? You're going to have to be used to it to use other frameworks anyway, you may as well just do it "right" in your own code.

    -Lee
     
  3. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #3
    You can define your own property accessor names by using the getter and setter attributes of any property.

    Code:
    @property (getter=gimmeThatFoo, setter=stuffThisIntoFooNow:) fooType foo;
    
     
  4. coolbeans777 thread starter macrumors newbie

    Joined:
    Mar 18, 2011
    #4
    That's true.
     
  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    Stay with Obj-C conventions. "get" in a method name has a specific and very different meaning in Obj-C than it does in other languages.
     
  6. coolbeans777 thread starter macrumors newbie

    Joined:
    Mar 18, 2011
    #6
    Didn't know that, I guess I'll definitely stay with Objective-C conventions:apple:
     
  7. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #7
    But "gimmeDa" appears to be safe. :)
     

Share This Page