Swift: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
No edit summary
Line 21: Line 21:
         print("Hello \(nameTexField.text!)");
         print("Hello \(nameTexField.text!)");
         helloLabel.text = "Hello \(nameTexField.text!), your age is \(ageTextField.text!)";
         helloLabel.text = "Hello \(nameTexField.text!), your age is \(ageTextField.text!)";
    }
}
</source>
==Alert & Action==
<source lang="swift">
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 okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        alertCtrl.addAction(okAction)
        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 okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        actionSheet.addAction(okAction)
        present(actionSheet, animated: true, completion: nil)
     }
     }
}
}

Revision as of 23:55, 7 April 2019

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 okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        alertCtrl.addAction(okAction)
        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 okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        actionSheet.addAction(okAction)
        present(actionSheet, animated: true, completion: nil)
    }
}

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