我读到此错误来自内存管理问题,例如向已释放的对象发送消息。在第二个“for”部分的第一行中,我在评论“输出关于‘info’数组中歌曲的信息”之后立即收到错误消息。信息数组是否没有存储我在第一个“for”部分中提供的对象?
还有其他想法吗?
MPMediaQuery *query = [[MPMediaQuery alloc] init]; //query iPod library
NSMutableArray *info; //create array to hold songs that fit
NSArray *allSongs = [query collections];
//only add those songs which have not
//been played since last login
for (MPMediaItem *recent in allSongs) {
NSDate *lastPlayed = [recent valueForProperty:MPMediaItemPropertyLastPlayedDate];
BOOL uploadInfo = [[PlayedSinceLastLogin alloc] initWithLastPlayedDateOfSong:lastPlayed] ;
if (uploadInfo == YES) {
[info addObject:recent];
}
}
//output information about songs
//in the 'info' array
for (MPMediaItem *played in info) {
NSString *playCount = [played valueForProperty:MPMediaItemPropertyPlayCount];
NSString *lastPlayed = [played valueForProperty:MPMediaItemPropertyLastPlayedDate];
NSString *songTitle =
[played valueForProperty: MPMediaItemPropertyTitle];
NSString *artistName =
[played valueForProperty: MPMediaItemPropertyArtist];
NSString *albumName =
[played valueForProperty: MPMediaItemPropertyAlbumTitle];
NSLog (@"\n %@ by %@, from album %@, played since last login.\nLast Played:%@.\nTotal Play Count: %@.", songTitle, artistName, albumName, lastPlayed,playCount);
}
最佳答案
看起来你实际上并没有为 info
创建一个 NSMutableArray
的实例来指向,所以错误的访问错误可能是由于试图将一些随机的内存位视为已初始化的对象。
改变
NSMutableArray *info;
到
NSMutableArray *info = [NSMutableArray array];
关于iphone - EXC_BAD_ACCESS 错误,内存管理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6147036/