【问题标题】:Swift Playground - Use of unimplemented initializer 'init(size:)' for classSwift Playground - 对类使用未实现的初始化程序“init(size:)”
【发布时间】:2018-09-10 21:22:14
【问题描述】:

我有一个类 Branch,我已经为它实现了我的自定义初始化程序。我收到一条错误消息,指出我没有实现 init?(coder aDecoder) 初始化程序,这是我在此之后实现的。我仍然收到错误提示

Fatal error: Use of unimplemented initializer 'init(size:)' for class 'wwdcGame_Sources.Branch'

我尝试实现初始化程序,但我得到的错误越来越多。我找不到任何其他答案。

非常感谢任何帮助。我在这个问题上坐了很长时间。提前致谢。

Branch.swift

import Foundation
import SpriteKit
import CoreGraphics

public class Branch : SKScene {

    var begin = CGPoint()
    var end = CGPoint()
    var finished: Bool = false

    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    public required init(_ begin: CGPoint,_ end: CGPoint,_ finished: Bool){
        self.begin = begin
        self.end = end
        self.finished = finished
        super.init()
    }

    public func show() {
        var points = [CGPoint(x: begin.x, y: begin.y),
                      CGPoint(x: end.x, y: end.y)]
        let line = SKShapeNode(points: &points,
                                          count: points.count)
        line.lineWidth = 5
        addChild(line)
    }

    public func branchRight() -> Branch {

        let angle : Float = (30 * Float(Double.pi) / 180)
        let point = CGPoint(x: end.x - begin.y , y : end.y - begin.y)
        let dir_x = Float(point.x)
        let dir_y = Float(point.y)

        var rotatedPoint : CGPoint = point
        rotatedPoint.x = CGFloat(dir_x * cosf(angle) - dir_y * sinf(angle)) * 0.67
        rotatedPoint.y = CGFloat(dir_y * cosf(angle) + dir_x * sinf(angle)) * 0.67

        let newEnd = CGPoint(x : end.x + rotatedPoint.x, y: end.y + rotatedPoint.y)
        return(Branch(end,newEnd,false))
    }

    public func branchLeft() -> Branch{

        let angle : Float = -(30 * Float(Double.pi) / 180)
        let point = CGPoint(x: end.x - begin.y , y : end.y - begin.y)
        let dir_x = Float(point.x)
        let dir_y = Float(point.y)

        var rotatedPoint : CGPoint = point
        rotatedPoint.x = CGFloat(dir_x * cosf(angle) - dir_y * sinf(angle)) * 0.67
        rotatedPoint.y = CGFloat(dir_y * cosf(angle) + dir_x * sinf(angle)) * 0.67

        let newEnd = CGPoint(x : end.x + rotatedPoint.x, y: end.y + rotatedPoint.y)
        return(Branch(end,newEnd,false))

    }

}

我在另一个场景中轮流使用这个类。包括这个类以防万一。

FractalTreesScene.swift

import SpriteKit
import Foundation
import CoreGraphics

public class FractalTreesScene: SKScene {

    var tree = [Branch]()
    var leaves = [CGPoint]()

    var count = 0;
    let tree_width = 400
    let tree_height = 0

    override public func didMove(to view: SKView) {
        self.scene!.backgroundColor = SKColor.black

        let a = CGPoint(x: tree_width,y: tree_height)
        let b = CGPoint(x: tree_width,y: tree_height + 100)
        let root = Branch(a,b,false)
        tree[0] = root

        for i in stride(from: tree.count, to: 0, by: -1) {
            if (tree[i].finished == false) {
                tree.append(tree[i].branchRight());
                tree.append(tree[i].branchLeft());
            }
            tree[i].finished = true;
            count += 1;

            if (count == 6) {
                for i in 0..<tree.count {
                    if (!tree[i].finished) {
                        let leaf = tree[i].end
                        leaves.append(leaf);
                    }
                }
            }
        }

        for i in 0..<tree.count {
            tree[i].show()
        }

        for i in 0..<leaves.count {
            let leaf = SKShapeNode(circleOfRadius: 3.0)
            leaf.position = leaves[i]
            leaf.fillColor = .red
            addChild(leaf)
        }

    }

}

编辑:这是我实例化类的 contents.swift 文件

import PlaygroundSupport
import SpriteKit

let view = SKView(frame: CGRect(x: 0, y: 0, width: 800,height:800))

if let scene = FractalTreesScene(fileNamed: "FractalTreesScene") {
    scene.scaleMode = .aspectFill
    view.presentScene(scene)
}

PlaygroundSupport.PlaygroundPage.current.liveView = view

【问题讨论】:

  • 尝试将您的代码示例减少到所需的最低限度。

标签: ios swift sprite-kit swift-playground initializer


【解决方案1】:

覆盖init(size:)

public override init(size: CGSize) {
    super.init(size: size)
}

由于您有自定义初始化程序,您的 Branch 类不会继承 init(size:)

【讨论】:

  • 那个错误消失了,一个新的弹出了error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0). The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.
  • 这是一个操场问题。一旦我在这个线程的帮助下解决了这个问题:forums.developer.apple.com/thread/63228
  • 我尝试使用这些解决方案,我尝试重新启动 xcode 但它仍然抛出该错误
  • 你能提供你实例化和使用这些类的代码吗?
  • 添加了文件,以及我在 FractalTreesScene.swift 文件中使用的 branch.swift
猜你喜欢
  • 2017-03-22
  • 1970-01-01
  • 2019-04-23
  • 1970-01-01
  • 1970-01-01
  • 2014-07-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多