export PATH=/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin:"${PATH}"
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!)";
}
}
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)
}
}
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
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)
}
}
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)
}
}
// Table View
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var items:[ToDoItem] = []
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
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")
}
return cell
}
//@IBAction func onAddItemAction(_ sender: UIBarButtonItem) {
//let item = ToDoItem(itemName: name, itemDesc: desc, itemPlace: place)
//self.items.append(item)
//self.tableView.reloadData()
//}
// send data to details view
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toDetails" { //restrict seque
let selectedIndex = tableView.indexPathForSelectedRow!
let selectedItem = items[selectedIndex.row]
let dest = segue.destination as! DetailsViewController
dest.todoItem = selectedItem
}
}
}