swift - How to call an optional protocol method? -
i have class conforms own protocol has optional methods. reference class's object optional. in other words, object may present , may have implemented method in question. how call it? have code this:
if let thedelegate = delegate { if let result = thedelegate.delegatemethod() { } }
but xcode complains "value of optional type '(()->())?' not unwrapped". wants me change second half of line 2 in example "thedelegate.delegatemethod!()", force unwrapping defeats purpose of trying do. how supposed call method? note method has no parameters or return values.
according documentation, optional methods should called this:
if let thedelegate = delegate { if let result = thedelegate.delegatemethod?() { }else { // delegatemethod not implemented } }
optional property requirements, , optional method requirements return value, return optional value of appropriate type when accessed or called, reflect fact optional requirement may not have been implemented.
Comments
Post a Comment