Cannot assign values of type 'Foo' to type 'Foo.Type'

Hello guys,

I trying to request some data from an API, decoded it and save the values into a @State variable in my View. I'm new with Swift, so I know that my explanation is wrong. It would be better if I show you my code:

Request function
Code Block Swiftui
    // Request API
    func request<T: Decodable>(url: String, model: T.Type, completion: @escaping (T) -> Void) {
        // We take some model data T.Type
        guard let url = URL(string: url) else {
            print("Invalid URL")
            return
        }
        let request = URLRequest(url: url)
        URLSession.shared.dataTask(with: request) { data, response, error in
            if let data = data {
                do {
                    // Decode response with the model passed
                    let decodedResponse = try JSONDecoder().decode(model, from: data)
                    DispatchQueue.main.async {
                        print(decodedResponse)
                        completion(decodedResponse)
                    }
                    return
                } catch {
                    print(error)
                }
            }
            print("Fetch failed: \(error?.localizedDescription ?? "Unknown error")")
        }
        .resume()
    }

Model data
Code Block Swiftui
struct QuoteModel: Codable {
    var latestPrice: Float
    var changePercent: Double
}

JSON Response
Code Block JSON
{
"latestPrice": 100.01,
"changePercent": 0.02
}

View
Code Block Swiftui
struct Price: View {
    @State var quote = QuoteModel.self
    var body: some View {
Text("Hello world")
        .onAppear {
            request(url: endpoint, model: QuoteModel.self) { self.quote = $0 }
        }
    }
}

The problem is, when I try to save the results into de @State var I get this error Cannot assign value of type 'QuoteModel to type QuoteModel.Type

When the JSON response is an array of JSON, for example
Code Block JSON
[
{
"latestPrice": 100.01,
"changePercent": 0.02
},
{
"latestPrice": 50.05,
"changePercent": 0.003
}
]

I do not get this error and it works perfectly. What am I missing?

Thank you.
Dennis.

Seems you need to re-learn the Swift language till you understand why this line is so odd:
Code Block
    @State var quote = QuoteModel.self


You omitted type annotation for quote here, so Swift infers it as QuoteModel.Type.
The line is equivalent to:
Code Block
    @State var quote: QuoteModel.Type = QuoteModel.self


So, you declare quote as a property to hold QuoteModel.Type, not QuoteModel.
In Swift, QuoteModel.self represents a type object, not an instance of the type.


You should better declare properties with explicit type annotations:
Code Block
    @State var quote: QuoteModel = 「an Instance of `QuoteModel`」

Or , if you cannot think of a right initial value, you may want to use an Optional:
Code Block
    @State var quote: QuoteModel? = nil

(You may need to modify other parts using quote in this case.)

Cannot assign values of type 'Foo' to type 'Foo.Type'
 
 
Q