Skip to content

Instantly share code, notes, and snippets.

@silnose
Last active May 3, 2019 20:22
Show Gist options
  • Select an option

  • Save silnose/c0f6103f11f649cd7abba63c0503d6e0 to your computer and use it in GitHub Desktop.

Select an option

Save silnose/c0f6103f11f649cd7abba63c0503d6e0 to your computer and use it in GitHub Desktop.

Revisions

  1. silnose revised this gist May 3, 2019. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions main.swift
    Original file line number Diff line number Diff line change
    @@ -536,3 +536,23 @@ 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/
  2. silnose revised this gist May 3, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.swift
    Original file line number Diff line number Diff line change
    @@ -527,7 +527,7 @@ struct Fruit {
    var myFruit: RedFruit?
    var myFruitSize: RedFruit.Size?
    var description : String {
    return String(myFruit?.rawValue ?? "a")
    return String(myFruit?.rawValue ?? " ")
    }
    }

  3. silnose revised this gist May 3, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.swift
    Original file line number Diff line number Diff line change
    @@ -527,7 +527,7 @@ struct Fruit {
    var myFruit: RedFruit?
    var myFruitSize: RedFruit.Size?
    var description : String {
    return String(myFruit?.rawValue ?? '')
    return String(myFruit?.rawValue ?? "a")
    }
    }

  4. silnose revised this gist May 3, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.swift
    Original file line number Diff line number Diff line change
    @@ -527,7 +527,7 @@ struct Fruit {
    var myFruit: RedFruit?
    var myFruitSize: RedFruit.Size?
    var description : String {
    return String(myFruit?.rawValue ?? "")
    return String(myFruit?.rawValue ?? '')
    }
    }

  5. silnose revised this gist May 3, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.swift
    Original file line number Diff line number Diff line change
    @@ -527,7 +527,7 @@ struct Fruit {
    var myFruit: RedFruit?
    var myFruitSize: RedFruit.Size?
    var description : String {
    return String(myFruit?.rawValue)
    return String(myFruit?.rawValue ?? "")
    }
    }

  6. silnose revised this gist May 3, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.swift
    Original file line number Diff line number Diff line change
    @@ -527,7 +527,7 @@ struct Fruit {
    var myFruit: RedFruit?
    var myFruitSize: RedFruit.Size?
    var description : String {
    return String("\(myFruit?.rawValue)")
    return String(myFruit?.rawValue)
    }
    }

  7. silnose revised this gist May 3, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.swift
    Original file line number Diff line number Diff line change
    @@ -527,7 +527,7 @@ struct Fruit {
    var myFruit: RedFruit?
    var myFruitSize: RedFruit.Size?
    var description : String {
    return String(myFruit?.rawValue)
    return String("\(myFruit?.rawValue)")
    }
    }

  8. silnose revised this gist May 3, 2019. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions main.swift
    Original file line number Diff line number Diff line change
    @@ -516,8 +516,7 @@ for aLotOfthing in aLotOfthings {

    struct Fruit {

    enum RedFruit: Character{

    enum RedFruit:Character {
    case apple = "🍎", strawberry = "🍓"
    enum Size: Int{
    case small = 1, medium, big
  9. silnose revised this gist May 3, 2019. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion main.swift
    Original file line number Diff line number Diff line change
    @@ -527,10 +527,13 @@ struct Fruit {

    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.myFruit)
    print(fruit.description)
  10. silnose revised this gist May 3, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.swift
    Original file line number Diff line number Diff line change
    @@ -533,4 +533,4 @@ struct Fruit {
    var fruit = Fruit()
    fruit.myFruit = Fruit.RedFruit.strawberry
    fruit.myFruitSize = Fruit.RedFruit.Size.medium
    print(fruit)
    print(fruit.myFruit)
  11. silnose revised this gist May 3, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.swift
    Original file line number Diff line number Diff line change
    @@ -518,7 +518,7 @@ struct Fruit {

    enum RedFruit: Character{

    case apple ="🍎", strawberry = "🍓"
    case apple = "🍎", strawberry = "🍓"
    enum Size: Int{
    case small = 1, medium, big
    }
  12. silnose revised this gist May 3, 2019. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions main.swift
    Original file line number Diff line number Diff line change
    @@ -510,3 +510,27 @@ for aLotOfthing in aLotOfthings {
    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 fruit = Fruit()
    fruit.myFruit = Fruit.RedFruit.strawberry
    fruit.myFruitSize = Fruit.RedFruit.Size.medium
    print(fruit)
  13. silnose revised this gist May 3, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion main.swift
    Original file line number Diff line number Diff line change
    @@ -494,7 +494,8 @@ print(aLotOfthings)


    for aLotOfthing in aLotOfthings {
    switch aLotOfthings {

    switch aLotOfthing {
    case let someInt as Int:
    print("an integer value of \(someInt)")
    case is Double:
  14. silnose revised this gist May 3, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions main.swift
    Original file line number Diff line number Diff line change
    @@ -493,8 +493,8 @@ aLotOfthings.append({ (name: String) -> String in "Hello, \(name)" })
    print(aLotOfthings)


    for thing in things {
    switch thing {
    for aLotOfthing in aLotOfthings {
    switch aLotOfthings {
    case let someInt as Int:
    print("an integer value of \(someInt)")
    case is Double:
  15. silnose revised this gist May 3, 2019. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion main.swift
    Original file line number Diff line number Diff line change
    @@ -493,7 +493,6 @@ aLotOfthings.append({ (name: String) -> String in "Hello, \(name)" })
    print(aLotOfthings)



    for thing in things {
    switch thing {
    case let someInt as Int:
  16. silnose revised this gist May 3, 2019. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions main.swift
    Original file line number Diff line number Diff line change
    @@ -494,3 +494,19 @@ print(aLotOfthings)



    for thing in things {
    switch thing {
    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")
    }
    }
  17. silnose revised this gist May 3, 2019. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions main.swift
    Original file line number Diff line number Diff line change
    @@ -477,4 +477,20 @@ 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)



  18. silnose revised this gist May 3, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.swift
    Original file line number Diff line number Diff line change
    @@ -471,7 +471,7 @@ func albumReleased(year: Int) -> String? {
    }

    //let album = albumReleased(year: 2003).uppercased() //boom
    let album = albumReleased(year: 2006)?.uppercased() ?? "unknown"
    let album = albumReleased(year: 2003)?.uppercased() ?? "unknown"

    print("The album is \(album)")

  19. silnose revised this gist May 3, 2019. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions main.swift
    Original file line number Diff line number Diff line change
    @@ -470,8 +470,9 @@ func albumReleased(year: Int) -> String? {
    }
    }

    let album = albumReleased(year: 2003).uppercased()
    //let album = albumReleased(year: 2003)?.uppercased()
    //let album = albumReleased(year: 2003).uppercased() //boom
    let album = albumReleased(year: 2006)?.uppercased() ?? "unknown"

    print("The album is \(album)")


  20. silnose revised this gist May 3, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion main.swift
    Original file line number Diff line number Diff line change
    @@ -470,7 +470,8 @@ func albumReleased(year: Int) -> String? {
    }
    }

    let album = albumReleased(year: 2003)?.uppercased()
    let album = albumReleased(year: 2003).uppercased()
    //let album = albumReleased(year: 2003)?.uppercased()
    print("The album is \(album)")


  21. silnose revised this gist May 3, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.swift
    Original file line number Diff line number Diff line change
    @@ -470,7 +470,7 @@ func albumReleased(year: Int) -> String? {
    }
    }

    let album = albumReleased(year: 2003)
    let album = albumReleased(year: 2003)?.uppercased()
    print("The album is \(album)")


  22. silnose revised this gist May 3, 2019. 1 changed file with 32 additions and 0 deletions.
    32 changes: 32 additions & 0 deletions main.swift
    Original file line number Diff line number Diff line change
    @@ -444,3 +444,35 @@ 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)
    print("The album is \(album)")




  23. silnose revised this gist May 3, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.swift
    Original file line number Diff line number Diff line change
    @@ -242,7 +242,7 @@ struct Developer : CompanyPeople {
    var typeName: String {
    return String(describing: Developer.self)
    }
    init(name: String, floor: String) {
    init(name: String, floor: Int) {
    self.name = name
    self.floor = floor
    }
  24. silnose revised this gist May 3, 2019. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions main.swift
    Original file line number Diff line number Diff line change
    @@ -242,6 +242,10 @@ struct Developer : CompanyPeople {
    var typeName: String {
    return String(describing: Developer.self)
    }
    init(name: String, floor: String) {
    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")
    }
  25. silnose revised this gist May 3, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions main.swift
    Original file line number Diff line number Diff line change
    @@ -254,7 +254,7 @@ struct Developer : CompanyPeople {

    class Sysadmin : CompanyPeople {
    var name:String
    var lastname:String
    var lastname:String?
    var floor:Int = 2
    var typeName: String {
    return String(describing: Sysadmin.self)
    @@ -274,7 +274,7 @@ class Sysadmin : CompanyPeople {
    class Administration : CompanyPeople {

    var name:String
    var lastname: String
    var lastname: String?
    var floor:Int = 3
    var typeName: String {
    return String(describing: Administration.self)
  26. silnose revised this gist May 3, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.swift
    Original file line number Diff line number Diff line change
    @@ -227,7 +227,7 @@ print(bDogStruct.name) //prints "B-Doggo"
    protocol CompanyPeople{

    var name:String { get }
    var lastname:String { get }
    var lastname:String? { get }
    var floor:Int{ get set }
    func presentation()
    func currentBonus()
  27. silnose revised this gist May 3, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.swift
    Original file line number Diff line number Diff line change
    @@ -237,7 +237,7 @@ protocol CompanyPeople{
    struct Developer : CompanyPeople {

    let name:String
    let lastname:String?
    var lastname:String?
    var floor:Int
    var typeName: String {
    return String(describing: Developer.self)
  28. silnose revised this gist May 3, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions main.swift
    Original file line number Diff line number Diff line change
    @@ -237,7 +237,7 @@ protocol CompanyPeople{
    struct Developer : CompanyPeople {

    let name:String
    let lastname:String
    let lastname:String?
    var floor:Int
    var typeName: String {
    return String(describing: Developer.self)
    @@ -292,7 +292,7 @@ class Administration : CompanyPeople {

    }

    var dev = Developer(name:"Sil",lastname:"Nose",floor:1)
    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")
  29. silnose revised this gist May 3, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.swift
    Original file line number Diff line number Diff line change
    @@ -436,7 +436,7 @@ private var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Fri


    }
    var p = daysofaweek()
    var p = DaysofaweekClass()
    print(p[0])
    p[0] = "Monday"
    print(p[0])
  30. silnose revised this gist May 3, 2019. 1 changed file with 94 additions and 0 deletions.
    94 changes: 94 additions & 0 deletions main.swift
    Original file line number Diff line number Diff line change
    @@ -325,6 +325,20 @@ 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{
    @@ -346,3 +360,83 @@ 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 = daysofaweek()
    print(p[0])
    p[0] = "Monday"
    print(p[0])