我有一个XCDYoutubeKit pod返回的字典字典,它是一个快速的项目:

print(video?.streamURLs ?? "nothing to print")


[AnyHashable(17): https://r7---sn-8vv-cg0l.googlevideo.com/videoplayback?source=youtube&pcm2cms=yes&pl=20&mv=m&initcwndbps=2522500&ipbits=0&ms=au&key=yt6&signature=373890DBE71537EB460B6D5E0C6D6B11066DAAC7.6E0898B1C97F6504432B4A2E96511203B86AFF89&mime=video%2F3gpp&ip=212.68.230.6&dur=275.388&id=o-AALDaM6KxpA6-UX-Zfhcy0MYtZSEWxj3HGfu-aPvZR5z&upn=Wolarcf9l8k&mt=1478956785&mn=sn-8vv-cg0l&itag=17&requiressl=yes&expire=1478978548&lmt=1394298238781402&mm=31&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpcm2cms%2Cpl%2Crequiressl%2Csource%2Cupn%2Cexpire&ratebypass=yes, AnyHashable(135): https://r7---sn-8vv-cg0l.googlevideo.com/videoplayback?pl=20&initcwndbps=2522500&ipbits=0&key=yt6&ip=212.68.230.6&upn=IqqzaXYA2Ag&expire=1478978548&lmt=1458192852355410&mm=31&clen=26845311&source=youtube&pcm2cms=yes&mv=m&mt=1478956785&ms=au&mn=sn-8vv-cg0l&mime=video%2Fmp4&gir=yes&dur=275.008&id=o-AALDaM6KxpA6-UX-Zfhcy0MYtZSEWxj3HGfu-aPvZR5z&signature=86328F117AEF617380C2EE4C0327891683F8571A.C57F883610B44498FEBDF556868860687223BDF2&itag=135&requiressl=yes&sparams=clen%2Cdur%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpcm2cms%2Cpl%2Crequiressl%2Csource%2Cupn%2Cexpire&ratebypass=yes]

我正在尝试比较键,但是遇到了看起来很简单的问题……我尝试过的是:
                for item in (video?.streamURLs)! {

                if item.key == AnyHashable(UInt(17)) {
                    //never pass here
                    print(item.key)
                }
            }

我不熟悉新的AnyHashable类型,但据我了解,它应该允许==运算符。
Apple doc

任何帮助将不胜感激,谢谢!

最佳答案

这是AnyHashable acknowledged by Joe Groff, Swift engineer中的错误:

@ 0xced看起来该错误在AnyHashable中。如果可以将您的字典键入NSDictionary,则可能会变通

您必须显式使用NSNumber来使用Swift 3访问streamURLs字典中的值。

struct YouTubeVideoQuality {
    static let hd720 = NSNumber(value: XCDYouTubeVideoQuality.HD720.rawValue)
    static let medium360 = NSNumber(value: XCDYouTubeVideoQuality.medium360.rawValue)
    static let small240 = NSNumber(value: XCDYouTubeVideoQuality.small240.rawValue)
}

func playVideo(videoIdentifier: String?) {
    let playerViewController = AVPlayerViewController()
    self.present(playerViewController, animated: true, completion: nil)

    XCDYouTubeClient.default().getVideoWithIdentifier(videoIdentifier) { [weak playerViewController] (video: XCDYouTubeVideo?, error: Error?) in
        if let streamURLs = video?.streamURLs, let streamURL = (streamURLs[XCDYouTubeVideoQualityHTTPLiveStreaming] ?? streamURLs[YouTubeVideoQuality.hd720] ?? streamURLs[YouTubeVideoQuality.medium360] ?? streamURLs[YouTubeVideoQuality.small240]) {
            playerViewController?.player = AVPlayer(url: streamURL)
        } else {
            self.dismiss(animated: true, completion: nil)
        }
    }
}

关于swift3 - 无法在Swift 3中比较AnyHashable,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40563476/

10-11 00:07