Swift

From Chorke Wiki
Jump to navigation Jump to search
export PATH=/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin:"${PATH}"

Hello World

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var nameTexField: UITextField!
    @IBOutlet weak var ageTextField: UITextField!
    @IBOutlet weak var helloLabel: UILabel!
    

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func clickMeAction(_ sender: Any) {
        print("Hello \(nameTexField.text!)");
        helloLabel.text = "Hello \(nameTexField.text!), your age is \(ageTextField.text!)";
    }
}

Alert & Action

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var nameTexField: UITextField!
    @IBOutlet weak var ageTextField: UITextField!
    @IBOutlet weak var helloLabel: UILabel!
    

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func clickMeAction(_ sender: Any) {
        print("Hello \(nameTexField.text!)")
        helloLabel.text = "Hello \(nameTexField.text!), your age is \(ageTextField.text!)"
    }
    
    @IBAction func showAlertAction(_ sender: UIButton) {
        let alertCtrl = UIAlertController(title: "Alert", message: "Hello Alert", preferredStyle: .alert)
        let destructiveAction = UIAlertAction(title: "Delete", style: .destructive, handler: nil)
        let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
        let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        alertCtrl.addAction(okAction)
        alertCtrl.addAction(cancelAction)
        alertCtrl.addAction(destructiveAction)
        present(alertCtrl, animated: true, completion: nil)
    }
    
    @IBAction func showConfirmAction(_ sender: UIButton) {
        let actionSheet = UIAlertController(title: "Hello World", message: "This is an Action Sheet", preferredStyle: .actionSheet)
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        actionSheet.addAction(okAction)
        actionSheet.addAction(cancelAction)
        present(actionSheet, animated: true, completion: nil)
    }
}

Playground

import UIKit

var hello = "Hello, playground"
print(hello)

// 1) constrant vs variable
var name = "Raiyan Bin Shahed" //mutable and variable
let greetings = "Hello"        //immutable and constrant
let message = greetings + name
print(message)

name = "Shahed Hossain"
print(name)

// 2) Implicit vs Explicit typing
let huaweiPrice:Double = 2999 //Explicit Typing
let deliveryCharge = 9.99     //Implicit Typing
let totalPrice = huaweiPrice + deliveryCharge
print(totalPrice)

// 3) If else in Swift
// No need to end statement in semicolon (optional)
// Conditional bracket is optional
// The curly bracket is compulsary even it is for one liner statement
let hungry = true
if hungry {
    print("Let's have a break now")
}
else {
    print("Let's have a break in 15 minutes")
}


// 4) Array and For Loop
var myArray = [2, 4, 6, 8, 10]
myArray.append(12) //add item
print(myArray.count) // length
print(myArray[1]) //accessing 2nd item by index


// 4) For Loop in Swift
//  collection for loop
for item in myArray {
    print(item)
}

// incrementaql for loop
for i in 0..<myArray.count {
    print("Item \(i) is \(myArray[i])") //interpolation
}

// 5) Dictionary
let myInfo    = ["name": "Shahed Hossain", "city": "KLC", "phone": "01117606778", "email": "[email protected]"]
let mySonInfo = ["name": "Raiyan Bin Shahed", "city": "KLC", "phone": "01117606778"]
// When we get a value from dictionary, we will get optional data type,
// Since it can be nil or it can eturn a value
print(myInfo["name"])
print(mySonInfo["email"])

// To get the value, you can use
// 5.1) Optional Binding
// Have to write more code but null safe
// Safe for runtime as well as production
if let newVar = myInfo["name"] {
    print(newVar)
}

// 5.2) Force unwrap
print(myInfo["name"]!) // easy to write but not null safe

Inheritance

import UIKit

var hello = "Hello, playground"
print(hello.suffix(10))
print(hello)

class BaseCalculator : NSObject {
    var a:Int
    var b:Int

    init(a:Int, b:Int){
        self.a = a
        self.b = b
    }

    func add() -> Int {
        return self.a + self.b
    }

    func printAdd(){
        print("a + b = \(self.add())")
    }
}

class SubCalculator: BaseCalculator {

    func sub() -> Int {
        return self.a - self.b
    }

    func printSub(){
        print("a - b = \(self.sub())")
    }

}

let baseCal = BaseCalculator(a:10, b:20)
print(baseCal.add())
baseCal.printAdd()

let subCal = SubCalculator(a:20, b:10)
print(subCal.add())
subCal.printAdd()

print(subCal.sub())
subCal.printSub()

Switcher and Slider

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var priceFilter: UISwitch!
    @IBOutlet weak var priceSlider: UISlider!
    @IBOutlet weak var priceLabel: UILabel!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }


    @IBAction func switchFilterAction(_ sender: UISwitch) {
        if priceFilter.isOn {
            priceSlider.isEnabled = true
            priceLabel.isHidden = false
            priceLabel.text = String(format:"%2.2f", priceSlider.value)
        }
        else {
            priceSlider.isEnabled = false
            priceLabel.isHidden = true
        }
    }
    
    
    @IBAction func whenPriceUpdated(_ sender: UISlider) {
        priceLabel.text = String(format:"%2.2f", priceSlider.value)
    }
}

Table View

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    @IBOutlet weak var tableView: UITableView!
    
    var items:[ToDoItem] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let tea = ToDoItem(itemName: "Tea", itemDesc: "Tea Break 20 min", itemPlace: "iTrain")
        let coffee = ToDoItem(itemName: "Coffee", itemDesc: "Coffee Break 20 min", itemPlace: "iTrain")
        let lunch = ToDoItem(itemName: "Lunch", itemDesc: "Lunch Break 50 min", itemPlace: "KFC")
        let dinner = ToDoItem(itemName: "Dinner", itemDesc: "Dinner Break 50 min", itemPlace: "MAC")
        let suffer = ToDoItem(itemName: "Suffer", itemDesc: "Suffer Break 30 min", itemPlace: "Home")
        
        tea.completed = true
        coffee.completed = true
        
        items.append(tea)
        items.append(coffee)
        items.append(lunch)
        items.append(dinner)
        items.append(suffer)
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
//      return 5
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = items[indexPath.row].itemName
        if items[indexPath.row].completed {
            cell.imageView?.image =  UIImage(named: "checked.png")
        }
        else {
            cell.imageView?.image =  UIImage(named: "unchecked.png")
        }
//      cell.textLabel?.text = "Hello World"
        return cell
    }

    @IBAction func onAddItemAction(_ sender: UIBarButtonItem) {
        let promptController = UIAlertController(title: "Add Item", message: "Please Add an Item", preferredStyle: .alert )
        
        promptController.addTextField{(textField) in
            textField.placeholder = "Item name"
        }
        
        promptController.addTextField{(textField) in
            textField.placeholder = "Item Description"
        }
        
        promptController.addTextField{(textField) in
            textField.placeholder = "Item Place"
        }
        
        
        let addAction = UIAlertAction(title: "Add", style: .default){(alert) in
            let textFields = promptController.textFields!
            let name  = textFields[0].text!
            let desc  = textFields[1].text!
            let place = textFields[2].text!
            
            let item = ToDoItem(itemName: name, itemDesc: desc, itemPlace: place)
            self.items.append(item)
            self.tableView.reloadData()
        }
        
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        
        promptController.addAction(cancelAction)
        promptController.addAction(addAction)
        
        present(promptController, animated: true, completion: nil)
    }
    
}

Memo or Todo

// Example of Alert with Input Field
class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    var items:[ToDoItem] = []

    @IBAction func onAddItemAction(_ sender: UIBarButtonItem) {
        let promptController = UIAlertController(title: "Add Item", message: "Please Add an Item", preferredStyle: .alert )
        
        promptController.addTextField{(textField) in
            textField.placeholder = "Item name"
        }
        promptController.addTextField{(textField) in
            textField.placeholder = "Item Description"
        }
        promptController.addTextField{(textField) in
            textField.placeholder = "Item Place"
        }
        
        let addAction = UIAlertAction(title: "Add", style: .default){(alert) in
            let textFields = promptController.textFields!
            let name  = textFields[0].text!
            let desc  = textFields[1].text!
            let place = textFields[2].text!
            
            let item = ToDoItem(itemName: name, itemDesc: desc, itemPlace: place)
            self.items.append(item)
            self.tableView.reloadData()
        }
        
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        promptController.addAction(cancelAction)
        promptController.addAction(addAction)
        present(promptController, animated: true, completion: nil)
    }

}

Cocoa Pods

pod --version
pod repo update
cd /opt/dev/cki_workspaces/
cd ./init_workspace/chorke-init-iosapp/
pod init
vim Podfile 
pod install

Exception

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <!-- exception ini -->
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>dev.chorke.org</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
            </dict>
        </dict>
    </dict>
    <!-- exception end -->
</dict>
</plist>

Features

  1. Closures unified with function pointers
  2. Tuples and multiple return values
  3. Generics
  4. Fast and concise iteration over a range or collection
  5. Structs that support methods, extensions, and protocols
  6. Functional programming patterns, e.g., map and filter
  7. Powerful error handling built-in
  8. Advanced control flow with do, guard, defer, and repeat keywords

References