1

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an error occurs. Just inside this function.

func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
    var dateForDot = dateFormatter.string(from: date)
        self.getScheduleDot(date: dateForDot)
    let dotArr = self.userDefaults.array(forKey: "arrForDotDisplay") as! [String]
        for dateString in dotArr {
            if dateForDot == dateString {
                return 1
            }
        }
    return 0
}

The details of the "getScheduleDot" method are as follows

func getScheduleDot(date: String) {
    self.db
        .collection("users")
        .document(fireAuthUID)
        .addSnapshotListener { (snapshot, error) in
        guard let document = snapshot else {
            print("erorr2 \(String(describing: error))")
            return
        }
        guard let data = document.data() else { return }
        self.teamIDFromFirebase = data["teamID"] as? String ?? ""

        self.db
            .collection("teams")
            .document(self.teamIDFromFirebase)
            .collection("schedule")
            .whereField("date_start", isEqualTo: date)
            .getDocuments() { (querySnapshot, err) in
            if err != nil {
                print("scheduleを取得できませんでした")
                return
            } else {
                var i = 0
                for document in querySnapshot!.documents {
                    let dataFromFirebase: [String : Any] = document.data()
                    let startDateFromFirebase = dataFromFirebase["date_start"] ?? ""
                    let endDateFromFirebase = dataFromFirebase["date_end"] ?? ""
                    self.dotDisplayArr.append(startDateFromFirebase as! String)
                    self.dotDisplayArr.append(endDateFromFirebase as! String)
                    i += 1
                }

                self.dotDisplayArr as NSArray
                self.userDefaults.removeObject(forKey: "arrForDotDisplay")
                self.userDefaults.set(self.dotDisplayArr, forKey: "arrForDotDisplay")
                self.userDefaults.synchronize()
            }
        }
    }
}

Although it was working normally until then, you will get such bugs after updating xcode. I would like to tell you the reason.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • @Paulw11 I don't think that generic dupe target applies here. The actual issue here is that OP is using async Firebase functions and setting `UserDefaults` from the completion handler, but trying to access the set values from outside the completion handler – Dávid Pásztor Nov 05 '18 at 11:02

1 Answers1

0
let dotArr = self.userDefaults.array(forKey: "arrForDotDisplay") as! [String]

Check this I think there is nothing in userDefaults. wrap this logic into guard or work with as? [String] and check for nil manually

Marco Pace
  • 3,820
  • 19
  • 38
Arkhyp Koshel
  • 175
  • 11
  • That's not the cause of the issue, that's only a symptom of the actual issue. The cause of the issue is that the firebase functions are asynchronous, so OP is trying to access `UserDefaults` before the value would actually be set. – Dávid Pásztor Nov 05 '18 at 10:55