0

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)
}

2 Answers2

1

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.

Dylon
  • 1,730
  • 15
  • 14
  • This is what I feel like would be the correct answer, but I'm pretty sure he said he did that, and got some weird error. It sounds like something else strange is going on. – Pierce Dec 27 '19 at 00:34
  • When I copy and pasted the suggested code I got this time: 'Generic parameter 'C0' could not be inferred' at the start of the VStack –  Dec 27 '19 at 01:27
1

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)
    }
  }
}
Nibr
  • 187
  • 1
  • 9