if let mathematicalsymbol = sender.currenttitle { brain.performoperation(mathematicalsymbol) } the code above introduces error below;
value of optional type 'string?' not unwrapped; did mean use '!' or '?'?
as can seen in screen shot;
sender.currenttitle optional.
here excerpt apple's "the swift programming language (swift 2.2)" example code below it;
if optional value
nil, conditionalfalse, code in braces skipped. otherwise, the optional value unwrapped , assigned constant afterlet, makes unwrapped value available inside block of code.
here sample code excerpt;
var optionalname: string? = "john appleseed" var greeting = "hello!" if let name = optionalname { greeting = "hello, \(name)" } so these reasons, i'm thinking either i'm missing something or i'm hitting bug.
i've tried similar on playground, , didn't similar error;
here swift version;
apple swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.31) target: x86_64-apple-macosx10.9
if @ currenttitle, you'll see inferred string??. example, go currenttitle in xcode , hit esc key see code completion options, , you'll see type thinks is:
i suspect have in method defining sender anyobject, such as:
@ibaction func didtapbutton(sender: anyobject) { if let mathematicalsymbol = sender.currenttitle { brain.performoperation(mathematicalsymbol) } } but if explicitly tell type sender is, can avoid error, namely either:
@ibaction func didtapbutton(sender: uibutton) { if let mathematicalsymbol = sender.currenttitle { brain.performoperation(mathematicalsymbol) } } or
@ibaction func didtapbutton(sender: anyobject) { if let button = sender as? uibutton, let mathematicalsymbol = button.currenttitle { brain.performoperation(mathematicalsymbol) } } 


Comments
Post a Comment