tags:

views:

240

answers:

2

Hi,which is the best in the following?

-(NSString *)send
{
  NSString *str = @"hai";
  return [str retain];
}
-(NSString *)send
{
  NSString *str = @"hai";
  return [[str retain] autoRelease];
}
+2  A: 

This one's better:

-(NSString *)send {
return @"hai";
}

Your second suggestion, i.e [retain + aurorelease], is meaningless . And although there's nothing wrong with doing so, first one is not good in naming convention: it returns a retained instance although the method doesn't describe it's doing so with including a copy/retain/alloc in its methodname.

ahmet emrah
suppose if i get very long string fron NSUrlrequest,(not assigning directly,i have to allocat memory), what is the best way?
Mikhail Naimy
@senthilmuthu, then you use [NSString stringWith....].
Michael Aaron Safyan
+2  A: 

Generally you should return autoreleased objects, unless there's some good reason not to. This way the caller is never responsible with releasing objects it's not allocating itself. If the string you're getting from some other function is not an autoreleased one, than by all means call [autorelease] on it.

MihaiD