[英]AVPlayer cannot assign value of type to type
I recently upgraded my code to swift 3, and I have 2 errors that relate to the AVPlayer in my app. 我最近将代码升级到了3,而我的应用中有2个与AVPlayer有关的错误。
EDIT: Here are the declarations: 编辑:这是声明:
public var avPlayer = AVPlayer()
public var avPlayerItem = AVPlayerItem?.self
Here's the function I'm referencing: 这是我要参考的功能:
func stream() {
let urlString = streamURLForSong
let urlItem = URL(string: urlString)
avPlayerItem = AVPlayerItem(url: urlItem!) // ERROR: Cannot assign value of type 'AVPlayerItem' to type 'AVPlayerItem?.Type?'
avPlayer = AVPlayer(playerItem: avPlayerItem!) // ERROR: Cannot convert value of type 'AVPlayerItem?.Type' (aka 'Optional<AVPlayerItem>.Type') to expected argument 'AVPlayerItem?'
}
This worked before, but after upgrading to Swift 3 using Xcode, the errors commented above are shown. 以前可以这样做,但是在使用Xcode升级到Swift 3之后,将显示上面注释的错误。
How do I fix this? 我该如何解决?
Thanks in advance! 提前致谢!
I was able to get this function to compile with slight modification in an Xcode playground. 我能够在Xcode游乐场中对该函数进行稍加修改即可进行编译。 My version looked like this:
我的版本如下所示:
import AVFoundation
func stream() {
let urlString = "http://google.com"
let urlItem = URL(string: urlString)
let avPlayerItem: AVPlayerItem? = AVPlayerItem(url: urlItem!) // ERROR: Cannot assign value of type 'AVPlayerItem' to type 'AVPlayerItem?.Type?'
let avPlayer = AVPlayer(playerItem: avPlayerItem) // ERROR: Cannot convert value of type 'AVPlayerItem?.Type' (aka 'Optional<AVPlayerItem>.Type') to expected argument 'AVPlayerItem?'
}
I made urlString a dummy address, and also changed the last line so that we don't force unwrap your AVPlayerItem
when passing it to AVPlayer
. 我将urlString设置为虚拟地址,还更改了最后一行,以免在将
AVPlayerItem
传递给AVPlayer
时不强制拆开它。
Edit: 编辑:
If you're trying to tell Swift what data type to use for avPlayerItem
, you want to use this syntax: 如果您试图告诉Swift将哪种数据类型用于
avPlayerItem
,则需要使用以下语法:
public var avPlayerItem: AVPlayerItem
What you've done is assign the class itself as the contents of the avPlayerItem
variable, so the data type is actually AVPlayerItem?.Type?
您要做的是将类本身分配为
avPlayerItem
变量的内容,因此数据类型实际上是AVPlayerItem?.Type?
instead of AVPlayerItem?
而不是
AVPlayerItem?
. 。
This code is wrong: 这段代码是错误的:
public var avPlayerItem = AVPlayerItem?.self
Delete .self
. 删除
.self
。 That code makes this a type whereas you want an instance to go here. 该代码使它成为一种类型,而您希望实例进入此处。 Now delete
=
and use :
instead, to declare the type. 现在删除
=
并使用:
来声明类型。
To give a simpler example, this code is illegal: 举一个简单的例子,这段代码是非法的:
var x = String?.self
x = "howdy" // compile error
This is what is needed: 这是需要的:
var x : String?
x = "howdy"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.