SwiftUI TextField initializer deprecated?

I've got this code:

TextField($searchText, placeholder:Text("Search"))


It's generating this error:

'init(_:placeholder:onEditingChanged:onCommit:)' is deprecated


As near as I can tell, there's no alternative. I'm not really an expert on reading Swift code yet, but to me it looks like all the inits have been deprecated. Is this a deprecation that was introduced in error? If not, what's the alternative?

Accepted Reply

It appears the API was updated but not the documentation, yet. If you command-click on TextField in Xcode it will take you to the SwiftUI file where the init()s are declared. The new init you want is


public init(_ titleKey: LocalizedStringKey, text: Binding, onEditingChanged: @escaping (Bool) -> Void = { _ in }, onCommit: @escaping () -> Void = {})

so for your example change the code to


TextField("Search", text: $searchText)

Replies

I ran into this as well and haven't figured out a way to suppress the warning either. I've filed a bug report via the Feedback Assistant in Catalina and will let you know if I hear anything. Otherwise I guess keep an eye out for a fix in Beta 4.

do you have a work around? alternative to textfield for user input? i have a test app that work in simulation mode in beta2 now it appears to hang and not load in beta3 sim.

It appears the API was updated but not the documentation, yet. If you command-click on TextField in Xcode it will take you to the SwiftUI file where the init()s are declared. The new init you want is


public init(_ titleKey: LocalizedStringKey, text: Binding, onEditingChanged: @escaping (Bool) -> Void = { _ in }, onCommit: @escaping () -> Void = {})

so for your example change the code to


TextField("Search", text: $searchText)

Ah, I think I know what you're seeing. The implemenation of SceneDelegate appears to have changed between beta 2 and 3.


Previously if you created a new SwiftUI project you'd see soemthing like this:


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        let window = UIWindow(frame: UIScreen.main.bounds)
        window.rootViewController = UIHostingController(rootView: ContentView())
        self.window = window
        window.makeKeyAndVisible()

}


As of beta 3, you'll need to cast the scene parameter as a UIWindowScene. Creating a new SwiftUI application with the Single View template now gives you something like this:


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
     if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: ContentView())
            self.window = window
            window.makeKeyAndVisible()
      }
}


Try updating your SceneDelegate and see if that resolves the issue.

Good catch! This is the answer.

Thanks. I can even see how I missed this, since I was searching the header for placeholder not title. Good change, though.

updating scenedelegate like you suggessted did the trick. thank you! my simulator is no longer a blank black screen.