Thomasn
Thomasn

Reputation: 101

Porting an Objective-C block to Swift

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)  {

    }

Upvotes: 1

Views: 419

Answers (1)

Joseph Mark
Joseph Mark

Reputation: 9418

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.

Upvotes: 3

Related Questions