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
enum AsyncError: Error { | |
case finishedWithoutValue | |
} | |
extension AnyPublisher { | |
func async() async throws -> Output { | |
try await withCheckedThrowingContinuation { continuation in | |
var cancellable: AnyCancellable? | |
var finishedWithoutValue = true | |
cancellable = first() |
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
struct API { | |
//... | |
func loadTodo() -> AnyPublisher<Todo, Error> { | |
//... | |
} | |
} | |
class Client1 { | |
// 1. Make function async | |
func loadTodo() async { |
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 AnyPublisher { | |
func async() async throws -> Output { | |
try await withCheckedThrowingContinuation { continuation in | |
var cancellable: AnyCancellable? | |
cancellable = first() | |
.sink { result in | |
switch result { | |
case .finished: | |
break |
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
struct API { | |
let url = URL(string: "https://jsonplaceholder.typicode.com/todos/1")! | |
func loadTodo() async throws -> Todo { | |
let (data, _) = try await URLSession.shared.data(from: url) | |
return try JSONDecoder().decode(Todo.self, from: data) | |
} | |
} | |
class Client1 { |
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 Client1 { | |
var cancellable: AnyCancellable? | |
let api = API() | |
func loadTodo() { | |
cancellable = api.loadTodo() | |
.sink(receiveCompletion: { result in | |
switch result { | |
case .finished: | |
print("finished") |
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
struct API { | |
let url = URL(string: "https://jsonplaceholder.typicode.com/todos/1")! | |
func loadTodo() -> AnyPublisher<Todo, Error> { | |
URLSession.shared.dataTaskPublisher(for: url) | |
.map { $0.data } | |
.decode(type: Todo.self, decoder: JSONDecoder()) | |
.eraseToAnyPublisher() | |
} | |
} |
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
// Repository.swift | |
import Foundation | |
protocol Repository { | |
associatedtype Model | |
func get(id: String, completion: @escaping (Result<Model, Error>) -> Void) | |
func save(_ item: Model, completion: @escaping (Result<Model, Error>) -> Void) | |
func remove(_ item: Model, completion: @escaping (Result<Model, Error>) -> Void) | |
} |
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
struct MusicSheetPlotter<T: Prototype> where T.CloneType: UIView { | |
let quarterNote: T | |
func plot(in view: UIView) { | |
... | |
stack.addArrangedSubview(quarterNote.clone()) | |
stack.addArrangedSubview(quarterNote.clone()) | |
stack.addArrangedSubview(quarterNote.clone()) | |
... |
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
protocol Prototype { | |
associatedtype CloneType | |
func clone() -> CloneType | |
} | |
extension UILabel: Prototype { | |
... | |
// Don't forget to specialise the return type! | |
func clone() -> UILabel { | |
UILabel(self) |
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
struct MusicSheetPloter { | |
let quarterNote: UIView & Prototype | |
... | |
} |