Cocoa 在方法中创建新对象的圆弧
我刚刚使用Xcode的工具将一个项目从MRR转移到ARC。我有一个惯例是这样的:Cocoa 在方法中创建新对象的圆弧,cocoa,automatic-ref-counting,Cocoa,Automatic Ref Counting,我刚刚使用Xcode的工具将一个项目从MRR转移到ARC。我有一个惯例是这样的: @interface myObject { NSMutableArray* __strong myItems; } @property NSMutableArray* myItems; - (BOOL) readLegacyFormatItems; @end - (BOOL) readLegacyFormatItems { NSMutableArray* localCopyOfMyItem
@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下立即发布。我怎样才能纠正这个问题
我读过关于强引用和弱引用的文章,但我还不知道如何在这种情况下应用它们
提前非常感谢所有人提供的任何信息 这应该是可行的。但是你不需要再申报IVAR了。只需使用属性。你甚至不需要合成它们。强属性将保留任何指定的对象,弱属性则不会 此外,类名应始终为大写。而且,由于存储的是可变数组,因此还可以将对象直接添加到属性中。不需要另一个本地可变数组变量
@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
谢谢你的信息。那么也许我所犯的错误就是我最初创建MyObject的方式。当单击某个特定菜单项时,我有一个iAction调用。看起来是这样的:(iAction)importLegacyItems:(id)发送方{myObject*newInstanceOfmyObject;newInstanceOfmyObject=[[myObject alloc]init];[newInstanceOfmyObject readLegacyFormatItems];}我需要做些什么来防止在这个例程结束时释放newInstanceOfmyObject吗?我想我可能已经找到了它--我需要通过windowcontroller创建对象。e、 g.WindowController=[[NSWindowController alloc]initWithWindowNibName:@“MyObject”MyObject];