0

I'm running an MPMediaQuery to find a song in the user's iPod library, like this:

MPMediaQuery *songQuery = [MPMediaQuery songsQuery];

[songQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:track.title forProperty:MPMediaItemPropertyTitle comparisonType:MPMediaPredicateComparisonEqualTo]];

[songQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:track.album forProperty:MPMediaItemPropertyAlbumTitle comparisonType:MPMediaPredicateComparisonEqualTo]];

[songQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:track.artist forProperty:MPMediaItemPropertyArtist comparisonType:MPMediaPredicateComparisonEqualTo]];


*** CRASH HERE ***
if (songQuery.items.count > 0) {

    return [songQuery.items objectAtIndex:0];

}

else {

    return nil;

}

The app crashes and spits out:

'invalid value (nil) for property item_extra.title.'

Am I doing anything wrong? Is there a better way to query for a song? Thanks everyone!

4

1 回答 1

1

在这条线上:

[songQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:track.title forProperty:MPMediaItemPropertyTitle comparisonType:MPMediaPredicateComparisonEqualTo]];

的值为track.titlenil。您需要进行检查,例如:

if (track.title != nil)
{
[songQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:track.title forProperty:MPMediaItemPropertyTitle comparisonType:MPMediaPredicateComparisonEqualTo]];
}

编辑:我建议对您将作为过滤谓词添加的每个变量进行检查,以确保它们也不为零!

于 2014-08-09T01:26:03.353 回答