Skip to content

Instantly share code, notes, and snippets.

View SebastianBoldt's full-sized avatar
:octocat:

Sebastian Boldt SebastianBoldt

:octocat:
View GitHub Profile
extension ViewController {
func addItems(items: [Item], to section: Section) {
var snapshot = dataSource.snapshot()
snapshot.appendSections([section])
snapshot.appendItems(items, toSection: section)
dataSource.apply(snapshot)
}
}
import UIKit
enum Section {
case section1
case section2
}
struct Item: Hashable {
let color: UIColor
}
@main
struct MainApp {
static func main() async {
await asyncFunction()
}
}
struct ContentView: View {
var body: some View {
VStack {
Button("Async") {
Task {
await asyncFunction()
}
}
}
}
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
func fetchItems() async throws {
for try await item in getAsyncSequence() {
print("Received item: \(item)")
}
}
try? await fetchItems()
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)")
}
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
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):
func createText() async throws -> String {
// ....
}