Loading...
Loading...
Swift Combine framework for reactive programming, handling asynchronous events with publishers, subscribers, and operators.
npx skill4agent add swiftzilla/skills swift_combineimport Combine
// Published property
@Published var username: String = ""
// CurrentValueSubject
let subject = CurrentValueSubject<String, Never>("initial")
// PassthroughSubject
let passthrough = PassthroughSubject<Int, Never>()
// Just publisher
let just = Just("value")
// Future publisher
let future = Future<String, Error> { promise in
promise(.success("result"))
}// Sink
let cancellable = publisher.sink(
receiveCompletion: { completion in
switch completion {
case .finished:
print("Completed")
case .failure(let error):
print("Error: \(error)")
}
},
receiveValue: { value in
print("Value: \(value)")
}
)
// Assign
let cancellable = publisher
.assign(to: \.text, on: label)private var cancellables = Set<AnyCancellable>()
publisher
.sink { value in
print(value)
}
.store(in: &cancellables)publisher
.filter { $0 > 0 }
.map { $0 * 2 }
.debounce(for: .seconds(0.5), scheduler: RunLoop.main)
.removeDuplicates()
.sink { value in
print(value)
}| Use Case | Combine | Async/Await |
|---|---|---|
| Event streams | ✅ Excellent | ⚠️ Complex |
| UI bindings | ✅ Perfect | ⚠️ Verbose |
| Chaining operations | ✅ Great | ✅ Good |
| Simple async calls | ⚠️ Overkill | ✅ Simple |
| Error handling | ✅ Rich | ✅ Good |