Skip to content

Instantly share code, notes, and snippets.

View hossamghareeb's full-sized avatar

Hossam Ghareeb hossamghareeb

View GitHub Profile
@hossamghareeb
hossamghareeb / singletons.swift
Created January 6, 2021 17:59
Subclass then override for singletons
class ViewController {
var manager: Singleton {
return Singleton.shared
}
}
// in unit testing target
class TestingViewController: ViewController {
override var manager: Singleton { Singleton() }
}
@hossamghareeb
hossamghareeb / singletons-backdoor.swift
Created January 6, 2021 14:51
Singletons backdoor for unit testing
class Singleton {
private static var instance = Singleton()
#if DEBUG
static var stubbedInstance: Singleton?
#endif
static var shared: Singleton {
#if DEBUG
if let stubbedInstance = stubbedInstance { return stubbedInstance }
#endif
@hossamghareeb
hossamghareeb / UIViewController+Storyboard.swift
Created January 3, 2021 12:02
instantiate view controller from storyboard
// Assuming you use one storyboard per view controller
extension UIViewController {
static func fromStoryboard() -> Self {
let name = String(describing: Self.self)
let sb = UIStoryboard(name: name, bundle: nil)
return sb.instantiateViewController(identifier: name) as! Self
}
}
@hossamghareeb
hossamghareeb / AppDelegate.swift
Created January 3, 2021 11:10
Bypassing AppDelegate and Scene delegate for Unit testing in iOS
Remove @main or @UIApplicationMain
@hossamghareeb
hossamghareeb / memberwise2.swift
Created May 11, 2020 21:56
memberwise init with custom init
// No memberwise initializer
struct Player {
let name: String
var score: Int = 0
init(json: [String: AnyObject]) {
}
}
// you have the both initializers:
@hossamghareeb
hossamghareeb / memberwise.swift
Created May 11, 2020 21:49
structs memberwise init
struct Player {
let name: String
var score: Int = 0
}
let p1 = Player(name: "John")
let p2 = Player(name: "Sam", score: 100)
@hossamghareeb
hossamghareeb / dynamic.swift
Created February 24, 2019 14:58
Dynamic UITable header view
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
updateTableViewHeaderHeight()
}
private func updateTableViewHeaderHeight() {
guard let headerView = tableView.tableHeaderView else { return }
/// Get the size that should meet the constraints
let size = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
guard size.height != headerView.frame.height else { return }
@hossamghareeb
hossamghareeb / swizzle.swift
Created November 6, 2018 11:35
Swizzle and print all method of a class
private func swizzle(method: String, of class: String, to newMethod: String) {
let original: Method
let swizzled: Method
guard let aClass = objc_getClass(`class`) as? AnyClass else { return }
original = class_getInstanceMethod(aClass, Selector((method)))!
swizzled = class_getInstanceMethod(aClass, Selector((newMethod)))!
method_exchangeImplementations(original, swizzled)
}
@hossamghareeb
hossamghareeb / partition.swift
Created September 20, 2018 06:00
Partition in Swift
var arr = [1, 2, 5, 5, 6, 7, 10]
let pivot = arr.partition { $0 % 2 == 0 }
arr // [1, 7, 5, 5, 6, 2, 10]
let part1 = arr[..<pivot] // odd numbers
let part2 = arr[pivot...] // even numbers
@hossamghareeb
hossamghareeb / binary-search.swift
Created April 16, 2018 18:50
Binary search Swift
/**
Returns index of item if found. Otherwise returns -1 * position where the item should be insertd to
*/
func binarySearch(arr: [Int], start: Int, end: Int, k: Int) -> Int {
var start = start
var end = end
var mid = 0
while start <= end {
mid = start + (end - start) / 2
if arr[mid] == k {