This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController { | |
var manager: Singleton { | |
return Singleton.shared | |
} | |
} | |
// in unit testing target | |
class TestingViewController: ViewController { | |
override var manager: Singleton { Singleton() } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Remove @main or @UIApplicationMain |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// No memberwise initializer | |
struct Player { | |
let name: String | |
var score: Int = 0 | |
init(json: [String: AnyObject]) { | |
} | |
} | |
// you have the both initializers: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Player { | |
let name: String | |
var score: Int = 0 | |
} | |
let p1 = Player(name: "John") | |
let p2 = Player(name: "Sam", score: 100) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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 { |
NewerOlder