This file contains 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
extension Dictionary { | |
mutating func value(for key: Key, orAdd closure: @autoclosure () -> Value) -> Value { | |
if let value = self[key] { | |
return value | |
} | |
let value = closure() | |
self[key] = value | |
return value | |
} |
This file contains 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 Cache<T> { | |
private lazy var objects = [String : T]() | |
func object(forKey key: String) -> T? { | |
return objects[key] | |
} | |
func addObject(_ object: T, forKey key: String) { | |
objects[key] = object | |
} |
This file contains 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
import UIKit | |
// Create a protocol that defines what APIs that you need from the singleton | |
protocol Application { | |
func open(url: URL) | |
} | |
// Make the singleton-based class conform to your protocol | |
extension UIApplication: Application { | |
func open(url: URL) { |
This file contains 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
/** | |
* Perform a throwing expression, and throw a custom error in case the expression threw | |
* | |
* - parameter expression: The expression to execute | |
* - parameter error: The custom error to throw instead of the expression's error | |
* - throws: The given error | |
* - returns: The return value of the given expression | |
*/ | |
func perform<T>(_ expression: @autoclosure () throws -> T, orThrow errorExpression: @autoclosure () -> Error) throws -> T { | |
do { |
This file contains 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
/** | |
* Copyright (c) John Sundell 2017 | |
* Licensed under the MIT license | |
*/ | |
import Foundation | |
import XCTest | |
/** | |
* Assert that an expression throws a given error |
This file contains 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
import UIKit | |
class OnboardingManager { | |
private let userDefaults: UserDefaults | |
init(userDefaults: UserDefaults = .standard) { | |
self.userDefaults = userDefaults | |
} | |
func presentOnboardingControllerIfNeeded(in viewController: UIViewController) { |
This file contains 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
func loadSearchData(matching query: String) throws -> Data { | |
let urlString = "https://my.api.com/search?q=\(query)" | |
guard let url = URL(string: urlString) else { | |
throw SearchError.invalidQuery(query) | |
} | |
return try Data(contentsOf: url) | |
} |
This file contains 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
func loadSearchData(matching query: String) throws -> Data { | |
let urlString = "https://my.api.com/search?q=\(query)" | |
guard let url = URL(string: urlString) else { | |
throw SearchError.invalidQuery(query) | |
} | |
do { | |
return try Data(contentsOf: url) | |
} catch { |
This file contains 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
func perform<T>(_ expression: @autoclosure () throws -> T, | |
orThrow error: Error) throws -> T { | |
do { | |
return try expression() | |
} catch { | |
throw error | |
} | |
} |
This file contains 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
func loadSearchData(matching query: String) throws -> Data { | |
let urlString = "https://my.api.com/search?q=\(query)" | |
guard let url = URL(string: urlString) else { | |
throw SearchError.invalidQuery(query) | |
} | |
return try perform(Data(contentsOf: url), | |
orThrow: SearchError.dataLoadingFailed(url)) | |
} |
OlderNewer