我在 UIViewController
的 didSelectRowAtIndexPath
函数中需要有关 EXC_BAD_ACCESS
的帮助。
属性:
@property (strong, nonatomic) Album *selectedAlbum;
@property (strong, nonatomic) NSMutableArray *artistAlbums;
@property (weak, nonatomic) IBOutlet UITableView *albums;
加载“链接”到 UITableView
的 NSMutableArray
:
- (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/