Cannot use instance member 'golfData' within property initializer; property initializers run before 'self' is available ?

******* TestData.swift ************************

struct Measures: Identifiable {
   let id = UUID()
   var dataSeq: Int
   var value: Double
}

struct Items: Identifiable {
   let id = UUID()
   var name: String
   var measures: [Measures]
}

struct chartItemInfo: Identifiable {
    var id = UUID()
    var testItem: String
    var value: Double
}

 var angleItem :  [chartItemInfo]?
 var degreeItem : [chartItemInfo]?
 var grip1Item : [chartItemInfo]?
 var grip2Item : [chartItemInfo]?

class TestData: ObservableObject {
    @Published var angleItem :  [chartItemInfo]?
    @Published var degreeItem : [chartItemInfo]?
    @Published var grip1Item : [chartItemInfo]?
    @Published var grip2Item : [chartItemInfo]?
}

******** CalculatorViewModel.swift ******************************************

class CalculatorViewModel : NSObject, ObservableObject, Identifiable { 
....

    typealias test_Array = (time: String, swingNum: Int, dataSeqInSwing: Int, timeStampInSeq: Int, angle: Double, degree: Double, grip1: Double, grip2: Double)    
....

@Published var testDBdata = [test_Array]()
    @Published var chartDBdata = [chart_Array]() // 
    @StateObject var testData : TestData   
   var Testitems = [ (channel: "angle", data: testData.angleItem), (channel: "degree", data: testData.degreeItem), (channel: "grip1", data: testData.grip1Item), (channel: "grip2", data: testData.grip2Item)].    => Cannot use instance member 'testData' within property initializer; property initializers run before 'self' is available ?

  • purpose of this project: read FMDB data and then make Array with DB data.

and then i use these Array for displaying multi plot in Chart.

Let’s boil this down to something simpler. Consider this program:

typealias TestData = String

class CalculatorViewModel {
    init() {
        print("instance class initialiser")
        self.testData = "Hello Cruel World!"
    }
    var testData: TestData
    // var testItems: [String] = [testData]
    var testItems2: [String] = {
        print("instance property initialiser")
        return ["Goodbye Cruel World!"]
    }()
}

_ = CalculatorViewModel()

If you uncomment testItems, you get exactly the error you’re reported. With it commented you can actually build and run the program, and it prints:

instance property initialiser
instance class initialiser

This explains what that error is getting at. The initialiser for the testItems2 property runs before the overall class initialiser, so it can’t reference testData because it’s not been set up properly.

There are a variety of ways that you can get around this but the easiest is to do all your initialiser in your class initialiser.

Share and Enjoy

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

Cannot use instance member 'golfData' within property initializer; property initializers run before 'self' is available ?
 
 
Q