xcode - Optional binding bug on Swift 2.2? -


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;

enter image description here

sender.currenttitle optional.

here excerpt apple's "the swift programming language (swift 2.2)" example code below it;

if optional value nil, conditional false , code in braces skipped. otherwise, the optional value unwrapped , assigned constant after let, 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;

enter image description here

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:

enter image description here

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