// Your code here! import Foundation //VARIABLES var hiVar:String = "holis word" //CONVERT VARIABLES var numberVar:Int = 100 var hiNumberVar = hiVar + String(numberVar) print(hiNumberVar) //ARRAYS var shoppingListVar = ["apple", "pen"] print(shoppingListVar) shoppingListVar.append("applepen") print(shoppingListVar) //DICTIONARIES var shoppingDictionatyVar = [String: Int]() shoppingDictionatyVar = ["apple":10, "pen":200] print(shoppingDictionatyVar) //DICTIONARIES MAPS let shoppingListInflation = shoppingDictionatyVar.map { name,price in price + 2 } print(shoppingListInflation) //DICTIONARIES FILTERS let shoppingListInflationBiggerThanTwelve = shoppingListInflation.filter { $0 > 12 } print(shoppingListInflationBiggerThanTwelve) //DICTIONARIES REDUCE let shoppingListInflationTotal = shoppingListInflation.reduce(0,+) print(shoppingListInflationTotal) //SORTED var developersList = ["Sil", "Fede", "Piruzi","Ali", "Sherazade"] let sortedDevelopersList = developersList.sorted { $0 < $1 } print(sortedDevelopersList) //SWITCH let shoppingListFede = "yerba mate" switch shoppingListFede { case "pochoclos": print("nope!.") case "tomato", "chocolate": print("nope.") case let x where x.hasPrefix("yerba"): print("that's what i'm talking about") default: print("Fede don't know") } // FOR //WHILE //FUNCTIONS func isFriday(_ person: String) -> String { var message:String = "Hello \(person)." let date = Date() let dateFormatter = DateFormatter() dateFormatter.dateFormat = "EEEE" let dayInWeek = dateFormatter.string(from: date) message += dayInWeek == "Friday" ? "YEIYYYY!" : " BUUUU! it's just another day " return message } print(isFriday("Sil")) //ENUMS enum Petersen { case bsj, bsf, ber, bsc func fullName() -> String { switch self { case .bsj: return "Banco San Juan" case .bsf: return "Banco Santa Fe" case .ber: return "Banco Entre Rios" case .bsc: return "Banco Santa Cruz" } } } let bank = Petersen.bsj let bankDescription = bank.fullName() print(bankDescription) //CLASS enum Team { case ios,android,web func fullName() -> String { switch self { case .ios: return "IOS" case .android: return "ANDROID" case .web: return "WEB" } } } class PersonClass{ var name: String init(name:String){ self.name = name } func hello() -> String { return "Hi \(self.name)." } } class DeveloperClass : PersonClass{ var team: String init(team:String,name:String){ self.team = team super.init(name: name) } override func hello()-> String { switch self.team { case "IOS": return "Hi \(self.name). Your repo is https://repos/selfcare-ios" case "ANDROID": return "Hi \(self.name). Your repo is https://repos/selfcare-android" default: return "Hi \(self.name). You don't have any repos :( " } } } var personVar = PersonClass(name:"Sil") var developerVar = DeveloperClass(team: Team.ios.fullName(),name:"Sil") print(personVar.hello()) print(developerVar.hello()) //Struct vs Class https://blog.usejournal.com/swift-basics-struct-vs-class-31b44ade28ae //In “value types”, (example: struct, enum, tuples) each instance keeps a unique copy of its data. //In “reference types”, (example: class) instances share a single copy of the data. //Use a value type when: //You want copies to have independent state //The data will be used in code across multiple threads //Use a reference type when: //You want to create shared, mutable state. //CLASS class DogClass { var name: String init(name: String) { self.name = name } } let aDogClass = DogClass(name: "A-Doggo Class") print(aDogClass.name) //prints "A-Doggo" var bDogClass = aDogClass bDogClass.name = "B-Doggo Class" print(aDogClass.name) //prints "B-Doggo" print(bDogClass.name) //prints "B-Doggo" //STRUCT struct DogStruct { var name: String init(name: String) { self.name = name } } let aDogStruct = DogStruct(name: "A-Doggo Struct") print(aDogStruct.name) // prints "A-Doggo" var bDogStruct = aDogStruct bDogStruct.name = "B-Doggo Struct" print(aDogStruct.name) //prints "A-Doggo" print(bDogStruct.name) //prints "B-Doggo" //Protocols and Protocol Extensions in Swift // Protocols are a good way to define a set of required functionality that other types can adopt. //When I think of protocols, I like to think that protocols provide information about what a type can do, not necessarily what it is. //Classes and structs provide you with information about what an object is, but protocols provide you with information about what an object does. protocol CompanyPeople{ var name:String { get } var lastname:String? { get } var floor:Int{ get set } func presentation() func currentBonus() mutating func changeFloor(newFloor:Int) } struct Developer : CompanyPeople { let name:String var lastname:String? var floor:Int var typeName: String { return String(describing: Developer.self) } init(name: String, floor: Int) { self.name = name self.floor = floor } func presentation(){ print("Hi I'm \(self.name) I'm working in the \(self.typeName) department at \(self.floor) floor") } func currentBonus(){} mutating func changeFloor(newFloor:Int){ self.floor = newFloor } } class Sysadmin : CompanyPeople { var name:String var lastname:String? var floor:Int = 2 var typeName: String { return String(describing: Sysadmin.self) } required init(name:String, lastname:String){ self.name = name self.lastname = lastname } func presentation(){ print("Hi I'm \(self.name) I'm working in the \(self.typeName) department at \(self.floor) floor") } func currentBonus(){} func changeFloor(newFloor:Int){} } class Administration : CompanyPeople { var name:String var lastname: String? var floor:Int = 3 var typeName: String { return String(describing: Administration.self) } required init(name:String, lastname:String){ self.name = name self.lastname = lastname } func presentation(){ print("Hi I'm \(self.name) I'm working in the \(self.typeName) department at \(self.floor) floor") } func currentBonus(){} func changeFloor(newFloor:Int){} } var dev = Developer(name:"Sil",floor:1) dev.changeFloor(newFloor:2); let admin = Administration(name:"Mariana",lastname:"Pepi") let sysadmin = Administration(name:"Mauricio",lastname:"Nolose") var companyPeople :[CompanyPeople] = [CompanyPeople]() companyPeople.append(dev) companyPeople.append(admin) companyPeople.append(sysadmin) let companyPeopleList = companyPeople.map{ $0.presentation() } //Closures //https://medium.com/@abhimuralidharan/functional-swift-all-about-closures-310bc8af31dd //{ (params) -> returnType in //statements //} let closure: (Int, Int) -> Int = { (num1, num2) in return num1 + num2 } print(closure(8,2)) var shortHandClosure:(Int,Int)->Int = { return $0 + $1 } print(shortHandClosure(8,2)) //PROPERTY //Readonly struct Cuboid { var width = 0.0, height = 0.0, depth = 0.0 var volume: Double { return width * height * depth } } let fourByFiveByTwo = Cuboid(width: 4.0, height: 5.0, depth: 2.0) print("the volume of fourByFiveByTwo is \(fourByFiveByTwo.volume)") // Prints "the volume of fourByFiveByTwo is 40.0" //Property Observers class BeerCounterClass{ var totalBeers: Int = 0 { willSet(newTotalBeer) { //willSet is called just before the value is stored. print("You take \(newTotalBeer) beers in total") } didSet { //didSet is called immediately after the new value is stored. if totalBeers > oldValue { print(" Take it easy man . you added \(totalBeers - oldValue) beers to the count") } } } } let beerCounter = BeerCounterClass() beerCounter.totalBeers = 1 beerCounter.totalBeers = 3 beerCounter.totalBeers = 8 //Computed Properties struct Point { var x = 0.0, y = 0.0 } struct Size { var width = 0.0, height = 0.0 } struct Rect { var origin = Point() var size = Size() var center: Point { get { let centerX = origin.x + (size.width / 2) let centerY = origin.y + (size.height / 2) return Point(x: centerX, y: centerY) } set(newCenter) { origin.x = newCenter.x - (size.width / 2) origin.y = newCenter.y - (size.height / 2) } } } var square = Rect(origin: Point(x: 0.0, y: 0.0), size: Size(width: 10.0, height: 10.0)) let initialSquareCenter = square.center square.center = Point(x: 15.0, y: 15.0) print("square.origin is now at (\(square.origin.x), \(square.origin.y))") //Type properties //You have access to them without needing to create a new object of that specific type. struct SomeStructure { static var storedTypeProperty = "Some value." static var computedTypeProperty: Int { return 1 } } enum SomeEnumeration { static var storedTypeProperty = "Some value." static var computedTypeProperty: Int { return 6 } } class SomeClass { static var storedTypeProperty = "Some value." static var computedTypeProperty: Int { return 27 } class var overrideableComputedTypeProperty: Int { return 107 } } //Subscripts //Subscripts are used to access information from a collection, sequence and a list in Classes, Structures and Enumerations without using a method. class DaysofaweekClass { private var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "saturday"] subscript(index: Int) -> String { get { return days[index] } set(newValue) { self.days[index] = newValue } } } var p = DaysofaweekClass() print(p[0]) p[0] = "Monday" print(p[0]) //Initializers ( use before) //Initializers, are like special methods that can be called to create a new instance of a particular type. //Swift defines two kinds of initializers for class types to help ensure all stored properties receive an initial value. //Designated initializers; Designated initializers are the primary initializers for a class. A designated initializer fully initializes all properties introduced by that class and calls an appropriate superclass initializer to continue the initialization process up the superclass chain. //Convenience initializers: Convenience initializers are secondary, supporting initializers for a class. You can define a convenience initializer to call a designated initializer from the same class as the convenience initializer with some of the designated initializer’s parameters set to default values. You can also define a convenience initializer to create an instance of that class for a specific use case or input value type. //Deinitializer:(deinit) A deinitializer is called immediately before a class instance is deallocated. You write deinitializers with the deinit keyword, similar to how initializers are written with the init keyword. Deinitializers are only available on class types. //Optional Chaining func albumReleased(year: Int) -> String? { switch year { case 2006: return "Taylor Swift" case 2008: return "Fearless" case 2010: return "Speak Now" case 2012: return "Red" case 2014: return "1989" default: return nil } } //let album = albumReleased(year: 2003).uppercased() //boom let album = albumReleased(year: 2003)?.uppercased() ?? "unknown" print("The album is \(album)") //Type Casting for Any and AnyObject var aLotOfthings = [Any]() aLotOfthings.append(0) aLotOfthings.append(0.0) aLotOfthings.append(42) aLotOfthings.append(3.14159) aLotOfthings.append("hello") aLotOfthings.append((3.0, 5.0)) aLotOfthings.append({ (name: String) -> String in "Hello, \(name)" }) print(aLotOfthings) for aLotOfthing in aLotOfthings { switch aLotOfthing { case let someInt as Int: print("an integer value of \(someInt)") case is Double: print("some other double value that I don't want to print") case let someString as String: print("a string value of \"\(someString)\"") case let (x, y) as (Double, Double): print("an (x, y) point at \(x), \(y)") case let stringConverter as (String) -> String: print(stringConverter("Michael")) default: print("something else") } } //Nested Types struct Fruit { enum RedFruit:Character { case apple = "🍎", strawberry = "🍓" enum Size: Int{ case small = 1, medium, big } } var myFruit: RedFruit? var myFruitSize: RedFruit.Size? var description : String { return String(myFruit?.rawValue ?? " ") } } var fruit = Fruit() fruit.myFruit = Fruit.RedFruit.strawberry fruit.myFruitSize = Fruit.RedFruit.Size.medium print(fruit.description) //ARC AUTOMATIC REFERENCE COUNTING //https://cocoacasts.com/what-is-automatic-reference-counting-arc // public class SomePublicClass {} //internal class SomeInternalClass {} //fileprivate class SomeFilePrivateClass {} //private class SomePrivateClass {} //public var somePublicVariable = 0 //internal let someInternalConstant = 0 //fileprivate func someFilePrivateFunction() {} //private func somePrivateFunction() {} //OPERATOR //https://developerinsider.co/advanced-operators-bitwise-by-example-swift-programming-language/