食谱
- recipeID: 整数
- recipeName: 字符串
我有一个Recipe实体,其中有一个recipeID属性。 如何在Swift中获取最大的recipeID作为Int值?
我是Swift的新手,请帮助我。 提前致谢。
func fetchMaxID() {
let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "Recipe")
fetchRequest.fetchLimit = 1
let sortDescriptor = NSSortDescriptor(key: "recipeID", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
do {
let maxID = try [managedObjectContext?.executeFetchRequest(fetchRequest)].first
print(maxID)
} catch _ {
}
}