ios: 条件绑定的初始化器必须是 Optional 类型,而不是 'LOTAnimationView'
ios: Initializer for conditional binding must have Optional type, not 'LOTAnimationView'
我不断收到此错误:条件绑定的初始化程序必须具有可选类型,而不是 'LOTAnimationView'
在 if let animationView = LOTAnimationView(name: "pop_heart") { 代码行。我认为代码的格式可能是错误的。谁能指导我正确的方向?谢谢:)
override func viewDidLoad() {
super.viewDidLoad()
print("view loaded")
if let animationView = LOTAnimationView(name: "pop_heart") {
animationView.frame = CGRect(x: 0, y: 0, width: 400, height: 400)
animationView.center = self.view.center
animationView.contentMode = .scaleAspectFill
animationView.loopAnimation = true
animationView.animationSpeed = 0.5
view.addSubview(animationView)
animationView.play()
}}
只需从您的代码中删除 if let
。要使用 if let 你必须有一个 Optional
变量。
override func viewDidLoad() {
super.viewDidLoad()
print("view loaded")
animationView = LOTAnimationView(name: "pop_heart")
animationView.frame = CGRect(x: 0, y: 0, width: 400, height: 400)
animationView.center = self.view.center
animationView.contentMode = .scaleAspectFill
animationView.loopAnimation = true
animationView.animationSpeed = 0.5
view.addSubview(animationView)
animationView.play()
}
或者如果你想使用 if let
你可以像这样使用。
if let value = someMethodWhichReturnsAny as? String {
//In above like there is a method which returns a string but in the form of type `Any` so I downcast it to String with optional and remove the nil case with if let
}
//测试
let backgroundAnimation: LOTAnimationView = {
let animationView = LOTAnimationView(name: "10173-success-lumina")
animationView.loopAnimation = true
animationView.contentMode = .scaleAspectFill
return animationView
}()
backgroundAnimation.frame = view.frame
view.addSubview(backgroundAnimation)
view.sendSubviewToBack(backgroundAnimation)
backgroundAnimation.play()
}
我不断收到此错误:条件绑定的初始化程序必须具有可选类型,而不是 'LOTAnimationView' 在 if let animationView = LOTAnimationView(name: "pop_heart") { 代码行。我认为代码的格式可能是错误的。谁能指导我正确的方向?谢谢:)
override func viewDidLoad() {
super.viewDidLoad()
print("view loaded")
if let animationView = LOTAnimationView(name: "pop_heart") {
animationView.frame = CGRect(x: 0, y: 0, width: 400, height: 400)
animationView.center = self.view.center
animationView.contentMode = .scaleAspectFill
animationView.loopAnimation = true
animationView.animationSpeed = 0.5
view.addSubview(animationView)
animationView.play()
}}
只需从您的代码中删除 if let
。要使用 if let 你必须有一个 Optional
变量。
override func viewDidLoad() {
super.viewDidLoad()
print("view loaded")
animationView = LOTAnimationView(name: "pop_heart")
animationView.frame = CGRect(x: 0, y: 0, width: 400, height: 400)
animationView.center = self.view.center
animationView.contentMode = .scaleAspectFill
animationView.loopAnimation = true
animationView.animationSpeed = 0.5
view.addSubview(animationView)
animationView.play()
}
或者如果你想使用 if let
你可以像这样使用。
if let value = someMethodWhichReturnsAny as? String {
//In above like there is a method which returns a string but in the form of type `Any` so I downcast it to String with optional and remove the nil case with if let
}
//测试
let backgroundAnimation: LOTAnimationView = {
let animationView = LOTAnimationView(name: "10173-success-lumina")
animationView.loopAnimation = true
animationView.contentMode = .scaleAspectFill
return animationView
}()
backgroundAnimation.frame = view.frame
view.addSubview(backgroundAnimation)
view.sendSubviewToBack(backgroundAnimation)
backgroundAnimation.play()
}