Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Khawaja Farooq khawajafarooq

🏠
Working from home
View GitHub Profile
@khawajafarooq
khawajafarooq / SimpleThreads.swift
Created Dec 15, 2018
Demonstration of app threads in swift
View SimpleThreads.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 Aug 22, 2018
Evaluate postfix operation
View EvaluatePostfix.swift
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 Aug 21, 2018
Lightweight property observer in swift
View Notifiable.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)
View ExpressionBalancing.swift
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 Mar 10, 2018
Find two maximum numbers from an array
View FindTwoMax.swift
import Foundation
func findTwoMax(_ array: [Int]) -> (Int, Int)? {
guard array.count > 1 else {
return nil
}
var max = Int.min
var secondMax = Int.min
View CardsDataStructure.swift
import Foundation
enum Suit {
case hearts
case spades
case diamonds
case clubs
}
extension Suit {
@khawajafarooq
khawajafarooq / YearError.swift
Created Dec 7, 2017
Custom error enum in swift
View YearError.swift
import Foundation
enum YearError: Error {
case empty
case invalidNumber
case invalidBirthYear
case invalidYear
}
extension YearError: CustomStringConvertible {
@khawajafarooq
khawajafarooq / ErrorAlert.swift
Created Dec 7, 2017
Error extension to support UIAlertController
View ErrorAlert.swift
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 Dec 5, 2017
Add 1 number in an array - Geeks for geeks
View addOneNumber.swift
/* 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 Nov 22, 2017
Creating app themes with swift enum
View AppTheme.swift
import UIKit
public enum AppColor {
case blue
case gray
case graySeparator
var rgba: [CGFloat] {
switch self {