Swift: Difference between revisions
Jump to navigation
Jump to search
Line 323: | Line 323: | ||
==References== | ==References== | ||
* [https://www.hackingwithswift.com/example-code/language/how-to-convert-json-into-swift-objects-using-codable Convert JSON to Swift objects using Codable] | * [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://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] |
Revision as of 02:55, 17 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
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 /Users/chorke/Documents/ios_training/MyPodsApp/
pod init
vim Podfile
pod install
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
References
- Convert JSON to Swift objects using Codable
- iOS App Transport Security Exception
- Cocoa Pods Playground How to
- Swift LLVM, Swift Docker
- Swift, Swift Clang
- Hacking With Swift
- Swift Cheat Sheet
- iSwift Playground
- iOS Chat Tutorial
- Ray Wender Lich
- Swift Community
- Training Manual
- Download Swift
- Cocoa Pods
- About Swift
- AppCoda
- CS193P