Skip to content

Instantly share code, notes, and snippets.

View eMdOS's full-sized avatar

Emilio Ojeda eMdOS

  • Zapopan, Jalisco
View GitHub Profile
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()
}
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 {
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)
}
}
}
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 {
// Obj-C
- (NSURLSessionDataTask *)dataTaskWithURL:(NSURL *)
urlcompletionHandler:(void (^)(
NSData * _Nullable data,
NSURLResponse * _Nullable response,
NSError * _Nullable error)
)completionHandler;
// Swift
func dataTask(
@eMdOS
eMdOS / Combine Publishers Example.swift
Created May 1, 2020 23:36
Medium: Extending the Swift's Result type
URLSession.shared
.dataTaskPublisher(for: url)
.map(\.data)
.decode(type: MoviesPage.self, decoder: JSONDecoder())
@eMdOS
eMdOS / combine example.swift
Created May 1, 2020 23:29
Medium: Extending the Swift's Result type
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())
@eMdOS
eMdOS / UIApplication.swift
Created August 23, 2019 23:11
Topmost view controller
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
/// ```
@eMdOS
eMdOS / Enum+Option.swift
Created August 23, 2019 21:10
Enum + Option -> Modern OptionSet
// 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)
}
@eMdOS
eMdOS / KeyedDecodingContainer.swift
Last active August 23, 2019 23:15
Decoding custom date formats using Decodable
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