本文介绍了onFormat string适用于NSPredicate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用id键对数据进行排序,如何理解 NSPredicate 格式的格式字符串是什么?我有一个100号的帖子。
我的代码:

I want to sort data with "id" key, how can I understand what is format string for NSPredicate format ? I have an 100 number post.My code :

 let objectIDs = posts.map{$0.id}
    let predicate = NSPredicate(format: "self IN %@", objectIDs)
    let sortByUserId = NSSortDescriptor(key: "id", ascending: true)

我对此描述有错误:

解析JSON数据的代码:

func postsFromJSONData(data : NSData, inContext context: NSManagedObjectContext) -> PostResult {
            do{
            let jsonObject : [[String:AnyObject]]
            = try NSJSONSerialization.JSONObjectWithData(data, options: []) as! [[String:AnyObject]]

            for postJSON in jsonObject {
                if let post = postFromJsonObject(postJSON, incontext : context) {

                        finalPost.append(post)
                    }
                }

            return .Sucsess(finalPost)
            }
            catch let error {
                return .Failure(error)
            }

        }

获取Web服务时的主要​​队列帖子完成:

fetch the main queue posts when the web service finishes:

func fetchRecentPost(completion completion: (PostResult) -> Void){

    let request = NSURLRequest(URL: recentPostURL(["":""]))

let task = session.dataTaskWithRequest(request, completionHandler: {
    (data, response, error)  in

    var result = self.processRecentPostRequest(data: data, error: error)

    if case let .Sucsess(posts) = result {

        let mainQueueContext = self.coreDataStack.mainQueueContext
        mainQueueContext.performBlockAndWait(){

            try! mainQueueContext.obtainPermanentIDsForObjects(posts)
        }

        let objectIDs = posts.map{$0.id}
        let predicate = NSPredicate(format: "self IN %@", objectIDs)
        let sortByUserId = NSSortDescriptor(key: "id", ascending: true)

        do{
            try self.coreDataStack.saveChanges()
            let mainQueuePosts = try self.fetchMainQueuePosts(predicate: predicate, sortDiscriptors: [sortByUserId])
            result = .Sucsess(mainQueuePosts)
        }
        catch let error {

            result = .Failure(error)
        }
    }

        completion(result)

        })


推荐答案

你的谓词应该是这样的

let predicate = NSPredicate(format: "id IN %@", objectIDs)
let sortByUserId = NSSortDescriptor(key: "id", ascending: true)

注意:它是 self.id IN%@ id IN%@

这篇关于onFormat string适用于NSPredicate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 05:37