在我的应用程序中,我试图显示我通过prepareForSegue
的特定药物的日期。比我想准备一个包含日期只与医学名称相关的日期的数组。如何准备这样的抓取?
我的模特:
extension Dates {
@NSManaged var date: NSDate?
@NSManaged var takes: Medicine?
}
extension Medicine {
@NSManaged var amount: String?
@NSManaged var endDate: String?
@NSManaged var name: String?
@NSManaged var time: String?
@NSManaged var notificationSet: NSNumber?
@NSManaged var taken: NSOrderedSet?
}
在viewDidLoad中,我尝试像这样准备这个数组:
var context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let fetchRequestDate = NSFetchRequest(entityName: "Dates")
let fetchRequestMedicine = NSFetchRequest(entityName: "Medicine")
var medicine : Medicine?
var dates = [NSDate]()
override func viewDidLoad() {
super.viewDidLoad()
do {
fetchRequestDate.predicate = NSPredicate(format: "takes.name == %@ ", medicine!.name!)
let resultsDates = try context.executeFetchRequest(fetchRequestDate)
for date in resultsDates {
print(date)
}
} catch let error as NSError {
print(error.userInfo)
}
稍后在此功能中使用它:
func calendar(calendar: CKCalendarView!, configureDateItem dateItem: CKDateItem!, forDate date: NSDate!) {
for dateTaken in dates {
if calendar.date(date, isSameDayAsDate: dateTaken) {
dateItem.backgroundColor = UIColor.redColor()
}
}
}