0
votes

Cannot convert value of type Void (aka'()' to expected argument type 'unsafeRawPointer'

The code allows me to draw lines on the screen by touch. I am using a dictionary to allow multiple the user to draw multiple lines simultaneously.

Its just when I am trying to store the pointers in a NSValue key.

This was working fine in Objective C before I started upgrading to Swift 3.

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

Does anybody know why I am getting this and how I can resolve the error ?

1

1 Answers

1
votes

Maybe you are confusing (void *)... casting in Objective-C and as! Void in Swift. They are completely different.

As UITouch is Hashable, you can use it as a Key of Swift Dictionary. Try using it:

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

I didn't know this usage of UITouch, but as you say your Objective-C version using NSValue actually works, my code above should work.