【发布时间】:2013-12-12 05:58:20
【问题描述】:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"salesRepName == %@",salesName]; [请求集合谓词:谓词]; 这是我的收获。是否可以从表中获取名称并直接转换为大写而不使用字符串?
【问题讨论】:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"salesRepName == %@",salesName]; [请求集合谓词:谓词]; 这是我的收获。是否可以从表中获取名称并直接转换为大写而不使用字符串?
【问题讨论】:
不,这是不可能的。在核心数据获取请求中获取计算结果的唯一方法(据我所知)是使用NSExpressionDescription 和[NSExpression expressionForFunction:…],
并且不支持uppercaseString 函数。所以你必须转换值
获取它们后变为大写。
【讨论】:
NSExpression 确实支持uppercase:,如下所示:
NSExpression(forFunction: "uppercase:", arguments: [NSExpression(forKeyPath: "name")]
我已经成功地将它与NSBatchUpdateRequest 一起使用,如下所示,将所有名称转换为大写而不获取记录:
let batchUpdateRequest = NSBatchUpdateRequest(entityName: "Person")
batchUpdateRequest.predicate = NSPredicate(value: true)
// Convert name to uppercase
batchUpdateRequest.propertiesToUpdate = ["name": NSExpression(forFunction: "uppercase:", arguments: [NSExpression(forKeyPath: "name")])]
batchUpdateRequest.resultType = .UpdatedObjectIDsResultType
try! self.context.executeRequest(batchUpdateRequest)
【讨论】: