onFormat string For NSPredicate

onFormat string For NSPredicate

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

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

我的描述有误:

can not parse "self IN %@" format string.

解析代码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 服务完成时获取主队列 posts:

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{[=12=].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 %@