​Fast is just a strong and instinctive programming language for macOS, iOS, watchOS and tvOS....

Page 31

Swift

import Cocoa

var myString:String!

myString = "Hello, Swift!"

if myString != nil { println(myString) }else{ println("myString has nil value") } When we run the above program using playground, we get the following result: Hello, Swift!

Optional Binding Use optional binding to find out whether an optional contains a value, and if so, to make that value available as a temporary constant or variable. An optional binding for the if statement is as follows: if let constantName = someOptional { statements } Let's take a simple example to understand the usage of optional binding: import Cocoa

var myString:String?

myString = "Hello, Swift!"

if let yourString = myString { println("Your string has - \(yourString)") }else{ println("Your string does not have a value")

19


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.