Instance member 'view' cannot be used on type 'GameScene'
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))
Swift: Instance member cannot be used on type
Try making a method inside the Data class like this .
func getusrid ()-> String
{
var id = (user.userId).string
return id ;
}
You cannot get value from instance variable outside any method
EDIT:
class Data {
static let sharedInstance = Data()
let id:String?
let quarterlyEndpoint: String?
let user = Users()
private init()
{
id = String((user.userId))
quarterlyEndpoint = "http://anyapi/api/users/\(id)/quarterly/tasks"
}
}
Coding SpriteKit game and I am receiving Instance member 'minX' cannot be used on type 'CGRect
minX
, maxX
, etc are instance properties, which mean they need to be called on a CGRect
instance, not the type itself.
Change
CGRect.minX(gameArea)
to
gameArea.minX
Side Note: It's important in Swift (and other languages as well) to understand the distinction between types and instances. In Swift the convention is to name types starting with capital letters and instances starting with lowercase letters.
Your line of code
let CheeseBlock = SKSpriteNode(imageNamed: "CheeseBlock\(randomImageNumber)")
should be changed to
let cheeseBlock = SKSpriteNode(imageNamed: "CheeseBlock\(randomImageNumber)")
This makes it easier to tell which variables are types and which are instances.
What's wrong here: Instance member cannot be used on type
The problem here is that you are using self
before the class is fully initialised. You can either have a getter which will be called every time you access the variable or compute it lazily.
Here is some code:
class TableViewController: UITableViewController {
let mydate = NSDate()
var items : [(Int,Int,Int,String,NSDate)] {
get {
return [
(1, 9, 7, "A", mydate),
(2, 9, 7, "B", mydate),
(3, 9, 7, "C", mydate),
(4, 9, 7, "D", mydate)
]
}
}
}
Lazy computation:
class TableViewController: UITableViewController {
let mydate = NSDate()
lazy var items : [(Int,Int,Int,String,NSDate)] = {
return [
(1, 9, 7, "A", self.mydate),
(2, 9, 7, "B", self.mydate),
(3, 9, 7, "C", self.mydate),
(4, 9, 7, "D", self.mydate)
]
}()
}
instance member baseURL cannot be used on type URL
You're calling baseURL
on the class URL
, not an actual instance of URL
. It would be like trying to eat not an apple, but the word "apple". Try this instead:
func getFileURL() -> URL {
let fileName = (imagePath! as NSString).lastPathComponent
let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let fileURL = URL(fileURLWithPath: dirPath, isDirectory: true).appendingPathComponent(fileName)
return fileURL.baseURL!
}
EDIT: I'm not exactly sure about what you're trying to ask, so you may need to change return fileURL.baseURL!
to return fileURL
.
My GameScene is not positioned correctly after switching scene
Take a look at scene.anchorPoint
, maybe you accidentally changed it. Default is CGPoint(x: 0.5, y: 0.5)
.
My GameScene reloads whenever my Text view updates
Scene is a model, and you create new scene (right in call) on each update, so SpriteView
thinks it should rebuild.
Try the following:
struct ContentView: View {
@EnvironmentObject var mainData: MainData
@State private var scene = GameScene() // << here, or put in MainData !!
var body: some View {
GeometryReader { geo in
Group {
if mainData.showSpriteScene {
SpriteView(scene: scene, // << here !!
transition: SKTransition.reveal(with: .down, duration: 1.0))
.ignoresSafeArea()
Related Topics
How to Compare Range<String.Index> and Defaultbidirectionalindices<String.Characterview>
Writing Data to an Outputstream with Swift 5+
How to Fix Memory Leaks in iOS Applications
How to Replicate Hash_Hmac('Sha256', $Key, $Secret_Key) Function in Swift 4
Implement Protocol Through Extension
Animate an Skspritenode with Textures That Have a Size Different from the Original
Split Paragraphs into Sentences
Scrolling Delegate in Tableview
Swift - Writing a Byte Stream to File
Swift Subclasses Used in Generics Don't Get Called When Inheriting from Nsobject
Button State Activates on Wrong Cells
Tvos Textfield Transparent Background
Swift: Generic Overloads, Definition of "More Specialized"
Creating a Subclass of Skshapenode
Set a Variable to the < ("Less Than") Operator as a Function in Swift