-1

有时,在引入弧之前,我没有使用@property 声明,而是只使用 iVar,如下所示:

//Foo.h
@interface Foo : NSObject
{
    NSString *str;
}

- (id)initWithStr:(NSString *)newStr;
..
//Foo.m

- (id)initWithStr:(NSString *)newStr
{
    if(self = [super init])
    {
        str = [newStr retain];
    }
    return self;
}

- (void)dealloc
{
   [str release];
   [super dealloc];
}
...

如果我不想使用@property声明,如何用 ARC 实现这样的事情?

4

2 回答 2

3

弧只是工作:

- (id)initWithStr:(NSString *)newStr
{
    if(self = [super init])
    {
        str = newStr;
    }
    return self;
}

...并且没有 dealloc :)

于 2012-08-31T11:49:16.687 回答
-1

在 .h 文件中声明 str 。

 @property (nonatomic, strong) NSString *str;

您可以在 中合成此变量。m 文件之类的@sythesize str;

您可以self.str用于分配或获取str. 有关更多详细信息,请阅读此文档

于 2012-08-31T11:51:57.793 回答