1

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")
        }
    }
wayneh
  • 4,393
  • 9
  • 35
  • 70
  • Have you tried setting a breakpoint and following the flow of the program? Maybe there's a side effect of something going on. Also try, instead of using the property accessor, `junk.value(forProperty: the date added key)` and see what that gives you. If it is null or something funky, you may have stumbled into a swift bug. – hola Mar 12 '17 at 06:26
  • I don't see a date added key actually. – hola Mar 12 '17 at 06:34
  • Hope you figure this out, good luck! – hola Mar 12 '17 at 06:35

2 Answers2

1

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.)

OOPer
  • 47,149
  • 6
  • 107
  • 142
  • Thanks, that worked. One thing to note is that "dateAdded" is only available since iOS 10 so I had to wrap both the extension clause and my usage like so: `extension MPMediaItem { @available(iOS 10.0, *) func getAddedDate() -> Date? { return self.perform(#selector(getter: MPMediaItem.dateAdded))?.takeUnretainedValue() as! NSDate? as Date? } }` – wayneh Mar 12 '17 at 15:24
0

After the last discussion, the dateAdded has un-nullable type Date and crash happen when it = nil.

So I recommend it type should Date? and you should unwrapping it.

 if let dAdded = junk.dateAdded {
 }

or check it will nil at this situation.

 if junk.dateAdded != nil {
 }
Luan Tran
  • 1,142
  • 7
  • 15
  • dateAdded is typed as `var dateAdded: Date { get }`. The compiler knows the type. Also it's not a nullable type, but that only gives a warning. – hola Mar 12 '17 at 06:25
  • Did it crash when `dateAdded = nil` right at `let dAdded: Date = junk.dateAdded` ? – Luan Tran Mar 12 '17 at 06:26
  • Regardless of the methods I use it is crashing at the "let" or "if" line. I changed to if junk.dateAdded != nil and the compiler is stating "Comparing non-optional value of type 'Date' to nil always returns true" – wayneh Mar 12 '17 at 06:44
  • Also, if I use `if let dAdded = junk.dateAdded` the compiler complains: "Initializer for conditional binding must have Optional type, not 'Date' " – wayneh Mar 12 '17 at 06:47
  • Yes. Because it's type is un-nullable `Date`. You should change `dateAdded` type to `Date` to `Date?` because it can be nil. Or you have to find out why it nil in init? – Luan Tran Mar 12 '17 at 06:49
  • Can't change to `Date` or `Date?` - compiler complains "Non-optional expression of type 'Date' used in a check for optionals" – wayneh Mar 12 '17 at 06:53
  • Where compiler show that error? Can you update more code where you init the `junk`. – Luan Tran Mar 12 '17 at 07:07