Learn Courses My Dashboard

Cannot assign to property: 'homeData' is a get-only property

Has anyone any idea why this error appears? I mean all the possibility’s from where it could come and why, and how I can solve it swiftUi.

There is a class with lots of functions, who needs if it is called, a parameter named query.

@StateObject homeData:HomeData

var query:String
init(query:String) {
self.query = query
self.homeData = HomeModel(query: query) --Here I am getting the error

self.homeData refers to the StateObject property wrapper’s wrappedValue, which the documentation shows us is get only:

public var wrappedValue: ObjectType { get }

You need to initialize the property wrapper itself, like so:

    @StateObject var homeData: HomeModel
    var query: String
    init(query: String) {
        self.query = query
        self._homeData = StateObject(wrappedValue: HomeModel(query: query))

(NOTE: I assume that homeData should actually be of type HomeModel, since that’s how you’re trying to initialize it.)

1 Like

Yes it is, thank you!