
构建和执行查询的函数:
func queryForContactDate(context:NSManagedobjectContext) -> AnyObject?{ var ExpressionDescriptions = [AnyObject](); let ExpressionDescription = NSExpressionDescription() // name the column ExpressionDescription.name = "maxUpdated" // Use an Expression to specify what aggregate action we want to take and // on which column. In this case max on the update_at column ExpressionDescription.Expression = NSExpression(format: "@max.updated_at") // Specify the return type we expect ExpressionDescription.ExpressionResultType = .DateAttributeType // Append the description to our array ExpressionDescriptions.append(ExpressionDescription) // Build out our fetch request the usual way let request = NSFetchRequest(entityname: Contact.entityname()) // Specify we want dictionarIEs to be returned request.resultType = .DictionaryResultType // Hand off our Expression descriptions to the propertIEsToFetch fIEld. request.propertIEsToFetch = ExpressionDescriptions // Our result is going to be an array of dictionarIEs. var results:[[String:AnyObject]]? // Perform the fetch. This is using Swfit 2,so we need a do/try/catch do { results = try context.executeFetchRequest(request) as? [[String:AnyObject]] } catch _ { // If it fails,ensure the array is nil results = nil } return results![0];} 如果我在最后放置一个断点并打印出它产生的结果:
Printing description of results:▿ Optional([[:],["maxUpdated": 2015-12-30 20:05:31 +0000]]) ▿ Some : 2 elements - [0] : 0 elements ▿ [1] : 1 elements ▿ [0] : 2 elements - .0 : "maxUpdated" - .1 : 2015-12-30 20:05:31 +0000获取最大或最小项目的传统Core Data方法是使用提取限制1进行查询并对该键进行排序.与此Objective-C代码一样:
+ (NSFetchRequest *) requestForStatusWithMaxID { NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityname: kAMStatusEntity]; NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey: kAMTwID ascending:NO]; request.sortDescriptors = @[sd]; request.fetchlimit = 1; return request;} // +requestForStatusWithMaxID 修改upd_at属性的上述内容非常简单.
总结以上是内存溢出为你收集整理的Swift获取请求返回结果中的空项全部内容,希望文章能够帮你解决Swift获取请求返回结果中的空项所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)