迅速跟随以下块属性的属性块声明等效于什么? [英] What is the property block declaration equivalent in swift of the following block property?

查看:50
本文介绍了迅速跟随以下块属性的属性块声明等效于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C中,我这样做:

In Objective-C I do this:

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

迅速做到这一点的正确方法是什么?

What is the correct way to do this in swift?

推荐答案

相应的闭包属性将声明为

The corresponding closure property would be declared as

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

您可以将属性设置为

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 ...
}

这篇关于迅速跟随以下块属性的属性块声明等效于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆