Comments (10)
Would something like this swiftui project help?
from swiftui-notes.
I'll definitely show using a textfield getting updates within the patterns - both with SwiftUI and with UIKit. I might pass on AppKit for starters though, since I'm not yet running the beta on the Mac
from swiftui-notes.
Perhaps my question was not clear. I was asking about using the changes of a Textfield as source for a pipeline, like shown in the WWDC video.
from swiftui-notes.
I think I understand - The current proposed method based on what I can find in apple's docs is leveraging the NSNotificationCenter for all UIKit updates, and then using that as the publisher to which you can react. Casey Liss even has some sample bits out there (he's not a fan of using NSNoticationCenter in this way, and I can't say I blame him here- https://www.caseyliss.com/2019/6/18/quick-notes-on-yesterdays-post) , as does @AvdLee in his playground at https://github.com/AvdLee/CombineSwiftPlayground. There's also something of an example at https://github.com/tailec/CombineExamples if you want to dig around a bit in that code.
I need to sit down and make it work for myself in my own samples, and I intend to include something like this, as its clearly a pattern folks would like to use.
from swiftui-notes.
Very interesting links. Thank you for sharing them. Though they all use UIKit. I wanted to know the easiest way to get a publisher from a SwiftUI TextField.
This should be as easy as calling a method like textfield.publisher(...) but it isn't. Or do I miss something.
from swiftui-notes.
I haven't looked at depth into SwiftUI to really have a solid answer yet, but in perusing the documentation there's a closure that you can set up (at initialization) to be called when the contents are edited (https://developer.apple.com/documentation/swiftui/textfield/3338359-init), and there may also be a direct way to look for the property being modified, such as @published property wrapper - but I'm getting quite a bit out ahead of anything I've tried to really know as yet.
SwiftUI includes a number of publishers built into their views, but I'm not clear on what they are or their uses as yet.
from swiftui-notes.
It also appears that initializing TextFields can be bound to a model, so it may be more sensible (depending on how you're using it), to get a publisher from the model - I suspect it depends entirely on where the source of truth is for the TextField - such as @State
or @ObjectBinding
from swiftui-notes.
Thanks, that seems to contain the necessary code. I still have to remove the URL specific features though to extract the essence.
from swiftui-notes.
In the meantime I found a solution myself. The key to it was that $property
of a @Published
property is a Publisher itself:
import PlaygroundSupport
import SwiftUI
import Combine
PlaygroundPage.current.needsIndefiniteExecution = true
class TextEngine:ObservableObject{
@Published var textInput = ""
@Published var textOutput = ""
private var worker:AnyCancellable? = nil
init(){
worker = $textInput.print("raw input")
.debounce(for: 2, scheduler: RunLoop.main)
.removeDuplicates{$0 == $1}
.print("debounced and duplicates removed")
.assign(to:\.textOutput, on:self)
}
}
let engine = TextEngine()
struct LiveView:View {
@ObservedObject var engine:TextEngine
var body: some View {
VStack{
TextField("Enter text", text: $engine.textInput)
.textFieldStyle(RoundedBorderTextFieldStyle())
Text(engine.textOutput)
Spacer()
}.padding()
}
}
let liveView = UIHostingController(rootView: LiveView(engine: engine).frame(width:770, height:400))
liveView.preferredContentSize = CGSize(width: 770, height: 1100)
PlaygroundPage.current.liveView = liveView
from swiftui-notes.
I've got a model/example now properly in place, as of the pr #25
from swiftui-notes.
Related Issues (20)
- "Operator is an object that acts both like a subscriber and a publisher" - Is this true? HOT 3
- `allSatisfy` description is misleading HOT 1
- Simplify Switch statement HOT 1
- Typo on Core Concepts Page HOT 3
- Updated Error Message Attempting To Assign A Non-Optional Type To A Keypath That Expects Has An Optional Type HOT 2
- update throttle to debounce in example
- Debounce, Throttle tests need to be broken up, potentially re-written HOT 1
- Typo in using-combine-book.adoc metadata HOT 1
- Details for the prepend operation is missing a "be" HOT 1
- Extra word in sentence HOT 1
- Minor typo HOT 1
- Paypal HOT 2
- How to export and generate docset format? HOT 4
- F966D9F1-3910-48BE-AF43-2471E84BC861
- testRetryWithOneShotFailPublisher unit test issue
- Add illustrations for "merge" & "zip" operators HOT 2
- Consider mentioning the Exponential Backoff approach in "retry" pattern
- Throttle and debounce timing diagram maybe wrong
- Doesn't build with Xcode 14.1 (with built-in MacOSX13.0.sdk) on macOS 12 Monterey HOT 4
- missing code in Sequencing asynchronous operations HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftui-notes.