Cannot convert value of type 'Binding String? ' to expected argument type 'Binding String '
Because all core data fields are optional, you have to wrap it into Binding
. And as you're using core data and may face a lot of optionals, you can create an extension:
extension Binding {
func toUnwrapped<T>(defaultValue: T) -> Binding<T> where Value == Optional<T> {
Binding<T>(get: { self.wrappedValue ?? defaultValue }, set: { self.wrappedValue = $0 })
}
}
Use it like this:
TextField("title", text: $text.name.toUnwrapped(defaultValue: ""))
Cannot convert value of type 'Binding Data Struct? ' to expected argument type 'Binding Data Struct '
In this case I think it should be enough just to unwrap binding, like
if selectedShirt != nil && showDetailShirt{
ShirtDetailView(shirtData: Binding($selectedShirt)!, showDetailShirt: $showDetailShirt,animation: _animation)
}
How to fix Cannot convert value of type 'Binding Device ' to expected argument type 'Device'
The problem here is that on this line:
ForEach($deviceArrays.devices, id: \.id) { deviceArray in
You should use $deviceArray
instead, because you are inputting a Binding
into the ForEach
. Then you can access deviceArray
for the regular value.
Other solution
Use its wrappedValue
property.
This converts a type of Binding<T>
to T
. In your case, Binding<Device>
to Device
.
ForEach($deviceArrays.devices, id: \.id) { deviceArray in
HStack {
ForEach(deviceArray.row, id: \.id) { device in
AnotherView(device: $currentDevice, size: $size)
.onAppear {
setCurrentDevice(to: device.wrappedValue)
}
}
}
}
Swift error, Cannot convert value of type 'Binding String? ' to expected argument type 'Binding String '
The error is originated from this line
Text(message ?? "Message")
because compiler tries to infer type and this line shows that type should be optional, but editor expects not optional, so you got conflict and error. Your message
state is not optional so you just need to move default value directly in intializer.
Here is corrected variant:
struct DemoView: View {
@State var message: String = "Message"
var body: some View {
VStack {
ZStack(alignment: .topLeading) {
TextEditor(text: $message)
.frame(minHeight: 30, alignment: .leading)
.cornerRadius(6.0)
.multilineTextAlignment(.leading)
Text(message)
.padding(.leading, 4)
.opacity(message == "" ? 1 : 0)
}
.font(.body)
}
}
}
Cannot convert value of type 'String' to expected argument type 'Binding String '
It is better to do by separating into different view, like
var body: some View {
Form {
Section(header: Text("")) {
if user.currentAb != nil {
SomeNameView(vm: user.currentAb!)
} else {
Text("any holder view here")
}
}
}
}
and separated view
struct SomeNameView: View {
@ObservedObject var vm: AB
var body: some View {
TextField("new name", text: $vm.name)
.textFieldStyle(RoundedBorderTextFieldStyle())
}
}
Cannot convert value of type 'Binding Double ' to expected argument type 'Binding Double? '
Here is a solution that allows to disambiguate w/ and w/o value binding.
Tested with Xcode 11.4 / iOS 13.4
struct ChildView: View {
@Binding var prop: Double?
init() {
_prop = .constant(nil)
}
init(prop: Binding<Double>) {
_prop = Binding(prop)
}
var body: some View {
Group {
if prop != nil {
Text("Child View: \(prop!)")
} else {
Text("Child View")
}
}
}
}
Cannot convert value of type 'Binding _ ' to expected argument type 'Binding Card '
Changing the Binding to ObservedObject compiles and seems to work properly, although I feel like I'm violating single source of truth policy by creating a new ObservedObject.
struct NavView: View {
@ObservedObject var item : Card
...
}
struct ContentView: View {
private var items: FetchedResults<Card>
var body: some View {
List {
ForEach(items) { item in
NavigationLink {
NavView(item: item)
}
}
}
}
}
cannot convert value of type 'String' to 'Binding String '
Here is possible approach - to use constant binding for Preview
struct SignInButtons_Previews: PreviewProvider {
static var previews: some View {
SignInButtons(backgroundColor: Color.blue,
signInUrl: .constant("http://www.apple.com"),
showModal: .constant(false))
}
}
Related Topics
How to Add Documentation to Enum Associated Values in Swift
Recursion Over a Swift Sliceable
Encoding Swift String as Escaped Unicode
How to Combine Two Nsdictionary in Swift
Characters Is Unavailable' Please Use String Directly
Creating a Subclass of Skshapenode
Swift Firebase Storage How to Retrieve Image with Unknow Name(Nsuuid)
For-In Loops Multiple Conditions
Round Currency Closest to Five
How to Delete Item from Collection View
Change Width of a Uibarbuttonitem in a Uinavigationbar in Swift
Passing Data from Simple Nsview to Swiftui View
Count the Number of Lines in a Swift String
How to Reduce the Opacity of the Shadows in Realitykit
Make Touch-Area for Sklabelnode Bigger for Small Characters