views:

53

answers:

3

Hi Guys,

These are setter and getters.

-(NSMutableArray*)contactList
{
    return contactList;
}
-(void)setContactList:(NSMutableArray*) aContactList
{
    [contactList release];
    contactList=aContactList; //its working fine but leaks 
    // contactList=[aContactList copy]; 
    //   If I keep like this getting exception as mutating 
    //   object setting to immutable but it is mutable only. 
}

In view controller, in edit funtionality, I am adding new object to the list like this

[tempDetailsObj.contactList addObject:editcontacts];

here I am getting exception as mutating

object setting to immutable but it is mutable only.

If I remove copy its working fine but I am getting Leak, So I want to add the object to the list without any exception and it should not produce any leaks.

Please Help me out of this problem

A: 

I don't know wether it solves the problem, but try out the following

instead of

 [contactList release]; contactList=aContactList;

try recyling the old reference

[contactList removeAllObjects]; [contactList addObjectsFromArray:aContactList];

regards

ReaperXmac
+1  A: 

You can either try -mutableCopy or just using the already existing reference with -removeAllObjects:

-(NSMutableArray*)contactList
{
    return contactList;
}
-(void)setContactList:(NSMutableArray*) aContactList
{
    //   Either this way:
    [contactList removeAllObjects];
    [contactList addObjectsFromArray:aContactList];
    //   Or this way:
    [contactList release];
    contactList = [aContactList mutableCopy];
}
Jacob Relkin
Thank You Very Much
Madan Mohan
+1  A: 

For starters, you are not taking ownership of the parameters passed to your setter. Also you'll want to copy mutable instances to avoid them being changed under you.

Use e.g. this:

- (void)setContactList:(NSMutableArray *)aContactList {
    NSMutableArray *tmp = contactList;
    contactList = [aContactList mutableCopy];
    [tmp release];
}

But then, why not use just use declared properties:

// interface:
@property (nonatomic, copy) NSMutableArray *contactList;

// implementation:
@synthesize contactList;
Georg Fritzsche