Swift Playground - “Use of unresolved identifier 'myClass'” but still compiles

五迷三道 提交于 2020-01-11 13:11:04

问题


I am currently experimenting in Swift Playgrounds, and am trying out SpriteKit. My playground is working as expected, and runs, but Xcode has an error saying "Use of unresolved identifier 'myClass'". The playground still runs perfectly still though.

Here is my code:

import UIKit
import PlaygroundSupport
import SpriteKit
let frame = CGRect(x: 0, y: 0, width: 800, height: 600)
let mySKScene = myClass()
let view = SKView(frame: frame)
view.presentScene(mySKScene)

PlaygroundPage.current.liveView = view

class myClass: SKScene {
    override func didMove(to view: SKView) {

    }
}

Thanks in advance for any help.

P.S. I am using the iOS SKD


回答1:


myClass has not been defined at the time you are trying to user it. Change your code to look like this:

import UIKit
import PlaygroundSupport
import SpriteKit

class myClass: SKScene {
    override func didMove(to view: SKView) {

    }
}

let frame = CGRect(x: 0, y: 0, width: 800, height: 600)
let mySKScene = myClass()
let view = SKView(frame: frame)
view.presentScene(mySKScene)

PlaygroundPage.current.liveView = view

Just a note about styling. Classes are usually declared with the first letter being capitalized.(ie. class MyClass: SKScene {})



来源:https://stackoverflow.com/questions/42728404/swift-playground-use-of-unresolved-identifier-myclass-but-still-compiles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!