Skip to content

Instantly share code, notes, and snippets.

@byJeevan
Last active June 7, 2020 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save byJeevan/29f1e20cf0a07e961e982890bd9c53a3 to your computer and use it in GitHub Desktop.
Save byJeevan/29f1e20cf0a07e961e982890bd9c53a3 to your computer and use it in GitHub Desktop.
Explore swift APIs that reduces lots of code effort
@byJeevan
Copy link
Author

byJeevan commented May 22, 2020

1. Leverage Protocols as parameters to function

protocol  Number {
    var floatValue:Float { get }
}

extension Double : Number {
    var floatValue:Float {
        return Float(self)
    }
}

extension Int : Number {
    var floatValue:Float {
        return Float(self)
    }
}

// Now you can write a method that can add any type of operands.
func +(left:Number, right:Number)  -> Float {
    return left.floatValue + right.floatValue;
}

//So that you can use

var x:Double = 1.2343
var y:Int = 5
var result = x+y ///6.2343

@byJeevan
Copy link
Author

byJeevan commented May 25, 2020

2. Protocol + Enum + Mutating function

/// Example Switch Status
protocol Toggler {
   mutating func toggle()
}

enum SwitchStatus : Toggler {
   case  on, off

   mutating func toggle() { //conforms Toggler protocol
       switch(self) {
       case .on:
           self = .off
       case .off:
           self = .on
       }
   }
}

var switchStatus = SwitchStatus.off /// defaults to - off
switchStatus.toggle() /// on
switchStatus == .on ///is true
switchStatus.toggle() ///off
switchStatus == .off ///is true

@byJeevan
Copy link
Author

byJeevan commented May 25, 2020

3. Summary of module - 1

Extensions are used to add new functionality to an existing class, structure, enumeration or protocol type. Extensions can be declared using the extension keyword.

A protocol defines a blueprint of methods, properties, and other requirements that suit a piece of functionality and can be adopted by a class, structure or enumeration. A protocol is defined similar to a class however, the protocol keyword is used.

Delegation is a simple way of connecting objects and communicate with each other and can be used to pass data between view controllers.

Mutating functions are functions that use the mutating keyword, this allows these functions to change the value of the variables in this structure or class.

Swift transformations are functions you can use on various collections and sequence types to permutate through and modify the data in an elegant way. For example the map, flatMap, compactMap, filter, reduce and zip.

@byJeevan
Copy link
Author

4. Data structures

///Factorial with recursion
func factorial(num:Int) -> Int {
if num == 1 { return 1 }
return num * factorial(num: num - 1)
}

factorial(num: 7) //gives 5040

@byJeevan
Copy link
Author

byJeevan commented May 25, 2020

4. Stack implementation (LIFO)

`

//Stack implementation (LIFO)
struct Stack {
    
    private var stackArray:[String] = []  
  
   func peak() -> String? {
        return self.isEmptyStack() ? nil : stackArray.first
    }
    
    mutating func pop() -> String? {
        return self.isEmptyStack() ? nil : stackArray.removeFirst() //returns removed element
    }
    
    mutating func push(element:String) {
        stackArray.append(element)
    }
    
    func isEmptyStack() -> Bool {
        return stackArray.count == 0
    }
}

extension Stack: CustomStringConvertible {
    var description: String {
        let topLogString = "-----STACK----- \n"
        let bottomLogString = "\n------*--------\n"
        let middleLogString = stackArray.joined(separator: "\n")
        return topLogString + middleLogString + bottomLogString
    }
}

//Testing
var stackTest = Stack()
stackTest.push(element: "Eleo")
stackTest.push(element: "Combo")
stackTest.push(element: "Rambo")

print(stackTest)
/*
-----STACK----- 
Eleo
Combo
Rambo
------*--------
*/
stackTest.peak()
stackTest.pop()
/*
-----STACK----- 
Combo
Rambo
------*--------
*/
print(stackTest)


`

//BONUS: Implement generic type of stack

@byJeevan
Copy link
Author

byJeevan commented Jun 7, 2020

Using Enums :

  • with combining Tuples for complex data structure.
  • Polymorphism representation of collections ex. Array. That gives compile time safety check.
  • Stay away from hierarchical architecture (means avoids subclass). Where we can add additional type with new property without re-arranging super class properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment