【问题标题】:Fetching names from core data and converting it to upper case从核心数据中获取名称并将其转换为大写
【发布时间】:2013-12-12 05:58:20
【问题描述】:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"salesRepName == %@",salesName]; [请求集合谓词:谓词]; 这是我的收获。是否可以从表中获取名称并直接转换为大写而不使用字符串?

【问题讨论】:

    标签: core-data xcode4.5


    【解决方案1】:

    不,这是不可能的。在核心数据获取请求中获取计算结果的唯一方法(据我所知)是使用NSExpressionDescription[NSExpression expressionForFunction:…], 并且不支持uppercaseString 函数。所以你必须转换值 获取它们后变为大写。

    【讨论】:

      【解决方案2】:

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-16
        • 1970-01-01
        • 1970-01-01
        • 2013-05-16
        相关资源
        最近更新 更多