Why am I getting an Index out of range error on Double value

Viewed 46

I am working on a SwiftUI app and I have a block of code that works perfectly when navigating from View A (MenuView) to View B (CartView). However, when navigating from View C to View B, I get a Thread 1: Fatal error: Index out of range.

I understand 100% how Index out of range works, my issue is that I am not working with a list but with a Double.

Also, I have checked in the debugger that there is actually a value when stepping through this code so the issue cannot be that the object or value is nil.

I do not understand how I am getting an Index out of range error on a Double value?

The specific value failing is basePrice.

CartItemView.swift

@Binding var cartItem: CartItem
@State var quantity = 1
@Binding var deleteIndex: Int
var cartItemIndex: Int
    
@State var total = 0.00
    
func updateTotal() {
    total = 0.00
    var item = cartItem
    var itemSubTotal = cartItem.basePrice
        
    for choice in cartItem.choices {
        itemSubTotal += choice.price
    }
        
    itemSubTotal = itemSubTotal * Double(cartItem.quantity)
        
    total += itemSubTotal
}

Item failing

▿ CartItem
  ▿ id : F9E1F7AA-8FA0-47ED-8F90-0DF865C4AD4B
    - uuid : "F9E1F7AA-8FA0-47ED-8F90-0DF865C4AD4B"
  - name : "Bacon & Avo Pizza"
  - description : ""
  - basePrice : 80.0
  - quantity : 1
  - choices : 0 elements
0 Answers
Related