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 ViewController { | |
func addItems(items: [Item], to section: Section) { | |
var snapshot = dataSource.snapshot() | |
snapshot.appendSections([section]) | |
snapshot.appendItems(items, toSection: section) | |
dataSource.apply(snapshot) | |
} | |
} |
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 | |
enum Section { | |
case section1 | |
case section2 | |
} | |
struct Item: Hashable { | |
let color: UIColor | |
} |
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
@main | |
struct MainApp { | |
static func main() async { | |
await asyncFunction() | |
} | |
} |
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 ContentView: View { | |
var body: some View { | |
VStack { | |
Button("Async") { | |
Task { | |
await asyncFunction() | |
} | |
} | |
} | |
} |
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 SomeError: Error { | |
case foo | |
} | |
func completionFunction(completion: @escaping ([Data], error: @escaping (Error) -> Void) -> Void) { | |
getData(complete: completion) | |
} | |
func asyncFunctionWrapper() async -> Data { | |
await withCheckedThrowingContinuation { continuation in |
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 fetchItems() async throws { | |
for try await item in getAsyncSequence() { | |
print("Received item: \(item)") | |
} | |
} | |
try? await fetchItems() |
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 printData() async throws { | |
var iterator = getAsyncIterator() | |
if let item = try await iterator.next() { | |
print("The first item is \(item)") | |
} | |
while let item = try await iterator.next() { | |
print("\(item)") | |
} |
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
let loadDataTask = Task(priority: .medium) { () -> Data | |
let url = URL(string: "https://hws.dev/headlines.json")! | |
let (data, _) = try await URLSession.shared.data(from: url) | |
return data | |
} | |
let data = try await loadDataTask.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
let loadDataTask = Task { () -> Data? in | |
let url = URL(string: "https://www.sebastianboldt.com")! | |
let (data, _) = try await URLSession.shared.data(from: url) | |
return data | |
} | |
let result = try await loadDataTask.result | |
switch result { | |
case let .success(data): |
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 createText() async throws -> String { | |
// .... | |
} |