Learn Courses My Dashboard

Unwrapping Optional Double Array Firebase SwiftUI

View using player.broncoTimes (a sports fitness test with times in seconds) from firestore into a SwiftUICharts LineView. Link and chart works perfectly EXCEPT broncoTimes is an optional array of DOUBLE that my players can enter. This is where i have problem as for players without a {broncoTime] data, the chart crashes due to NIL. How to check for NIL and maybe replace with default array like [0,0,0,0]?

BroncoTest View below and Firebase func FetchUser both below - anything else needed?

Please help thx.

struct BroncoTest: View {
var player: Player

            var body: some View {

                    LineView(data: (player.broncoTimes! ), title: "Bronco Test Times", legend: "20202").padding()

View Model (// with several attempts!! ):

func fetchUser() {

    guard let uid = userSession2?.uid else { return }
    Firestore.firestore().collection("players").document(uid).getDocument { document, err in
        if err != nil {
            print("DEBUG: error fetching user: \(String(describing: err?.localizedDescription))")
        if let document = document {

// print(“Cached document data: (document)”)
guard let dictionary = document.data() else {return}

            guard let username = dictionary["username"] as? String else {return}
            guard let fullname = dictionary["fullname"] as? String else {return}
            guard let email = dictionary["email"] as? String else {return}
            guard let profileImageUrl = dictionary["profileImageUrl"] as? String else {return}
            guard let uid = dictionary["uid"] as? String else {return}

// guard let bio = dictionary[“bio”] as? String? else {return}
guard let fiveKmRun = dictionary[“fiveKmRun”] as? [Double]? else {return}
// let fiveKmRun = dictionary[“fiveKmRun”] as? [Double]? ?? [0, 0, 0]
// guard (dictionary[“fiveKmRun”] as? [Double]?) != nil else {return}
guard let glycoTimes = dictionary[“glycoTimes”] as? [Double]? else {return}
// let glycoTimes = dictionary[“glycoTimes”] as? [Double]? ??
// guard (dictionary[“glycoTimes”] as? [Double]?) != nil else {return}
guard let broncoTimes = dictionary[“broncoTimes”] as? [Double]? else {return}
// let broncoTimes = dictionary[“broncoTimes”] as? [Double]? ??
// guard (dictionary[“broncoTimes”] as? [Double]?) != nil else {return}

            let user = User(username: username, email: email, profileImageUrl: profileImageUrl, fullname: fullname, id: uid)
            self.currentUser = user

// print("self.currentUser: ", self.currentUser)
// print("isCurrentUser: ", self.currentUser?.isCurrentUser)
} else {
print(“Document does not exist in cache”)