ios swift属性块设置并获取

时间:2017-06-16 12:51:50

标签: swift swift3 ios10 ios10.3.2

在objective-c中我们做

@property (strong, nonatomic) void(^Name)(id input, id selectedListItem);

然后我们设置属性

if (self.Name) {
    self.Name(self,nil);
}

用于访问它的viewcontroller

[classObject setName:^(id input, id selectedListItem)
// do something with the input and selectedListItem
];

我们如何在swift3中完成它。

1 个答案:

答案 0 :(得分:0)

@property (strong, nonatomic) void(^Name)(id input, id selectedListItem);

var name: ((input: Any, selectedListItem: Any?) -> ())? = nil

设置

obj.name = { (input, selectedListItem) in
    // do something with the input and selectedListItem
}

致电是

if let name = obj.name {
     name(self, nil)
}