将数据添加到闭包内部的字典中,字典为空

时间:2018-10-07 05:15:22

标签: ios dictionary swift4

我在关闭字典时遇到了问题。我创建了一个字典变量:

var dates = [String:String]()

然后我用以下代码填充字典:

// Function to Retrieve Dates from iCloud
@objc func dotDates() {

    let predicate = NSPredicate(value: true)

    let query = CKQuery(recordType: "Events", predicate: predicate)

    database.perform(query, inZoneWith: nil) {
        (records, error) in

        if error == nil {

            for record in records! {

                self.jobHold = record["jobName"]!
                self.dateHold = record["jobDateTime"]!

                // Date Conversion to CVDate
                let conversion = Convert()
                self.dateHold = conversion.convertStringToDate(date: self.dateHold)

                // Store Dates in a Dictionary
                self.dates[self.dateHold] = self.jobHold

            }

        } else {

            print(error?.localizedDescription as Any)
        }
    }

    print("**********", self.dates, "**********")
}

调试区域显示数据在其中:

********** ["8 October, 2018": "Anchor Lodge Rehab",
"7 October, 2018": "Anchor Lodge Office", "15 October,
2018": "Jarm", "12 October, 2018": "North Shore Endo",
"17 October, 2018": "Kidsmile Fresh"] **********

我的问题是,当我尝试访问代码中其他地方的字典时,字典为空。

// Display dots for date events
func dotMarker(shouldShowOnDayView dayView: DayView) -> Bool{

    print("**********", dates, "**********")

    // Look up date in dictionary
    if(dates[dayView.date.commonDescription] != nil){
        return true // date is in the array so draw a dot
    }
    return false
}

调试区域显示:

********** [:] **********
********** [:] **********
********** [:] **********

我不确定为什么它是空的?任何帮助将不胜感激!

0 个答案:

没有答案