https://youtu.be/GX4H0QHbByc
Download
You can download the React Bootstrap app from here
After download make sure to navigate into the downloaded folder and run `npm install`
https://youtu.be/GX4H0QHbByc
if let variableName = someValue { //someValue is not nil and it's safe to unwrap } else { //someValue is nil and cannot be unwrapped }In the above statement, if someValue is not nil it will go to the first bracket but if it's nill, it will go to the else statement. This concept is highly recommended for unwrapping the values that we are not sure if it's nil or not.
import UIKit class ViewController: UIViewController { @IBOutlet weak var billAmountField: UITextField! @IBOutlet weak var tipPercentageLabel: UILabel! @IBOutlet weak var totalLabel: UILabel! @IBOutlet weak var tipAmountLabel: UILabel! @IBOutlet weak var tpSlider: UISlider! @IBAction func calculatePressed(sender: UIButton) { calculateTip() } @IBAction func sliderChanged(sender: UISlider) { tipPercentageLabel.text! = "Tip Percentage " + String(Int(sender.value)) + "%" calculateTip() } func calculateTip() { var tipAmount = Float() var total = Float() if let billAmount = Float(billAmountField.text!) { tipAmount = billAmount * tpSlider.value/100 total = tipAmount + billAmount } else { tipAmount = 0 total = 0 } tipAmountLabel.text! = String(tipAmount) totalLabel.text! = String(total) } }
override func viewDidLoad() { super.viewDidLoad() //Do one time initializations }
override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) //Do updating the view and geometry settings }
override func viewWillDisappear(animated: Bool) { super.viewWillAppear(animated) }
@IBOutlet weak var resultsLabel: UILabel!
@IBOutlet weak var milesField: UITextField! var textFieldValue = milesField.text!
@IBOutlet weak var resultsLabel: UILabel! resultsLabel.text = "Some Value for the label";
@IBAction func convertPressed(sender: UIButton) { }
// // ViewController.swift // SConverterApp // // Created by Hamid on 4/3/16. // Copyright © 2016 HuxTek. All rights reserved. // import UIKit class ViewController: UIViewController { @IBOutlet weak var milesField: UITextField! @IBOutlet weak var resultsLabel: UILabel! @IBAction func convertPressed(sender: UIButton) { // 1 mile = 5280.0 feet let miles = Float(milesField.text!) let feets = miles! * 5280.0 resultsLabel.text = String(feets) } }
enum Gender { case Male case Female }
var huxTek: Gender huxTek = Gender.Male
huxTek = .Male
switch huxTek { case .Male: print("A man") case .Female: print("A woman") }
enum Optional{ case None case Some(T) }
var myDict = [String:String]() myDict = [ "key1": "val1", "key2": "val2", "key3": "val3" ] print(myDict["key1"])
"Optional("val1")\n"
print(myDict["key1"]!)
"val1"\n
if let key1Val = myDict["key1"] { print(key1Val) }Cast
var myDict = [String:AnyObject]() myDict = [ "key1": ["subKey1":"subVal1"], "key2": ["dasda", "asdasd"], "key3": "val3" ]If we define a variable to get "key1" value, it returns the type as AnyObject because it doesn't know the type. In order to define a type for it we need to cast it like bellow:
var sub1Key1Value = myDict["key1"] as! [String:String]Now it knows that the type is a dictionary with key as String and value as String. In order to safely check if an object is a certain type we can use "is" function as bellow:
if myDict["key1"] is [String:String] { var sub1Key1Value = myDict["key1"] as! [String:String] print(sub1Key1Value["subKey1"]!) }?
var myString: String?It's very important to know that after we put question mark, the variable is no longer a String type. It's type optional that could be String. In order to print it out we need to unwap it with !
var myVariable:String = "Some String Value"
var myVariable:String = "Some String Value"
print("This text will be printed in console after running the application")
var myVariable:String = "Some String Value"
let version: Double = 1.0 print(version)
let myInteger: Int = 2 print(myInteger)
let isAwesome: Bool = true print(isAwesome)
var letters = Array<String>() letters = ["a", "b", "c", "d"] print(letters[2])The second way is to just define it like bellow:
var letters = ["a", "b", "c", "d"] print(letters[2])We can iterate the array objects like bellow:
var numbers = [Int]() numbers = [1, 2, 3, 4] for number in numbers { print(number) }
var dict = Dictionary<String, Int>() dict = ["One": 1, "Two":2]
var dict = [String:Int]()
for (key, value) in dict { print("Value for \(key) is \(value)") }