BnJ
BnJ

Reputation: 1044

EXC_BAD_ACCESS after didSelectRowAtIndexPath

I need help with an EXC_BAD_ACCESS during the didSelectRowAtIndexPath function in an UIViewController.

Properties :

@property (strong, nonatomic) Album *selectedAlbum;
@property (strong, nonatomic) NSMutableArray *artistAlbums;
@property (weak, nonatomic) IBOutlet UITableView *albums;

Loading the NSMutableArray "linked" to the UITableView :

- (void) loadAlbums {

    self.artistAlbums = [self.albumService getAlbumsFromDatabase];

    for (Album * album in self.artistAlbums) {
        // No error
        NSLog(album.title);
        NSLog(album._id);
    }
}

AlbumService, getAlbumsFromDatabase :

- (NSMutableArray *)getAlbumsFromDatabase: {

    NSMutableArray * result = [@[] mutableCopy];

    // FavAlbumDpo.id is NSNumber
    for (FavAlbumDpo *albumdpo in [self.databaseManager getAllAlbums]) {
        Album *album1 = [[Album alloc]init];
        album1._id = [albumdpo.id stringValue];
        album1.title = albumdpo.title;
        [result addObject:album1];
    }

    return result;
}

Function didSelectRowAtIndexPath :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.selectedAlbum = self.artistAlbums[indexPath.row];
    for (Album * album in self.artistAlbums) {
        NSLog(album.title); // Print the title
        NSLog(album._id); // EXC_BAD_ACCESS
    }
    [self performSegueWithIdentifier:SEGUE_ID sender:self];
}

Album.h :

@interface Album : NSObject
@property (strong, nonatomic) NSString *title;
@property (assign, nonatomic) NSString *_id;
@end

Album.m :

@implementation Album
- (instancetype)init{
    self = [super init];
    if (self){
        self.title = @"title";
        self._id = @"id";
    }

    return self;
}
@end

Upvotes: 0

Views: 54

Answers (1)

Avt
Avt

Reputation: 17043

Your _id's property string is deallocated. Change

@property (assign, nonatomic) NSString *_id;

to

@property (strong, nonatomic) NSString *_id;

Upvotes: 1

Related Questions