cyclingIsBetter
cyclingIsBetter

Reputation: 17591

IOS: delete objects from array with crash

I have a genearl object in this way:

.h

@interface Object : NSObject{

    NSNumber *iden;
    NSString *name;
    NSString *address;
}

@property (nonatomic, retain) NSNumber *iden;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *address;

@end

.m

@implementation Object

@synthesize name;
@synthesize iden;
@synthesize address;

- (id) initWithCoder:(NSCoder *)aDecoder {
    self = [super init];

    if (self) {

        self.iden = [aDecoder decodeObjectForKey:@"iden"];
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.address = [aDecoder decodeObjectForKey:@"address"];

    }

    return self;
}

- (void) encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.iden forKey:@"iden"];
    [aCoder encodeObject:self.nome forKey:@"name"];
    [aCoder encodeObject:self.indirizzo forKey:@"address"];
}

- (void) dealloc{
    [super dealloc];
    [name release];
    [iden release];
    [address release];
}

@end

it's work fine and at the same time I have an nsmutablearray where I insert these object called "favorite" With this array "favorite" I populate a tableview and I save it with NSUserDefault in this way:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData* myClassArrayData = [NSKeyedArchiver archivedDataWithRootObject:appDelegate.favorite];
    [defaults setObject:myClassArrayData forKey:@"favorite"];

when I open a second time my app and show my tableview it's all ok but when I try to delete a row of this tableview I have a crash...the reason is the dealloc method inside class object.

if I comment these method:

//- (void) dealloc{
//    [super dealloc];
//    [name release];
//    [iden release];
//    [address release];
//}

I have not a crash and it work fine, what's the solution for this case? Should I comment dealloc method?

Upvotes: 0

Views: 150

Answers (1)

Wayne Hartman
Wayne Hartman

Reputation: 18487

Always, always, always call [super dealloc] last.

On that note, switch to Automatic Reference Counting so you don't have to deal with that anymore.

Upvotes: 4

Related Questions