Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the property block declaration equivalent in swift of the following block property?

In Objective-C I do this:

@property (nonatomic, copy) void(^completion)(MyObject * obj);

What is the correct way to do this in swift?

like image 573
zumzum Avatar asked Jun 30 '14 16:06

zumzum


1 Answers

The corresponding closure property would be declared as

class MyClass {
     var completion : ((MyObject) -> Void)? // or ...! for an implicitly unwrapped optional
}

You can set the property like

completion = {
    (obj : MyObject) -> Void in
    // do something with obj ...
}

which can be shortened (due to the automatic type inference) to

completion = {
    obj in
    // do something with obj ...
}
like image 179
Martin R Avatar answered Nov 11 '22 06:11

Martin R