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
| func geNowPlaying(completion: @escaping (Result<MoviesPage, Error>) -> Void) { | |
| URLSession.shared | |
| .dataTask(with: url) { result in | |
| completion( | |
| result | |
| .map(\.data) | |
| .decode(MoviesPage.self, using: JSONDecoder()) | |
| ) | |
| }.resume() | |
| } |
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
| public protocol DecodableDataDecoder { | |
| func decode<T>(_ type: T.Type, from data: Data) throws -> T where T: Decodable | |
| } | |
| extension JSONDecoder: DecodableDataDecoder {} | |
| extension PropertyListDecoder: DecodableDataDecoder {} | |
| public extension Result where Success == Data { | |
| func decode<T: Decodable>(_ type: T.Type, using decoder: DecodableDataDecoder) -> Result<T, Error> { | |
| do { |
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
| public extension Result where Success == (data: Data, response: URLResponse) { | |
| func map<T>(_ keyPath: KeyPath<Success, T>) -> Result<T, Failure> { | |
| switch self { | |
| case .success(let response): | |
| return .success(response[keyPath: keyPath]) | |
| case .failure(let error): | |
| return .failure(error) | |
| } | |
| } | |
| } |
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
| public extension URLSession { | |
| typealias DataTaskResult = Result<(data: Data, response: URLResponse), Error> | |
| typealias DataTaskResultCompletion = (DataTaskResult) -> Void | |
| func dataTask( | |
| with url: URL, | |
| completion: @escaping DataTaskResultCompletion | |
| ) -> URLSessionDataTask { | |
| dataTask(with: url) { (data, response, error) in | |
| if let error = error { |
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
| // Obj-C | |
| - (NSURLSessionDataTask *)dataTaskWithURL:(NSURL *) | |
| urlcompletionHandler:(void (^)( | |
| NSData * _Nullable data, | |
| NSURLResponse * _Nullable response, | |
| NSError * _Nullable error) | |
| )completionHandler; | |
| // Swift | |
| func dataTask( |
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
| URLSession.shared | |
| .dataTaskPublisher(for: url) | |
| .map(\.data) | |
| .decode(type: MoviesPage.self, decoder: JSONDecoder()) |
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
| let url: URL = "https://api.themoviedb.org/3/movie/now_playing?api_key=\(apiKey)&page=1" | |
| URLSession.shared | |
| .dataTaskPublisher(for: url) | |
| .map(\.data) | |
| .decode(type: MoviesPage.self, decoder: JSONDecoder()) |
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
| import UIKit | |
| public extension UIApplication { | |
| /// Returns the topmost view controller in the hierarchy of the given view controller. | |
| /// | |
| /// If no parameter passed, by default the function takes the root view controller set for the key window in the application. | |
| /// | |
| /// ```swift | |
| /// UIApplication.shared.keyWindow?.rootViewController | |
| /// ``` |
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
| // Playground | |
| protocol Option: RawRepresentable, Hashable, CaseIterable {} | |
| extension Set where Element: Option { | |
| var rawValue: Int { | |
| var rawValue = 0 | |
| for (index, element) in Element.allCases.enumerated() where contains(element) { | |
| rawValue |= (1 << index) | |
| } |
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
| extension KeyedDecodingContainer where Key: CodingKey { | |
| func decodeDate(from key: Key, using formats: String...) throws -> Date { | |
| let dateAsString = try decode(String.self, forKey: key) | |
| let dateFormatter = DateFormatter() | |
| for format in formats { | |
| dateFormatter.dateFormat = format | |
| guard let date = dateFormatter.date(from: dateAsString) else { | |
| continue | |
| } | |
| return date |