Skip to content

Instantly share code, notes, and snippets.

View hossamghareeb's full-sized avatar

Hossam Ghareeb hossamghareeb

View GitHub Profile
@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 / AppDelegate.swift
Created January 3, 2021 11:10
Bypassing AppDelegate and Scene delegate for Unit testing in iOS
Remove @main or @UIApplicationMain
@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 / 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 / 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() }
}