2016-09-22 3 views
0

にvoid型(別名「()」の値が「unsafeRawPointer」Spritekit | SWIFT 3 | - 期待される引数の型「unsafeRawPointer」

コードが可能に予想される引数の型に(別名「()」void型の値を変換できません変換できません。私はタッチして、画面上に線を描画します。私は、複数のユーザーが同時に複数の線を描画できるようにするために辞書を使用しています。私は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の場合、スウィフト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のこの使用方法を知りませんでしたが、あなたはNSValueを使用して、Objective-Cのバージョンが実際に動作すると言うように、上記の私のコードは動作するはずです。

+0

素晴らしい。魅力を発揮します。ありがとうございました – user3482617

関連する問題