I'm working with MPMediaQuery and specifically Podcasts. I'm having a very difficult time with Optionals and wrapping/unwrapping in this particular case.
titleFilter = MPMediaPropertyPredicate(value: selectedPodcastTitle, forProperty: MPMediaItemPropertyPodcastTitle, comparisonType: .equalTo)
qryPodcasts.addFilterPredicate(titleFilter)
for junk in qryPodcasts.items!{
//works fine
if let sTitle = junk.title {
print("episode title: \(sTitle)")
}else{
print("episode title is nil")
}
//works fine
if let dRelease = junk.releaseDate {
print("episode release date: \(dRelease)")
}else{
print("episode release date is nil")
}
if #available(iOS 10.0, *) {
// can't figure out how to make this work
// crashes on some podcasts and I suspect it's when 'addedDate' is nil
if let dAdded: Date = junk.dateAdded {
print("episode added date: \(junk.dateAdded)")
}else{
print("episode added date is nil")
}
} else {
print("episode added date is not available")
}
}
Seems the nullability of dateAdded
is wrongly annotated in the current iOS SDK, and you may need some workaround.
Try this extension:
extension MPMediaItem {
func getAddedDate() -> Date? {
return self.perform(#selector(getter: MPMediaItem.dateAdded))?.takeUnretainedValue() as! NSDate? as Date?
}
}
And use it like:
if let dAdded = junk.getAddedDate() {
print("episode added date: \(dAdded)")
}else{
print("episode added date is nil")
}
You'd better send a bug report to Apple about this issue. (It may be classified as duplicate, but the number may affect.)