Iphone 从MRC到ARC

Iphone 从MRC到ARC,iphone,objective-c,ios,macos,automatic-ref-counting,Iphone,Objective C,Ios,Macos,Automatic Ref Counting,有时,在引入arc之前,我没有使用@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 reta

有时,在引入arc之前,我没有使用@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实现类似的功能?

ARC只起作用:

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

。。。并且没有dealloc:)

在.h文件中声明str

 @property (nonatomic, strong) NSString *str;
您可以在中合成此变量。m类文件
@sythesize str

您可以使用
self.str
分配或获取
str

有关更多详细信息,请阅读。

我不想使用@property,我只想使用ivar。如果您不想使用@property等,请不要使用ARC。您可以将您在其中完成此类工作的外资企业排除在ARC之下。比试着让一个方形的钉子装一个圆孔要简单得多。默认情况下,在ARC下str是强的。所以它将被保留。@Bourne在问这个问题之前,我已经测试过这种情况,ref count等于1,而不是12@tikhop对不起,我没意识到。我收回那句话。