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

前端 未结 1 774
无人共我
无人共我 2020-12-29 05:55

In Objective-C I do this:

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

What is the correct way to do this in swift?

相关标签:
1条回答
  • 2020-12-29 06:40

    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 ...
    }
    
    0 讨论(0)
提交回复
热议问题