Obj-C, how do I release a NSString return value?
I'm trying to gain a NSString value from my database in a function, ho开发者_运维百科wever I keep get analyzer warnings. Heres my code...
- (NSString*)getCategoryDesc:(int)pintCid {
NSString *ret;
ret = value from my db ...
return [ret autorelease];
}
It doesn't like
return ret;
return [ret retain];
The key point (which you're not showing us) is: what does "value from my db" do?
If it's doing something like:
ret = [[NSString alloc] initWithString:@"something"];
then you are responsible for releasing the object, but if it's doing something like
ret = [NSString stringWithString:@"something"];
you do not need to release it (and, indeed, you must NOT).
Because your method is named "get*" (and not "create*"), you need to return an object that the caller does not own (generally, that means an autoreleased object).
Read up on the object ownership policy rules.
精彩评论