Reputation:
My Xcode keeps giving me a Double is not convertible to CGFloat error. Thing is the error keeps moving whenever I change the code. At first it was on this line:
Text("left") .frame(width: 100.0, height: 100.0, alignment: .center)
where I set the height to 100.0 now it is at the bottom:TextField("example", text: "PLACEHOLDER") .frame(width: 300.0, height: 300.0, alignment: .center)
at the height.
On the apple dev site it says for swiftUI as an example:
.frame(width: 200, height: 200, alignment: .topLeading)
I tried removing the decimals and I get an Int is not convertible to CGFloat.
At one point I tried to put CGFloat() around each and I got a 'CGFloat is not convertible to CGFloat?' error
Any help is appreciated thank you
import Cocoa
import SwiftUI
HSplitView{
VStack{
Text("left")
.frame(width: 100.0, height: 100.0, alignment: .center)
Text("left bottom")
.frame(width: 100.0, height: 100.0, alignment: .center)
Text("leftbottombottom")
.frame(width: 100.0, height: 100.0, alignment: .center)
}
TextField("example", text: "PLACEHOLDER")
.frame(width: 300.0, height: 300.0, alignment: .center)
}
Upvotes: 1
Views: 1548
Reputation: 187
SwiftUI sometimes reports wrong errors, as is the case here. Your problem is that you have a type error in TextField("example", text: "PLACEHOLDER")
. The second argument should be a binding, not a string. This works:
import Cocoa
import SwiftUI
struct MyView: View {
@State var text = "PLACEHOLDER"
var body: some View {
HSplitView{
VStack{
Text("left")
.frame(width: 100.0, height: 100.0, alignment: .center)
Text("left bottom")
.frame(width: 100.0, height: 100.0, alignment: .center)
Text("leftbottombottom")
.frame(width: 100.0, height: 100.0, alignment: .center)
}
TextField("example", text: $text)
.frame(width: 300.0, height: 300.0, alignment: .center)
}
}
}
Upvotes: 1
Reputation: 1750
Try casting Double
to CGFloat
:
import Cocoa
import SwiftUI
HSplitView{
VStack{
Text("left")
.frame(width: CGFloat(100.0), height: CGFloat(100.0), alignment: .center)
Text("left bottom")
.frame(width: CGFloat(100.0), height: CGFloat(100.0), alignment: .center)
Text("leftbottombottom")
.frame(width: CGFloat(100.0), height: CGFloat(100.0), alignment: .center)
}
TextField("example", text: "PLACEHOLDER")
.frame(width: CGFloat(300.0), height: CGFloat(300.0), alignment: .center)
}
Depending on the type and language, you may have to explicitly cast one type to another rather than expecting the language to implicitly coerce it for you.
Upvotes: 1