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
Post a Comment