An Obj C question

Discussion in 'iOS Programming' started by multinode, Mar 12, 2011.

  1. multinode, Mar 12, 2011
    Last edited: Mar 12, 2011

    multinode macrumors regular

    Joined:
    Feb 4, 2011
    #1
    Can someone please tell me the difference between
    Code:
    class method
    
    NSString * = stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc       
    
    Return Value
    A string containing the characters described in cString.
    
    and
    Code:
    instance method
    
    NSString * = [[NSString alloc] initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding
    
    Return Value
    An NSString object initialized using the characters from nullTerminatedCString.
    
    I am assuming that in both cases memory is allocated for the returned NSString object ... is that correct?

    Thank you.
     
  2. yaniv92648 macrumors member

    Joined:
    Oct 26, 2009
    #2
    Differences

    1. Class Method: memory here is allocated but by the class method itself and not directly by u. so u don't release the memory.
    2. Instance Method: memory here is allocated by u directly. so u must release the memory.
     
  3. multinode thread starter macrumors regular

    Joined:
    Feb 4, 2011
    #3
    Thanx ... so when should I use the first way and when should I use the second way?
     
  4. yaniv92648 macrumors member

    Joined:
    Oct 26, 2009
    #4
    Well..

    BTW, class method is called "convenience method" (cuz it takes care of the object releasing 4 u). Apple docs recommend using convenience methods alone. if u must - use instance methods.
     
  5. multinode thread starter macrumors regular

    Joined:
    Feb 4, 2011
    #5
    Thanx Yaniv.
     
  6. dantastic macrumors 6502a

    dantastic

    Joined:
    Jan 21, 2011
    #6
    Is the recommendation really to use convenience methods? I was of the understanding that, at least on the memory constrained iPhone, the preferred way is not to use them.
     
  7. yaniv92648 macrumors member

    Joined:
    Oct 26, 2009
    #7
    i looked it up in the docs..

    but couldn't find it.. :(
    though i remember reading it's better 2 use convenience constructors - i don't c it now. i actually didn't c any preference 2 convenience constructors or alloc.
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8

Share This Page