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

                }

Answered by Claude31 in 697150022

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

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.

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)

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

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 {
  }
}
Cannot convert value of type 'ChooseModel.Angle' to expected argument type 'Angle'
 
 
Q