Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

multinode

macrumors regular
Original poster
Feb 4, 2011
150
0
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.
 
Last edited:
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.
 
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.

Thanx ... so when should I use the first way and when should I use the second way?
 
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.
 
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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.