Swift:如何检查对协议中声明为可选的var的响应和分配var
议定书声明:Swift:如何检查对协议中声明为可选的var的响应和分配var,swift,protocols,xcode6,optional,optional-variables,Swift,Protocols,Xcode6,Optional,Optional Variables,议定书声明: @objc protocol LeftSideMenuViewControllerProtocol { var containerShouldPerformContentSegueWithIdentifier:((segueIdentifier: String, object: AnyObject!) -> Void)! {set get} optional var containerShouldResizeLeftMenuToValue:((CGFloat)
@objc protocol LeftSideMenuViewControllerProtocol {
var containerShouldPerformContentSegueWithIdentifier:((segueIdentifier: String, object: AnyObject!) -> Void)! {set get}
optional var containerShouldResizeLeftMenuToValue:((CGFloat) -> Void)! {set get}
}
用法:
override var leftEmbedNavigation: CommonNavigationController? {
willSet{
if let nav = newValue {
if let vc = nav.viewControllers[0] as? LeftSideMenuViewControllerProtocol {
// non optional var, all good
vc.containerShouldPerformContentSegueWithIdentifier = {
[weak self] (segueIdentifier: String, object: AnyObject!) in
}
// optional declaration, cannot assign
vc.containerShouldResizeLeftMenuToValue = { [weak self] (newConstraintValue) in
}
}
}
}
}
如何检查实现的var以及如何分配?为什么要使用变量而不是正确的委托方法?@akashivskyy使用闭包/块从我的观点来看,效率更高、可读性更强、更有用。在像这样的Objective-C任务中,解决起来很容易,我不明白为什么协议有“可选”方法和var,如果没有文档化的方法可以使用?现在,您无法分配协议的可选变量(可能是一个bug)。如果你愿意,你可以跟着。