目标C中的内存管理

Luc*_*uke 0 iphone memory-management objective-c

我有一个关于引用计数的问题.

这是我的构造函数:

- (id)initWithId:(NSString *)graphId;
Run Code Online (Sandbox Code Playgroud)

在另一个类中,我通过以下方式创建一个对象:

GraphViewController *graph =  
[[GraphViewController alloc] initWithId:[[NSString alloc] initWithFormat:@"%s", current_ID];
Run Code Online (Sandbox Code Playgroud)

我的问题是:如何正确释放字符串对象?

释放作为参数传递的字符串是否正确?

bbu*_*bum 6

任何这些方式都可行:

(1)

GraphViewController *graph = [[GraphViewController alloc] initWithId:
    [[[NSString alloc] initWithFormat:@"%s", current_ID] autorelease]];
Run Code Online (Sandbox Code Playgroud)

(2)

GraphViewController *graph = [[GraphViewController alloc] initWithId:
    [NSString stringWithFormat:@"%s", current_ID]];
Run Code Online (Sandbox Code Playgroud)

(3)

NSString *aString = [[NSString alloc] initWithFormat:@"%s", current_ID];
GraphViewController *graph = [[GraphViewController alloc] initWithId: aString];
[aString release];
Run Code Online (Sandbox Code Playgroud)

当然,graph需要在某处释放或自动释放.