【问题标题】:Spritekit | SWIFT 3 | - Cannot convert value of type Void (aka'()' to expected argument type 'unsafeRawPointer'精灵包 |斯威夫特 3 | - 无法将 Void 类型的值(aka'()' 转换为预期的参数类型 'unsafeRawPointer'
【发布时间】:2016-09-22 09:47:28
【问题描述】:

无法将 Void 类型的值(aka'()' 转换为预期的参数类型 'unsafeRawPointer'

代码允许我通过触摸在屏幕上画线。我正在使用字典来允许多个用户同时绘制多条线。

只是当我试图将指针存储在 NSValue 键中时。

在我开始升级到 Swift 3 之前,这在 Objective C 中运行良好。

var lines: NSMutableDictionary! 

override func didMove(to view: SKView) {
    lines = NSMutableDictionary.init(capacity: 4)
}

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {        
    for t in touches {
        let location: CGPoint = t.location(in: self)
        // Create a mutable path
        let path: UIBezierPath = UIBezierPath()
        path.move(to: location)
        // Create a shape node using the path
        lineNode = SKShapeNode(path: path.cgPath)
        lineNode.name = "sprite\(i)"
        self.addChild(lineNode)

        // Use touch pointer as the dictionary key. Since the dictionary key must conform to
        // NSCopying, box the touch pointer in an NSValue
        var key = NSValue(pointer: (t as! Void))  -- **ERROR HERE**
        lines[key] = lineNode 
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for t in touches {
        let location = t.location(in: self)

         // Retrieve the shape node that corresponds to touch
         let key = NSValue(pointer: (touches as! Void))  -- **ERROR HERE**
         lineNode = (lines[key] as! String)

        if lineNode != nil {
            // Create and initial a mutable path with the lineNode's path
            let path = UIBezierPath(cgPath: lineNode.path!)
            // Add a line to the current touch point
            path.addLine(to: location)
            // Update lineNode
            lineNode.path = path.cgPath
            lineNode.physicsBody = SKPhysicsBody(edgeChainFrom: path.cgPath)
            lineNode.name = lineNodeCategoryName
        }
    }
}

有谁知道我为什么会得到这个以及如何解决这个错误?

【问题讨论】:

    标签: ios sprite-kit swift3


    【解决方案1】:

    也许你混淆了 (void *)... 在 Objective-C 中的转换和 as! Void 在 Swift 中的转换。它们完全不同。

    由于UITouch 是Hashable,您可以将其用作Swift Dictionary 的Key。 尝试使用它:

    var lines: [UITouch: SKShapeNode] = [:]
    
    override func didMove(to view: SKView) {
        lines = Dictionary(minimumCapacity: 4)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for t in touches {
            let location: CGPoint = t.location(in: self)
            // Create a mutable path
            let path: UIBezierPath = UIBezierPath()
            path.move(to: location)
            // Create a shape node using the path
            let lineNode = SKShapeNode(path: path.cgPath)
            lineNode.name = "sprite\(i)"
            self.addChild(lineNode)
    
            lines[t] = lineNode
        }
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for t in touches {
            let location = t.location(in: self)
    
            // Retrieve the shape node that corresponds to touch
            if let lineNode = lines[t] {
                // Create and initial a mutable path with the lineNode's path
                let path = UIBezierPath(cgPath: lineNode.path!)
                // Add a line to the current touch point
                path.addLine(to: location)
                // Update lineNode
                lineNode.path = path.cgPath
                lineNode.physicsBody = SKPhysicsBody(edgeChainFrom: path.cgPath)
                lineNode.name = lineNodeCategoryName
            }
        }
    }
    

    我不知道UITouch 的这种用法,但正如你所说的使用NSValue 的Objective-C 版本实际上可以工作,我上面的代码应该可以工作。

    【讨论】:

    • 太棒了。很有魅力。谢谢你
    猜你喜欢
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多