Created
December 9, 2016 05:23
-
-
Save jamesrochabrun/0ad198939894d4a3fabb22f5fa79881e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //: Playground - noun: a place where people can play | |
| import Foundation | |
| //basic syntax | |
| func sayHello () { | |
| print("hello") | |
| } | |
| sayHello() | |
| //parameters | |
| func sayGoodBye(time: Int, to: String) { | |
| print("\(time) \(to)") | |
| } | |
| sayGoodBye(time: 0, to: "pepe") | |
| //return tipe | |
| func returnCompletString(basicString: String) -> (String) { | |
| return ("\(basicString) es mas completa") | |
| } | |
| print(returnCompletString(basicString: "esta funcion retorna y..")) | |
| var extras: Dictionary = ["piña" : 0.45, "salame" : 0.90, "peperoni" : 0.44] | |
| //return an new price | |
| func addExtraIngredient(extras: [String: Double] , | |
| ingredientName: String, | |
| quantity: Int) -> Double? { | |
| // if let unitPrice = extras[ingredientName] { | |
| // let finalPrice = Double(quantity) * unitPrice | |
| // return finalPrice | |
| // } | |
| // return 0 | |
| //introducing Guard | |
| guard let unitPrice = extras[ingredientName] else { | |
| return nil | |
| } | |
| return Double(quantity) * unitPrice | |
| } | |
| let finalPrice = addExtraIngredient(extras: extras, ingredientName: "piña", quantity: 4) | |
| print(finalPrice!) | |
| //Introducing tuples, if we dont use labels for parameters we can use the indexvalue of the tuple | |
| let company = ("Apple", "apple.inc" , 89) | |
| company.0 | |
| company.1 | |
| let (stockCode, companyName , stockPrice) = ("Apple", "apple.inc" , 89) | |
| print("el valor de bolsa de \(stockCode) es \(stockPrice)") | |
| //better to integrate both | |
| let stocksCompany = (id: "Apple", companyName: "apple.inc", stockPrice:89) | |
| stocksCompany.companyName | |
| func getProduct(idProduct: Int) -> (id: String, name: String, price: Double) { | |
| var id = "IPHOTO", name = "Iphone 5", price = 777 | |
| switch idProduct { | |
| case 1: | |
| id = "iph5" | |
| name = "iphone 6" | |
| price = 77777 | |
| case 2: | |
| id = "iph57" | |
| name = "iphone 7" | |
| price = 77777 | |
| case 3: | |
| id = "iph55" | |
| name = "iphone 8" | |
| price = 77777 | |
| default: | |
| break | |
| } | |
| return (id, name, Double(price)) | |
| } | |
| let product = getProduct(idProduct: 2) | |
| //functionswith default values and external variables | |
| func area(length: Int, width:Int) -> Int { | |
| let area = length * width | |
| return area | |
| } | |
| func getAreaBasedOn(length:Int, width:Int, forCarpetType carpet:String = "tan") -> Int { | |
| let areaOfRoom = area(length:length, width:width) | |
| var price = 0 | |
| switch carpet { | |
| case "tan": price = areaOfRoom*1 | |
| case "red": price = areaOfRoom*2 | |
| case "blue": price = areaOfRoom*3 | |
| default:price = 0 | |
| } | |
| return price | |
| } | |
| getAreaBasedOn(length: 10, width: 10, forCarpetType: "blue") | |
| getAreaBasedOn(length: 30, width: 30)//default value not related with the default of the switch | |
| //function with no arguments labels | |
| func somefunction(_ test:Int) { | |
| print(test) | |
| } | |
| somefunction(5) | |
| //code challenge | |
| func coordinates(for location:String)-> (Double, Double) { | |
| var tuple = (Double(0),Double(0)) | |
| switch location { | |
| case "Eiffel Tower": tuple = (48.8582,2.2945) | |
| case "Great Pyramid": tuple = (29.9792, 31.1344) | |
| case "Sydney Opera House": tuple = (33.8587,151.2140) | |
| default: tuple = (Double(0),Double(0)) } | |
| return tuple | |
| } | |
| coordinates(for: "Eiffel Tower") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment