ios - didSelectRowAtIndexPath 之后的 EXC_BAD_ACCESS

标签 ios objective-c uitableview

我在 UIViewControllerdidSelectRowAtIndexPath 函数中需要有关 EXC_BAD_ACCESS 的帮助。

属性:

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

加载“链接”到 UITableViewNSMutableArray:

- (void) loadAlbums {

    self.artistAlbums = [self.albumService getAlbumsFromDatabase];

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

相册服务,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;
}

函数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

相册.m :

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

    return self;
}
@end

最佳答案

您的 _id 的属性字符串已被释放。变化

@property (assign, nonatomic) NSString *_id;

@property (strong, nonatomic) NSString *_id;

关于ios - didSelectRowAtIndexPath 之后的 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28865666/

相关文章:

ios - 断点和日志根本不起作用

ios - 使用超过 1 个 Prototype Cell 时 UITableView 的奇怪行为

javascript - iOS 5 - 编译用于调试的私有(private) API - 特别是 _enableRemoteInspector

ios - iOS 开发和分发证书是否共享相同的用户 ID (UID)?

objective-c - 为什么 waitUntilAllOperationsAreFinished 不在此代码中等待?

ios - 当我有多个 iOS 部分时从 UITableView 中删除行(值在 Dynamic NSMutableDictionary 中)

ios - UITableViewCell 删除内容

ios - 为什么我的 UITableView 的顶部有额外的填充,在 iOS7 中具有 UITableViewStyleGrouped 样式

ios - UISearchbar 解析 API

ios - Google 将 ios 的自动完成设置为 100% 的时间都无法正常工作