> recipeID:Int
> recipename:String
我有一个带有属性recipeID的实体配方.
如何在Swift中将max(recipeID)作为Int值?
我很快,请帮助我.
提前致谢.
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 _ { }}Apple推荐并且速度最快的方式是使用NSExpressions. moc是NSManagedobjectContext.
private func getLastContactSyncTimestamp() -> Int64? { let request: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest() request.entity = NSEntityDescription.entity(forEntityname: "Contact",in: self.moc) request.resultType = NSFetchRequestResultType.dictionaryResultType let keypathExpression = NSExpression(forKeyPath: "timestamp") let maxExpression = NSExpression(forFunction: "max:",arguments: [keypathExpression]) let key = "maxTimestamp" let ExpressionDescription = NSExpressionDescription() ExpressionDescription.name = key ExpressionDescription.Expression = maxExpression ExpressionDescription.ExpressionResultType = .integer64AttributeType request.propertIEsToFetch = [ExpressionDescription] var maxTimestamp: Int64? = nil do { if let result = try self.moc.fetch(request) as? [[String: Int64]],let dict = result.first { maxTimestamp = dict[key] } } catch { assertionFailure("Failed to fetch max timestamp with error = \(error)") return nil } return maxTimestamp}总结
以上是内存溢出为你收集整理的核心数据 – 如何从实体属性获取最大值(Swift)全部内容,希望文章能够帮你解决核心数据 – 如何从实体属性获取最大值(Swift)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)