1

I am porting a game I made in Objective c to swift as it is a good way to learn how to port something and how to mix swift with objective c and objective c with swift.

But after lots of searching I can't find how to port this line of code:

GCControllerDirectionPadValueChangedHandler dpadMoveHandler = 
 ^(GCControllerDirectionPad *dpad, float xValue, float yValue) { }

Only thing I found was this piece of code from apple:

Swift

typealias GCControllerDirectionPadValueChangedHandler = 
 (GCControllerDirectionPad!, CFloat, CFloat) -> Void

OBJECTIVE-C

typedef void (^GCControllerDirectionPadValueChangedHandler)
   (GCControllerDirectionPad *dpad, float xValue, float yValue)

--

This is the code I've tried so far, but with no luck.

var dpadMoveHandler: GCControllerDirectionPadValueChangedHandler = 
   (#dpad:GCControllerDirectionPad, #xValue:CFloat, #yValue:CFloat)  {

    }
0

1 Answer 1

3

That line declares an Objc block. The typealias you're looking at in Swift is a closure type. The equivalent code would be this:

var dpadMoveHandler:GCControllerDirectionPadValueChangedHandler = 
  { 
     (dpad:GCControllerDirectionPad!, xValue:CFloat, yValue:CFloat) -> () in 
     return 
  }

Read about Swift closures here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.