首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >swift - NSFetchRequest Order By语法

swift - NSFetchRequest Order By语法
EN

Stack Overflow用户
提问于 2016-02-08 07:32:09
回答 1查看 250关注 0票数 0

怎么了伙计们。

我目前正在这样做:

代码语言:javascript
复制
var results = NSArray()
        let appDel = UIApplication.sharedApplication().delegate as! AppDelegate
        let context = appDel.managedObjectContext

        let fetch = NSFetchRequest(entityName: "Work")



        let entity = NSEntityDescription.entityForName("Work", inManagedObjectContext: context)!
        let monthDesc = entity.attributesByName["shortMonth"] as NSAttributeDescription!
        let keyPathExpression  = NSExpression(forKeyPath: "date")
        // Does not really matter
        let countExpression = NSExpression(forFunction: "count:", arguments: [keyPathExpression])
        let expressionDescription = NSExpressionDescription()
        expressionDescription.name = "count"
        expressionDescription.expression = countExpression
        expressionDescription.expressionResultType = .Integer32AttributeType


        fetch.propertiesToFetch = [monthDesc, expressionDescription]
        fetch.propertiesToGroupBy = [monthDesc]
        fetch.resultType = .DictionaryResultType
        //var error: NSError? = nil




        fetch.returnsObjectsAsFaults = false;


        do {
            results = try context.executeFetchRequest(fetch)

        } catch {
            print("Unable to fetch results")
            abort()
        }


        let months = results.flatMap{$0["shortMonth"] as? String}
        let countt = results.flatMap{$0["count"] as? Double}

        print("Months \(months)")
        print("Countt \(countt)")

这将返回两个数组,其中包含月份名称和该月份名称的记录数。

结果:

代码语言:javascript
复制
Months ["Apr", "Feb", "Mar", "May"]
Countt [4.0, 10.0, 12.0, 5.0]

在这个实体中,我还有一个用于monthNumber的属性。

我怎样才能打印出这些按月份编号排序的数组呢?

它现在看起来是按字母顺序排列的。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2016-02-08 08:05:45

这将完成所要求的操作:

代码语言:javascript
复制
var results = NSArray()
        let appDel = UIApplication.sharedApplication().delegate as! AppDelegate
        let context = appDel.managedObjectContext

        let fetch = NSFetchRequest(entityName: "Work")



        let entity = NSEntityDescription.entityForName("Work", inManagedObjectContext: context)!
        let monthDesc = entity.attributesByName["shortMonth"] as NSAttributeDescription!
        let monthNumber = entity.attributesByName["month"] as NSAttributeDescription!
        let keyPathExpression  = NSExpression(forKeyPath: "date")
        let countExpression = NSExpression(forFunction: "count:", arguments: [keyPathExpression])
        let expressionDescription = NSExpressionDescription()
        expressionDescription.name = "count"
        expressionDescription.expression = countExpression
        expressionDescription.expressionResultType = .Integer32AttributeType

        fetch.propertiesToFetch = [monthDesc, monthNumber, expressionDescription]
        fetch.propertiesToGroupBy = [monthDesc, monthNumber]

        let sortDescriptor = NSSortDescriptor(key: "month", ascending: true)
        let sortDescriptors = [sortDescriptor]
        fetch.sortDescriptors = sortDescriptors
        fetch.resultType = .DictionaryResultType

        fetch.returnsObjectsAsFaults = false;

        do {
            results = try context.executeFetchRequest(fetch)

        } catch {
            print("Unable to fetch results")
            abort()
        }


        let months = results.flatMap{$0["shortMonth"] as? String}
        let countt = results.flatMap{$0["count"] as? Double}

        print("Months \(months)")
        print("Countt \(countt)")

这将同时按monthNumber和monthName进行分组,并将按monthNumber排序,并输出monthName和计数。

输出:

代码语言:javascript
复制
Months ["Jan", "Feb", "Mar", "Apr"]
Countt [3.0, 10.0, 3.0, 10.0]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35260533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档