swift - constant 'result' inferred to have type (), which may be unexpected -
@ibaction func operate(sender: uibutton) { if let operation = sender.currenttitle { if let result = brain.performoperation(operation) { displayvalue = result } else { displayvalue = 0.0 } } } i new coding pardon coding format , other inconsistencies. have been trying out ios 8 intro swift programming taught stanford university , have ran problem modified calculator.
i 3 errors. first 1 swift compiler warning - @
if let result = brain.performoperation(operation) it says
constant 'result' inferred have type () may unexpected.
it gives me suggestion ----
if let result: () = brain.performoperation(operation) the other 2 errors
bound value in conditional binding must of optional type @ if let result line
cannot assign value of type () value of double @ "displayvalue = result"
here github link if needs more information on code.
thanks in advance.
guessing errors, expect performoperation() supposed return double? (optional double) while if fact returns nothing.
i.e. it's signature probably:
func performoperation(operation: string) { // ... } .. while in fact should be:
func performoperation(operation: string) -> double? { // ... } reason why think line: if let result = brain.performoperation(operation) call "unwrapping optional" , expects assigned value optional type. later assign value unwrap variable seems of double type.
by way, shorter (and more readable) way write same is:
displayvalue = brain.performoperation(operation) ?? 0.0
Comments
Post a Comment