Swift: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
 
(24 intermediate revisions by the same user not shown)
Line 138: Line 138:
// 5.2) Force unwrap
// 5.2) Force unwrap
print(myInfo["name"]!) // easy to write but not null safe
print(myInfo["name"]!) // easy to write but not null safe
</source>
==Inheritance==
<source lang="swift">
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()
</source>
</source>


Line 173: Line 223:
     }
     }
}
}
</source>
==Table View==
<source lang="swift">
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)
    }
   
}
</source>
==Memo or Todo==
<source lang="swift">
// 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)
    }
}
</source>
==Cocoa Pods==
<source lang="bash">
pod --version
pod repo update
cd /opt/dev/cki_workspaces/
cd ./init_workspace/chorke-init-iosapp/
pod init
vim Podfile
pod install
</source>
==Exception==
<source lang="xml">
<?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>
</source>
</source>


Line 186: Line 396:


==References==
==References==
{|
| valign="top" |
* [https://www.hackingwithswift.com/example-code/language/how-to-convert-json-into-swift-objects-using-codable Convert JSON to Swift objects using Codable]
* [https://cocoacasts.com/app-transport-security-has-blocked-my-request iOS App Transport Security Exception]
* [https://learnappmaking.com/cocoapods-playground-how-to/  Cocoa Pods Playground How to]
* [https://github.com/apple/swift-llvm Swift LLVM], [https://github.com/apple/swift-docker Swift Docker]
* [https://github.com/apple/swift-llvm Swift LLVM], [https://github.com/apple/swift-docker Swift Docker]
* [https://github.com/apple/swift Swift], [https://github.com/apple/swift-clang Swift Clang]
* [https://github.com/apple/swift Swift], [https://github.com/apple/swift-clang Swift Clang]
* [https://www.hackingwithswift.com/ Hacking With Swift]
* [https://koenig-media.raywenderlich.com/uploads/2014/06/RW-Swift-Cheatsheet-0_8.pdf Swift Cheat Sheet]
* [https://koenig-media.raywenderlich.com/uploads/2014/06/RW-Swift-Cheatsheet-0_8.pdf Swift Cheat Sheet]
* [https://iswift.org/playground iSwift Playground]
* [https://www.scaledrone.com/blog/ios-chat-tutorial/ iOS Chat Tutorial]
* [https://www.scaledrone.com/blog/ios-chat-tutorial/ iOS Chat Tutorial]
* [https://www.raywenderlich.com/ Ray Wender Lich]
| valign="top" |
* [https://stackoverflow.com/questions/55127152 Get a Popup Dialog Box in Swift Playgrounds]
* [https://swift.org/ Swift Community]
* [https://swift.org/ Swift Community]
* [https://drive.google.com/drive/folders/1iOAfsvSPu-qyHLSs_jAcMQvlk1Z-pnuv Training Manual]
* [https://swift.org/download/ Download Swift]
* [https://swift.org/download/ Download Swift]
* [[Swift Custom UI]]
* [https://cocoapods.org/ Cocoa Pods]
* [https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md SQLite.swift]
* [https://swift.org/about/ About Swift]
* [https://swift.org/about/ About Swift]
* [https://www.appcoda.com/ AppCoda]
* [https://web.stanford.edu/class/cs193p/cgi-bin/drupal/ CS193P]
| valign="top" |
* [https://github.com/victorpimentel/MarvelAPI/tree/master/MarvelAPI.playground Marvel API]
|}

Latest revision as of 22:25, 10 December 2020

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