开发者

Objective-C memory management: do I have to release?

开发者 https://www.devze.com 2022-12-25 06:17 出处:网络
I have a method which runs this: Track* track = [[Track alloc] init:[obj objectForKey:@\"PersistentID\"] :[obj objec开发者_高级运维tForKey:@\"Name\"] :[obj objectForKey:@\"Artist\"] :(NSInteger*)[obj

I have a method which runs this:

Track* track = [[Track alloc] init:[obj objectForKey:@"PersistentID"] :[obj objec开发者_高级运维tForKey:@"Name"] :[obj objectForKey:@"Artist"] :(NSInteger*)[obj objectForKey:@"Total Time"] :(NSInteger*)[obj objectForKey:@"Play Count"]];
[self setCurrentTrack:(Track*) track];
[track release];

Do I have to release track?


Yes. You are responsible for release'ing an object anytime you own the object. You own an object any time you send it an alloc, new, copy or retain message. Your currentTrack property should retain the track.


well, you don't HAVE to, but if you don't have to use it anymore then you should. also it's not necessary to have ":(Track*) track];" I personally prefer

[self setCurrentTrack:track];

because it's a more common practice.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号