Swift Optionals and Forced Unwrapping -


i having hard time understanding optionals , forced unwrapping in swift language. have read book , chapters several times cannot understand it.

is there difference between following two:

totalamounttextfield?.text.toint()  totalamounttextfield!.text.toint() 

also, when declaring iboutlets why make optional field this:

@iboutlet var nametextfield :uitextfield? 

if don't use "?" @ end gives errors.

totalamounttextfield?.text.toint() equivalent

func foo() -> int? { // give optional int     if let field = totalamounttextfield {         return field.text.toint()     } else {         return nil // return nil if totalamounttextfield nil     } }  foo() 

it should used if totalamounttextfield can nil


totalamounttextfield!.text.toint() equivalent

func foo() -> int { // give int     if let field = totalamounttextfield {         return field.text.toint()     } else {         crash() // crash if totalamounttextfield nil     } }  foo() 

it should used if know totalamounttextfield must not nil


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -