我正在尝试为在Swift 3.0中工作的CocoaPods获取“Swift-YouTube-Player”广告连播,并且我在以下代码中遇到以下错误(错误指向let pathComponents部分)。
“用于条件绑定(bind)的初始化程序必须具有可选类型,而不是'[String]'”
public func videoIDFromYouTubeURL(_ videoURL: URL) -> String? {
if let host = videoURL.host, let pathComponents = videoURL.pathComponents , pathComponents.count > 1 && host.hasSuffix("youtu.be") {
return pathComponents[1]
}
return videoURL.queryStringComponents()["v"] as? String
}
最佳答案
看来videoURL.pathComponents
不是Optional,所以您必须分解如下条件:
public func videoIDFromYouTubeURL(_ videoURL: URL) -> String? {
if let host = videoURL.host {
let pathComponents = videoURL.pathComponents
if pathComponents.count > 1 && host.hasSuffix("youtu.be") {
return pathComponents[1]
}
}
return videoURL.queryStringComponents()["v"] as? String
}
关于ios - “Initializer for conditional binding must have Optional type, not ' [String] '”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39486299/