Cannot convert value of type 'ChooseModel.Angle' to expected argument type 'Angle'

Hi, guys i got errors from my View like below : Is anyone who can help me to fix it?

My model like:


    var id = UUID()

    var decision: String

    var choices: [String]

    var startAngle: Angle

    var endAngle: Angle

     

    @CodableColor var color: UIColor

    struct Angle: Codable, Equatable {

        var degrees: Double

    }

}

My model view like:


import SwiftUI



class ChooseModelView: ObservableObject {

    var midRadians: Double {

        return Double.pi / 2.0 - (model.startAngle.degrees + model.endAngle.degrees) * .pi / 180 / 2.0

    }

    @Published var model = ChooseModel(decision: "What to eat tonight?", choices: ["Sushi", "Ramen", "Korean BBQ", "Chinese"], startAngle: ChooseModel.Angle(degrees: 0.0), endAngle: ChooseModel.Angle(degrees: 90.0), color: UIColor.green)

My view like :


                    let width: CGFloat = min(geometry.size.width, geometry.size.height)

                    let height = width

                    let center = CGPoint(x: width * 0.5, y: height * 0.5)

                    path.move(to: center)

                    path.addArc(

                        center: center,

                        radius: width * 0.5,

                        startAngle: Angle(degrees: -90.0) + chooseMV.model.startAngle,

                        endAngle: Angle(degrees: -90.0) + chooseMV.model.endAngle,

                        clockwise: false)

                    path.closeSubpath()

                }

Accepted Answer

Did you try to make Angle Codable:

extension Angle: Codable {
  public init(from decoder: Decoder) throws {
    self.init()
  }

  public func encode(to encoder: Encoder) throws {
  }
}
  • Thank you, I'm working on it this way right now.

  • @tankman, please share your solution (and mark it SOLVED) when you are done well.

Add a Comment

Answers

My model like:

Some lines missing. Is it a struct or a class? What is the type name?

Cannot convert value of type 'ChooseModel.Angle' to expected argument type 'Angle'

If you want to use SwiftUI.Angle, you should not define your own Angle. They have the same name but two different things.

To make a struct Codable which contains SwiftUI.Angle, you need to make SwiftUI.Angle Codable, or add a conversion method to and from your own Angle and SwiftUI.Angle.


By the way, you should better respond to all answers and comments you get, which helps you to get better answers sooner.

  • Thank you, the line missing is struct, I tried to make SwiftUI.Angle codable because I want to store it to UserDefaults, but i don't know how to make SwiftuiAngle codable, So i created on in model struct then i didn't know how to deal with it :(

  • Thanks for you reply. i created on in model struct then i didn't know how to deal with it > That means defining your own struct with the same name is the right way to go.

Add a Comment

That's a similar post to your previous one. Could you answer to this previous post first ? That will help and is more correct than just ignoring replies you got. Thanks.

struct ChooseModel: Codable {
     var id = UUID()
    var decision: String
    var choices: [String]
    var startAngle: ChooseModel.Angle // Qualify Your type Angle by the `ChooseModel` namespace
    var endAngle: ChooseModel.Angle  // Qualify Your type Angle by the `ChooseModel` namespace
    @CodableColor var color: UIColor
    struct Angle: Codable, Equatable {
        var degrees: Double
    }
}
// Do this `-90 + chooseMV.model.[startAngle|endAngle].degrees` instead of `ChooseModel.Angle(degrees: -90.0).degrees` since the instance of `ChooseModel.Angle` is not used beyond these two lines.
path.addArc(
                        center: center,
                        radius: width * 0.5,
                        startAngle: -90.0 + chooseMV.model.startAngle.degrees, // startAngle are CGFloat types and not Angle types so you will to use the `degrees` properties of the Angle objects for the computation to CGFloat
                        endAngle: -90.0 + chooseMV.model.endAngle.degrees,  // endAngle are CGFloat types and not Angle types so you will to use the `degrees` properties of the Angle objects for the computation to CGFloat
                        clockwise: false)
  • Thank you I have tried it but the same places throw me another error: Cannot convert value of type 'Double' to expected argument type 'Angle'

  • struct Angle: Codable, Equatable {         var degrees: CGFloat     }

Add a Comment

Why are you writing your own struct Angle, when SwiftUI already provides an Angle, which you could easily extend, to make it Codable?

  • I don't how to how extend or codable Angle , So i tried to declare a new struct Angle to make it codable :(

Add a Comment

Did you try to make Angle Codable:

extension Angle: Codable {
  public init(from decoder: Decoder) throws {
    self.init()
  }

  public func encode(to encoder: Encoder) throws {
  }
}
  • Thank you, I'm working on it this way right now.

  • @tankman, please share your solution (and mark it SOLVED) when you are done well.

Add a Comment