访问变量输出完成块
Accessing variable out completion block
我如何访问完成块之外的员工值,以便在 ViewController
中的任何地方使用它
extension TabsParentViewController{
class func checkCreateTabPermission(completion: @escaping PermCompletionWithCancel){
RRS.s.permissionService.checkForPermission(.createTab, completion: { (pass, employee, cancel) in
if !cancel {
ActivityLogsBuilder.logActivity(forEmployee: employee, module: .tabs, action: ActivityLogsBuilder.action(from: LogAction.createTab, authorized: pass))
}
completion(pass, employee, cancel)
})
}
}
默认情况下,调用 checkCreateTabPermission
为:
checkCreateTabPermission { pass, employee, cancel in
// here you can use the `employee`
}
应该能让您访问返回的 employee
。因此,您可以在 checkCreateTabPermission
:
的 completion
中调用所需的方法
checkCreateTabPermission { pass, employee, cancel in
myMethod(employee: employee)
}
或者,如果您想访问 completion
之外的员工,您可以声明一个变量(默认情况下为 nil
)以在返回后保存其值:
var myEmployee: Employee?
checkCreateTabPermission { [weak self] pass, employee, cancel in
self?.myEmployee = employee
}
// you could use `myEmployee` here, but you have to make sure its not nil,
// in other words, `checkCreateTabPermission` has been called and retrieved one.
if let myUnwrappedEmployee = myEmployee {
// use `myUnwrappedEmployee`...
}
我如何访问完成块之外的员工值,以便在 ViewController
extension TabsParentViewController{
class func checkCreateTabPermission(completion: @escaping PermCompletionWithCancel){
RRS.s.permissionService.checkForPermission(.createTab, completion: { (pass, employee, cancel) in
if !cancel {
ActivityLogsBuilder.logActivity(forEmployee: employee, module: .tabs, action: ActivityLogsBuilder.action(from: LogAction.createTab, authorized: pass))
}
completion(pass, employee, cancel)
})
}
}
默认情况下,调用 checkCreateTabPermission
为:
checkCreateTabPermission { pass, employee, cancel in
// here you can use the `employee`
}
应该能让您访问返回的 employee
。因此,您可以在 checkCreateTabPermission
:
completion
中调用所需的方法
checkCreateTabPermission { pass, employee, cancel in
myMethod(employee: employee)
}
或者,如果您想访问 completion
之外的员工,您可以声明一个变量(默认情况下为 nil
)以在返回后保存其值:
var myEmployee: Employee?
checkCreateTabPermission { [weak self] pass, employee, cancel in
self?.myEmployee = employee
}
// you could use `myEmployee` here, but you have to make sure its not nil,
// in other words, `checkCreateTabPermission` has been called and retrieved one.
if let myUnwrappedEmployee = myEmployee {
// use `myUnwrappedEmployee`...
}