<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/TextField/init(_:value:format:prompt:)-6ug7k",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI9TextFieldVA2A0C0VRszrlE_5value6format6promptACyAEGqd___AA7BindingVy11FormatInputQyd_0_SgGqd_0_AESgtcSyRd__10Foundation09ParseableI5StyleRd_0_SS0I6OutputRtd_0_r0_lufc"
  },
  "title" : "init(_:value:format:prompt:)"
}
-->

# init(_:value:format:prompt:)

Creates a text field that applies a format style to a bound optional
value, with a label generated from a title string.

```
nonisolated init<S, F>(_ title: S, value: Binding<F.FormatInput?>, format: F, prompt: Text? = nil) where S : StringProtocol, F : ParseableFormatStyle, F.FormatOutput == String
```

## Parameters

`title`

The title of the text field, describing its purpose.

`value`

The underlying value to edit.

`format`

A format style of type `F` to use when converting between
the string the user edits and the underlying value of type
`F.FormatInput`. If `format` can’t perform the conversion, the text
field sets `binding.value` to `nil`.

`prompt`

A `Text` which provides users with guidance on what to type
into the text field.

## Discussion

Use this initializer to create a text field that binds to a bound optional
value, using a
<doc://com.apple.documentation/documentation/Foundation/ParseableFormatStyle>
to convert to and from this type. Changes to the bound value update
the string displayed by the text field. Editing the text field
updates the bound value, as long as the format style can parse the
text. If the format style can’t parse the input, the text field
sets the bound value to `nil`.

Use the `View/onSubmit(of:_:)` modifier to invoke an action
whenever the user submits this text field.

The following example uses an optional
<doc://com.apple.documentation/documentation/Swift/Double>
as the bound currency value, and a
<doc://com.apple.documentation/documentation/Foundation/FloatingPointFormatStyle/Currency>
instance to convert to and from a representation as U.S. dollars. As
the user types, a `View.onChange(of:_:)` modifier logs the new value to
the console. If the user enters an invalid currency value, like letters
or emoji, the console output is `Optional(nil)`.

```
@State private var label = "Currency (USD)"
@State private var myMoney: Double? = 300.0
var body: some View {
    TextField(
        label,
        value: $myMoney,
        format: .currency(code: "USD")
    )
    .onChange(of: myMoney) { newValue in
        print ("myMoney: \(newValue)")
    }
}
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)