在init中swift调用self里面的闭包

时间:2017-12-29 05:00:06

标签: swift closures

convenience init(title: String!,
    datePickerMode: UIDatePickerMode,
    selectedDate: Date!,
    origin view: UIView!) {

        self.init(title: title,
                  datePickerMode: datePickerMode,
                  selectedDate: selectedDate,
                  doneBlock: { [weak self] (_, date , _) in
                                guard let sself = self else { return }
                                sself.done.next(date as! Date)
                    }
            ,
            cancel: { [weak self] (_) in
                                guard let sself = self else { return }
                                sself.cancel.next()
                    }
            ,
            origin: view)

但我收到了编译错误:

  

'自'在self.init call之前使用

我一直在处理它,首先用闭包nil进行初始化并在之后设置。

convenience init(title: String!,
                 datePickerMode: UIDatePickerMode,
                 selectedDate: Date!,
                 origin view: UIView!) {

    self.init(title: title,
              datePickerMode: datePickerMode,
              selectedDate: selectedDate,
              doneBlock: nil,
              cancel: nil,
              origin: view)


    self.onActionSheetDone = { [weak self] (_, date , _) in
        guard let sself = self else { return }
        sself.done.next(date as! Date)
    }


    self.onActionSheetCancel = { [weak self] (_) in
        guard let sself = self else { return }
        sself.cancel.next()
    }

但我不明白第一次做这件事的问题。 感谢您提供一些信息

1 个答案:

答案 0 :(得分:-1)

在第一个条件中,您在self初始化之前使用了self。这是不允许的。