ObservableObject protocol to @Observable macro

// Before

class A: ObservableObject {
    @Published var dataArray: [dataType]
}

class B: ObservableObject {
    @Published var dataArray: [dataType]

    init(a: A) {
        A.$dataArray
           .assign(to: &$dataArray)
    }
}

// After applying @Observable, I get an error "Cannot find 'A.$anArray' in scope" on the line indicated below.

@Observable
class A {
    var dataArray: [dataType]
}

@Observable
class B {
    var dataArray: [dataType]

    init(a: A) {
        A.$dataArray  ----------------------------- Error
           .assign(to: &$dataArray)
    }
}

Class A contains an array to store data fetched from an API. I'm trying to extract some contents from class A into class B. Making them focus on different functionality. All functions in Class B also require the downloaded data.

Answered by DTS Engineer in 757147022

Quoting SE-0395 Observation:

Because observable types generally use the implicitly generated initializers, the @Observable macro requires that all stored properties have a default value.

See here for links to the effort to change this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

Quoting SE-0395 Observation:

Because observable types generally use the implicitly generated initializers, the @Observable macro requires that all stored properties have a default value.

See here for links to the effort to change this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

ObservableObject protocol to @Observable macro
 
 
Q