实例成员 'view' 不能用于类型 'GameScene' [英] Instance member 'view' cannot be used on type 'GameScene'
问题描述
我最近更新到 Xcode 7 Beta,现在我收到一条错误消息实例成员 'view' 不能用于第 5 行的类型 'GameScene'.有人知道如何解决这个问题吗?另外,如果你想更有帮助,请参阅我的其他问题:
您的第一个问题是在使用惰性变量时需要显式声明属性的类型.其次,在使用惰性属性时需要显式引用 self:
惰性变量标签:UILabel =UILabel(frame: CGRect(x: self.view!.scene!.frame.maxX, y: 5, width: 5, height: 5))
您可以通过不使用 view
来访问 scene
来稍微清理一下 - 您已经获得了对 scene
的引用- 这是自我
!
惰性变量标签:UILabel =UILabel(frame: CGRect(x: self.frame.maxX, y: 5, width: 5, height: 5))
I recently updated to Xcode 7 Beta and now I am getting an error message "Instance member 'view' cannot be used on type 'GameScene' for line 5. Anyone got any ideas how to resolve this? Also if you want to be extra helpful, see my other question: ConvertPointToView Function not working in Swift Xcode 7 Beta
import SpriteKit
class GameScene: SKScene {
var titleLabel: StandardLabel = StandardLabel(x: 0, y: 0, width: 250, height: 80, doCenter: true, text: "Baore", textColor: UIColor.redColor(), backgroundColor: UIColor(white: 0, alpha: 0), font: "Futura-CondensedExtraBold", fontSize: 80, border: false, sceneWidth: view.scene.frame.maxX)
override func didMoveToView(view: SKView) {
self.scene?.size = StandardScene.size
self.view?.addSubview(titleLabel)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in (touches ) {
let location = touch.locationInNode(self)
}
}
override func update(currentTime: CFTimeInterval) {
}
}
Your problem is you're using self
before your GameScene
instance has been fully initialised. If you take a look at the end of line 5:
var titleLabel = StandardLabel(..., sceneWidth: view.scene.frame.maxX)
// Would be a good idea to use `let` here if you're not changing `titleLabel`.
Here you're referencing self.view
.
To solve this I would lazily initialise titleLabel
:
lazy var titleLabel: StandardLabel = StandardLabel(..., sceneWidth: self.view!.scene.frame.maxX)
// You need to explicitly reference `self` when creating lazy properties.
// You also need to explicitly state the type of your property.
From The Swift Programming Language: Properties, on Lazy Stored Properties:
A lazy stored property is a property whose initial value is not calculated until the first time it is used.
Therefore, by the time you use titleLabel
in didMoveToView
, self
has been fully initialised and it's safe to use self.view!.frame.maxX
(see below for how to achieve the same result without needing to force unwrap view
).
Edit
Taking a look at the picture of your error:
Your first problem is you need to explicitly state the type of the property when using lazy variables. Secondly you need to explicitly reference self when using lazy properties:
lazy var label: UILabel =
UILabel(frame: CGRect(x: self.view!.scene!.frame.maxX, y: 5, width: 5, height: 5))
You could clean this up a bit though by not using view
to get to scene
- you've already got a reference to scene
- it's self
!
lazy var label: UILabel =
UILabel(frame: CGRect(x: self.frame.maxX, y: 5, width: 5, height: 5))
这篇关于实例成员 'view' 不能用于类型 'GameScene'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!