Create an optional block as a variable

前端 未结 2 976
臣服心动
臣服心动 2021-01-17 16:35

I have a simple class where I declare a block as a variable:

class MyObject : NSObject 
{
    var progressBlock:(progress:Double) -> ()?

    init() { }
}         


        
2条回答
  •  生来不讨喜
    2021-01-17 17:03

    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 */ }
        }
    }
    

提交回复
热议问题