Skip to content

Instantly share code, notes, and snippets.

View khawajafarooq's full-sized avatar

Khawaja Farooq khawajafarooq

View GitHub Profile
@khawajafarooq
khawajafarooq / SimpleThreads.swift
Created December 15, 2018 14:43
Demonstration of app threads in swift
// Application threads
// Background
func BG(_ block: @escaping () -> Void) {
DispatchQueue.global(qos: .default).async(execute: block)
}
// Main
func UI(_ block: @escaping () -> Void) {
DispatchQueue.main.async(execute: block)
}
@khawajafarooq
khawajafarooq / EvaluatePostfix.swift
Created August 22, 2018 14:58
Evaluate postfix operation
var Operations: [String: (Double, Double) -> Double] = [
"*": { $0 * $1 },
"+": { $0 + $1 },
"-": { $1 - $0 },
"/": { $1 / $0 }
]
let Operators = ["+", "-", "*", "/"]
func postfix(_ expression: String) -> Double {
@khawajafarooq
khawajafarooq / Notifiable.swift
Created August 21, 2018 17:20
Lightweight property observer in swift
final class Notifiable<T> {
typealias CompletionHandler = ((T) -> ())
private var propertyChanged: CompletionHandler? = nil
var value: T {
didSet {
guard let observer = propertyChanged else { return }
observer(value)
@khawajafarooq
khawajafarooq / ExpressionBalancing.swift
Created July 29, 2018 20:27
swift bracket balancing
import Foundation
let openBrackets: [Character] = ["(", "{", "["]
let closedBrackets: [Character] = [")", "}", "]"]
// overloading ~= empowers your pattern mathing in switch
func ~=<T: Equatable>(pattern: [T], value: T) -> Bool {
return pattern.contains(value)
}
@khawajafarooq
khawajafarooq / FindTwoMax.swift
Created March 10, 2018 14:10
Find two maximum numbers from an array
import Foundation
func findTwoMax(_ array: [Int]) -> (Int, Int)? {
guard array.count > 1 else {
return nil
}
var max = Int.min
var secondMax = Int.min
import Foundation
enum Suit {
case hearts
case spades
case diamonds
case clubs
}
extension Suit {
@khawajafarooq
khawajafarooq / YearError.swift
Created December 7, 2017 12:50
Custom error enum in swift
import Foundation
enum YearError: Error {
case empty
case invalidNumber
case invalidBirthYear
case invalidYear
}
extension YearError: CustomStringConvertible {
@khawajafarooq
khawajafarooq / ErrorAlert.swift
Created December 7, 2017 10:42
Error extension to support UIAlertController
import UIKit
/// Extending error to make it alertable
extension Error {
/// displays alert from source controller
func alert(with controller: UIViewController) {
let alertController = UIAlertController(title: "Oops ❗️", message: "\(self)", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
@khawajafarooq
khawajafarooq / addOneNumber.swift
Created December 5, 2017 03:48
Add 1 number in an array - Geeks for geeks
/* Test Cases
Input: [1, 2, 3, 4]
Output: [1, 2, 3, 5]
Input: [1, 2, 9, 9]
Output: [1, 3, 0, 0]
Input: [9, 9, 9, 9]
Output: [1, 0, 0, 0]
@khawajafarooq
khawajafarooq / AppTheme.swift
Last active November 22, 2017 07:20
Creating app themes with swift enum
import UIKit
public enum AppColor {
case blue
case gray
case graySeparator
var rgba: [CGFloat] {
switch self {