2016-09-22 38 views
0

無法轉換類型爲Void(又名「()」到期望的參數類型「unsafeRawPointer」Spritekit | SWIFT 3 | - 無法轉換爲void類型值(又名「()」到期望的參數類型「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 
     } 
    } 
} 

有人知道我爲什麼得到這個以及如何解決錯誤嗎?

回答

1

也許你很困惑(void *)...在Objective-C中鑄造和在Swift中鑄造as! Void。他們完全不同。

由於UITouchHashable,您可以使用它作爲Swift的鑰匙Dictionary。 嘗試使用它:

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這種用法,但正如你說你的Objective-C版本使用NSValue實際工作,我上面的代碼應該工作。

+0

很好。發揮魅力。謝謝 – user3482617

相關問題