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
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))