VikR
VikR

Reputation: 5142

ARC Creating New Objects in Method

I just moved a project from MRR to ARC using Xcode's tool. I have a routine that works like this:

@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;
}

This worked fine under MRR, but under ARC myItems is immediately released. How can I correct this?

I've read about __strong and __weak references, but I don't yet see how to apply them in this case.

Thanks very much in advance to all for any info!

Upvotes: 0

Views: 92

Answers (1)

calimarkus
calimarkus

Reputation: 9977

This should work, as it is. But you don't need to declare the iVars anymore. Just use properties. You even don't need to synthesize them. Strong properties will retain any assigned object, weak properties won't.

Also class names should always be uppercase. And - since you store a mutable array - you can also add your objects directly to the property. No need for another local mutable array variable.

@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

Upvotes: 1

Related Questions