Swift & IOS Application Development Tutorials
Tutorial 1:
Introduction the Swift and Xcode Playground
Hello everyone, I start to new series about the IOS App Development with Swift programming language. In this article, I mentioned the Swift material and give the example source code. Bellow source code is the example of the Swift languages. I explain codes in comments. Good Luck.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import UIKit //Library initilation like Python. var str = "Hello, playground" //initliazing changable variable var x: Int = 0 //Int x += 1 // Increasing x variable at one var message = "A new message" var name = "John" print("We have a new message to \(name)") // Printing with variable var price = 5.6 //Float var unitsSold = 10 // Int let profit = price * Double(unitsSold) //let is not changable constant variable price -= 1 unitsSold += 2 let discountedProfit = price * Double(unitsSold) if (profit > discountedProfit) { // If condition print("DO NOT MAKE A DISCOUNT") } else { print ("MAKE A DISCOUNT") } class PriceSetup { //class initilazing let price: Double let unitsSold: Int let taxRatio: [Double] = [0.2, 0.3, 0.5] let taxDictinary : [String: Double] = ["Food": 0.08, "Clothes": 0.1, "Car": 0.98] init (initialPrice: Double, unitsSold: Int) { //initilazing class self.price = initialPrice self.unitsSold = unitsSold } func calculateProfit (productType: String) -> Double { if let tax = taxDictinary[productType] { return price * Double(unitsSold) * (1 - tax) } // if (tax == nil) { // return 0 // } return 0 } } let regularPriceSetup = PriceSetup(initialPrice: 10, unitsSold: 10) print("The profit for regular price setup is \(regularPriceSetup.calculateProfit(productType: "Food"))") for ratio in regularPriceSetup.taxRatio { //for loop example print(ratio) } for i in 4...9 { print (i) } class ExtendedPriceSetup : PriceSetup { //Extended class like subclass let taxFree: Bool init(taxFree: Bool, initialPrice: Double, unitsSold: Int){ self.taxFree = taxFree super.init(initialPrice: initialPrice, unitsSold: unitsSold) } } |