fatal error: Index out of range...

class CalculatorViewModel : NSObject, ObservableObject, Identifiable { 

    var id = UUID()
 

    @Published var output = "Disconnected"  
    @Published var connected = false  
    
    @Published var databasePath =  String()
    
    enum itemType : Int{ 
        case angle = 1 
        case degree = 2 
        case grip1 = 3
        case grip2  = 4
    }
    
    struct test_Array: Identifiable {
        var id = UUID()  
        var time: String 
        var swingNum : Int 
        var dataSeqInSwing: Int 
        var timeStampInSeq: Double 
        var itemType: Int 
        var value: Double 
    }
    @Published var testDBdata  = [test_Array] ()

   .....
      private var centralManager: CBCentralManager?

    func connectCalculator() {
        output = "Connecting..."
        centralQueue = DispatchQueue(label: "test.discovery") 
        centralManager = CBCentralManager(delegate: self, queue: centralQueue)
    }

  ....

     func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {

 .....
        if characteristic.uuid == outputCharUUID, var data = characteristic.value {

  .....
                   DispatchQueue.main.async() {

                    for index in tupleSets {
                        strItemSets = index.components(separatedBy: ",") 
                        .......

                        var itemN = 1
                        for indexStr in strItemSets {

                            self.golfDBdata[numDB].dataSeqInSwing = countItem.  <= Error 
                            self.golfDBdata[numDB].itemType = itemN
                            self.golfDBdata[numDB].value = Double(strItemSets[itemN]) ?? 0.0

Replies

What is golfDBdata? I don't see it declared in the code you showed. I assume it's an array. How many elements does it have?

What is numDB? What is its value?

The error message is telling you that the value of numDB is greater than or equal to the number of elements in golfDBdata.

Another potential problem you have is you are running the code asynchronously. You may be trying to access data before it's been fetched.

  • how can i edit my article ? there isn't edit button.

Add a Comment

    var id = UUID()
 

    @Published var output = "Disconnected"  
    @Published var connected = false  
    
    @Published var databasePath =  String()
    
    enum itemType : Int{ 
        case angle = 1 
        case degree = 2 
        case grip1 = 3
        case grip2  = 4
    }
    
    struct test_Array: Identifiable {
        var id = UUID()  
        var time: String 
        var swingNum : Int 
        var dataSeqInSwing: Int 
        var timeStampInSeq: Double 
        var itemType: Int 
        var value: Double 
    }
    @Published var golfDBdata  = [test_Array] ()

   .....
      private var centralManager: CBCentralManager?

    func connectCalculator() {
        output = "Connecting..."
        centralQueue = DispatchQueue(label: "test.discovery") 
        centralManager = CBCentralManager(delegate: self, queue: centralQueue)
    }

  ....

     func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {

 .....
        if characteristic.uuid == outputCharUUID, var data = characteristic.value {

  .....
                   DispatchQueue.main.async() {

                    for index in tupleSets {
                        strItemSets = index.components(separatedBy: ",") 
                        .......
                         .......
                        var countItem = 0 
                        var numDB = 1
                        var itemN = 1
                        for indexStr in strItemSets {

                            self.golfDBdata[numDB].dataSeqInSwing = countItem.  <= Error 
                            self.golfDBdata[numDB].itemType = itemN
                            self.golfDBdata[numDB].value = Double(strItemSets[itemN]) ?? 0.0

What is golfDBdata? I don't see it declared in the code you showed. I assume it's an array. How many elements does it have? => testDBdata is golfDBdata

What is numDB? What is its value? numDB is initialized as 1.

The error message is telling you that the value of numDB is greater than or equal to the number of elements in golfDBdata.

Another potential problem you have is you are running the code asynchronously. You may be trying to access data before it's been fetched. => this may be right guess. so, i think, "DispatchQueue.main.async " is related with this error. => self.golfDBdata isn't initialized yet, someone advised me that i need to initialize golfDBdata at class initializer or at property intializer.

=> some book says that it is not recommanded that using Array item in DispatchQueue.

error capture

capture of error is like below

i guess two reason.

  1. concurrency problem trouble is happens at closure of " Dispatch.main.async"

  2. i think when i try to save the data at structure ,

the struct golf_array maybe is not ready to save the data.

  1. so, i think the reason, how i can initialize the struct

  2. and do i need to use class ? rather than a struct?