获取请求和 NSpredicate 的错误处理

Error handling on fetch request and NSpredicate

我正在使用 CoreData 提取过去一周的数据。在存储某些数据的几天内一切正常。但是,当没有存储数据时,程序会崩溃。我该如何处理这个错误?

    for i in 0...6 { // Get every day in the past week
        let appDel = (UIApplication.sharedApplication().delegate as! AppDelegate)
        let context: NSManagedObjectContext = appDel.managedObjectContext
        let request = NSFetchRequest(entityName: "Pulser")
        request.returnsObjectsAsFaults = false

        let calendar = NSCalendar.currentCalendar()
        let now = NSDate()
        let beginningOfToday = calendar.startOfDayForDate(now)
        let previousDay = calendar.startOfDayForDate(calendar.dateByAddingUnit(.Day, value: -i, toDate: beginningOfToday, options: [])!)
        let previousPreiviousDay = calendar.startOfDayForDate(calendar.dateByAddingUnit(.Day, value: -i - 1 , toDate: beginningOfToday, options: [])!)

         request.predicate = NSPredicate(format:"(date >= %@) AND (date < %@)", previousPreiviousDay, previousDay)

        do {
            let results = try context.executeFetchRequest(request)  as! [Pulser]
            pulseArray.append(meanValue(results)) // if more than one value stored for each day, take the mean value of it

        }
        catch let error as NSError {
            print(error)
        }
    }

它现在就像一个魅力,谢谢。

 if results.count > 0 {
            pulseArray.append(meanValue(results)) 

  }