格式化sectionNameKeyPath NSFetchedResultsController-Swift [英] Format sectionNameKeyPath NSFetchedResultsController - Swift

查看:70
本文介绍了格式化sectionNameKeyPath NSFetchedResultsController-Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用存储的NSDate属性为我的核心数据格式化我的sectionNameKeyPath格式.它可以完全正常工作,并且可以根据NSDate将记录拉入/分类到各个部分,但是我不确定如何格式化它以显示dd-mm-yyyy.

我知道如何使用以下格式格式化NSDate:

let date = NSDate(timeIntervalSince1970:myTimeInterval)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"

我当前使用核心数据属性sectionDate作为sectionNameKeyPath

的部分划分的获取请求

FETCH

let fetchRequest = NSFetchRequest(entityName: "VisitDetails")
let primarySortDescriptor = NSSortDescriptor(key: "dateDue", ascending: false)
let sortDescriptors = [primarySortDescriptor]
fetchRequest.sortDescriptors = sortDescriptors
        
let frc = NSFetchedResultsController(
            fetchRequest: fetchRequest,
            managedObjectContext: self.managedObjectContext!,
            sectionNameKeyPath: "dateSections",
            cacheName: nil)
        
frc.delegate = self

为了澄清,我想格式化输出并仅按dd-MM-yyy进行排序.当前,它看起来像:

我假设 Hack 正在创建一个附加属性并输入格式化的字符串,然后使用它来分割结果?

解决方案

我只是针对我正在研究的项目执行了此操作.这是我所做的:

可选的,但强烈建议您设置发电机. 此处有一个很好的教程,用于设置您的核心数据项目以使用它. /p>

无论如何,请确保您的数据模型中有一个与实体关联的NSManagedObject子类.如果您尚未使用Mogenerator,则可以以苹果的方式做到.

在VisitDetails类中,添加:

var formattedDateDue: String {
    get {
        let dateFormatter = NSDateFormatter()
        // Apple suggested locale-aware technique:
        // dateFormatter.dateStyle = .ShortStyle
        // dateFormatter.timeStyle = .NoStyle
        // ..or to stick to your original question:
        dateFormatter.dateFormat = "dd-MM-yyyy"
        return dateFormatter.stringFromDate(self.dateDue)
    }
}

(假设您希望将各部分按dateDue的日期分组,否则将为计算的属性设置为dateSections属性.)然后,您从上方修改获取的结果控制器以使用计算的属性:

let frc = NSFetchedResultsController(
        fetchRequest: fetchRequest,
        managedObjectContext: self.managedObjectContext!,
        sectionNameKeyPath: "formattedDateDue",
        cacheName: nil)

仅此而已!

I am attempting to format my sectionNameKeyPath for my core data fetch using a stored NSDate attribute. It is fully working and pulling/sorting the records into sections based on the NSDate but I am not sure how to format it to display it by dd-mm-yyyy.

I know how to format NSDate using:

let date = NSDate(timeIntervalSince1970:myTimeInterval)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"

My current fetch request using core data attribute sectionDate as the section division for sectionNameKeyPath

FETCH

let fetchRequest = NSFetchRequest(entityName: "VisitDetails")
let primarySortDescriptor = NSSortDescriptor(key: "dateDue", ascending: false)
let sortDescriptors = [primarySortDescriptor]
fetchRequest.sortDescriptors = sortDescriptors
        
let frc = NSFetchedResultsController(
            fetchRequest: fetchRequest,
            managedObjectContext: self.managedObjectContext!,
            sectionNameKeyPath: "dateSections",
            cacheName: nil)
        
frc.delegate = self

So to clarify I am wanting to format the output and sort by just the dd-MM-yyy. Currently, it is looking like:

I presume a Hack is creating an additional attribute and inputting a formatted string then using this to section the results?

解决方案

I just did just this for a project that I'm working on. Here's what I did:

Optional, but highly recommended, is to set up mogenerator. There is a nice tutorial here for setting up your core data project to use it.

Regardless, in your data model, make sure you have a NSManagedObject subclass associated with the entity. If you haven't already, and you're not using mogenerator, then you can do it the apple way.

In your VisitDetails class, add:

var formattedDateDue: String {
    get {
        let dateFormatter = NSDateFormatter()
        // Apple suggested locale-aware technique:
        // dateFormatter.dateStyle = .ShortStyle
        // dateFormatter.timeStyle = .NoStyle
        // ..or to stick to your original question:
        dateFormatter.dateFormat = "dd-MM-yyyy"
        return dateFormatter.stringFromDate(self.dateDue)
    }
}

(Assuming you want your sections to be grouped on dateDue's date, otherwise make the computed attribute for your dateSections attribute.) You then modify your fetched results controller from above to use the computed attribute:

let frc = NSFetchedResultsController(
        fetchRequest: fetchRequest,
        managedObjectContext: self.managedObjectContext!,
        sectionNameKeyPath: "formattedDateDue",
        cacheName: nil)

And that's all there is to it!

这篇关于格式化sectionNameKeyPath NSFetchedResultsController-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆