Back to Browse

SWIFT PROGRAMMING TUTORIAL: OPTIONAL IN SWIFT PT 1/4

1.3K views
Jun 18, 2017
2:25

JOIN THIS COURSE FOR FREE: https://ductran.co/p/optional-swift As a programmer, it is our main job to deal with data: store, process, manipulate, share data. This is what you do when building a social network apps to allow users share photos and videos. It's the games you create with beautiful animation, graphics and stories. Data comes in many forms. And as a developer, we all have to deal with a situation when data is not available. It means that we have something to store but that thing is nothingness. That's why in Swift, we have a different "layer" of data type - Optional type. It means that the type can store nothingness - empty data. In this course, you'll learn: + What is Optional and why we use Optional in Swift + How to create, and use Optional with unwrapping + Optional binding to safely unwrap optionals + Optional chaining JOIN THIS COURSE FOR FREE: https://ductran.co/p/optional-swift ********* ABOUT CODE MASTERY ********* Code Mastery is hosted by Duc Tran, founder of Developers Academy. This is his free-style no notes, no teleprompter presentation and live coding broadcast with you guys everyday. To join Duc's free courses, register for free at http://ductran.co/ ********* MEET DUC TRAN ********* Duc Tran is founder of Developers Academy, one of the world's leading iOS, Android and Web development trainers. More than 2,000,000 developers have studied his video trainings; 100,000 developers see his posts each month. Each year, Duc has helped 20,000 plus developers graduate from his online courses or video series. ********* FREE TRAININGS IN IOS DEVELOPMENT ********* To subscribe and get free tutorials, courses and weekly content, visit me at: http://ductran.co/ Connect with Duc on facebook: http://facebook.com/ductranfan Tweet him: https://twitter.com/ductrongtran Get daily inspiration: http://instagram.com/ductran.co/ ********* SOURCE CODE IN THIS COURSE ********* import UIKit // 2.1 Introduce Optional class GameViewController { var questionLabel: UILabel? var question: String init(question: String, questionLabel: UILabel?) { self.question = question self.questionLabel = questionLabel } func viewDidLoad() { // questionLabel!.text = question // this can crash if questionLabel != nil { questionLabel!.text = question } else { questionLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 40)) questionLabel!.text = question } } } let gameVC = GameViewController(question: "What is 5+5?", questionLabel: nil) // this means there's no label created gameVC.viewDidLoad() // 2.2 Optional Binding (if-let) // introduction to Dictionary in Swift var states: [String : String] = [ "CA" : "California", "LA" : "Los Angeles", "PA" : "Pennsylvania", "WA" : "Washington", "MA" : "Massachusetts" ] let ca = states["CA"] let la = states["LA"] let pa = states["PA"] // but nil: let ny = states["NY"] let tx = states["tx"] let key = "CA" let state = states[key] print("\(key) is \(state)") if let caState = states[key] { print("\(key) is \(caState)") } else { print("Ooops. There's no state matching this key") } // another example: var weatherForecast : [String : [String : Int]] = [ "today" : [ "temperature" : 92, "humidity" : 98, "rain" : 82 ], "tomorrow" : [ "temperature" : 72, "humidity" : 12, "rain" : 99 ] ] if let todayWeather = weatherForecast["today"] { if let temperature = todayWeather["temperature"] { print("today's temperature is \(temperature)") } } // 2.3 Code Challenge if let tomorrow = weatherForecast["tomorrow"] { if let rain = tomorrow["rain"] { print(rain) } } var programs = [ "Total iOS Blueprint" : ["Swift", "Build Foursquare"], "Socialize Your Apps" : ["Build Facebook", "Build Messenger"] ] if let tibCourses = programs["Total iOS Blueprint"] { print(tibCourses[0]) } // 2.4 Downside of if-let, introduce guard let syntax var weatherForecast1 = [ "today" : [ "temperature" : 92, "humidity" : 98 ], "tomorrow" : [ "temperature" : 72, "rain" : 99 ] ] typealias WeatherDictionary = [String : [String : Int]] struct CurrentWeather { var temperature: Int var humidity: Int? var rain: Int? } // 1. Downside of if-let func processWeather(for date: String, from weatherDictionary: WeatherDictionary) - CurrentWeather? { if let today = weatherDictionary[date] { if let temp = today["temperature"], let humidity = today["humidity"], let rain = today["rain"] { return CurrentWeather(temperature: temp, humidity: humidity, rain: rain) } else { return nil } } else { return nil } } func processWeather2(for date: String, from weatherDictionary: WeatherDictionary) - CurrentWeather? { guard let today = weatherDictionary[date], let temp = today["temperature"] else { return nil } return CurrentWeather(temperature: temp, humidity: today["humidity"], rain: today["rain"]) } // 2.5 Code Challenge var fourSquareDatabase = [ "nearBy" : [ "1m" : ["Starbucks", "Apple Bees"], "5m" : ["Red Lobster", "Sunny Cafe"] ] ]

Download

0 formats

No download links available.

SWIFT PROGRAMMING TUTORIAL: OPTIONAL IN SWIFT PT 1/4 | NatokHD