ARC在Method中创建新对象

时间:2013-07-05 23:12:19

标签: cocoa automatic-ref-counting

我刚使用Xcode的工具将一个项目从MRR移到了ARC。我有一个这样的例行程序:

@interface myObject 
{
    NSMutableArray* __strong myItems;
}
@property  NSMutableArray* myItems;
- (BOOL) readLegacyFormatItems;
@end



- (BOOL) readLegacyFormatItems
{
    NSMutableArray* localCopyOfMyItems = [[NSMutableArray alloc]init];
    //create objects and store them to localCopyOfMyItems

    [self setMyItems: localCopyOfMyItems]

    return TRUE;
}

这在MRR下运行良好,但在ARC myItems下立即发布。我怎么能纠正这个?

我已经阅读了__strong和__weak引用,但在这种情况下我还没有看到如何应用它们。

非常感谢所有人提供任何信息!

1 个答案:

答案 0 :(得分:1)

这应该可行。但是你不需要再申报iVars了。只需使用属性。你甚至不需要合成它们。强属性将保留任何已分配的对象,弱属性则不会。

同样,类名应始终为大写。而且 - 由于您存储了一个可变数组 - 您还可以将对象直接添加到属性中。不需要另一个本地可变数组变量。

@interface MyObject 
@property (nonatomic, strong) NSMutableArray *myItems;
- (BOOL)readLegacyFormatItems;
@end


@implementation MyObject

- (BOOL) readLegacyFormatItems
{
    self.myItems = [[NSMutableArray alloc]init];

    //create objects and store them directly to self.myItems

    return TRUE;
}

@end