Swift: Difference between revisions
Jump to navigation
Jump to search
Line 65: | Line 65: | ||
} | } | ||
} | } | ||
</source> | |||
==Playground== | |||
<source lang="swift"> | |||
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 | |||
</source> | </source> | ||
Revision as of 02:20, 8 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 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
Features
- Closures unified with function pointers
- Tuples and multiple return values
- Generics
- Fast and concise iteration over a range or collection
- Structs that support methods, extensions, and protocols
- Functional programming patterns, e.g., map and filter
- Powerful error handling built-in
- Advanced control flow with do, guard, defer, and repeat keywords