Create an optional block as a variable

依然范特西╮ 提交于 2019-12-01 15:43:07

The way you have written it, the compiler assumes progressBlock is a closure that returns an optional empty tuple instead of an optional closure that returns an empty tuple. Try writing it like this instead:

class MyObject:NSObject {
    var progressBlock:((progress:Double) -> ())?
    init() {
        progressBlock = nil
        progressBlock = { (Double) -> () in /* code */ }
    }
}

Adding to connor's reply. An optional block can be written as:

var block : (() -> ())? = nil

Or as an explicit Optional:

var block : Optional<() -> ()> = nil

Or better yet, with a custom type

typealias BlockType = () -> ()

var block : BlockType? = nil
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!