我有一个基于核心数据的应用程序,它是围绕一个主要实体构建的。还有其他几个实体与其连接,其中之一是名为“Notes”的实体。
此 Notes 实体具有一个日期 (NSDate)、一个描述 (NSString) 和一个其他属性。这个属性有 4 个可能的选项,其中每个实体至少有 1 个,也可能有全部 4 个。
我在想,当创建一个 Note 时,可能会有一个带有 4 个可能选项的分段控制器。 (是否可以在此处选择多个按钮?)
我还希望能够通过此选项对所有这些笔记进行排序。也就是说,创建一个仅返回选择了选项 3 的注释的提取请求。 (即使他们也选择了选项 2,甚至选择了所有 4 个选项。)
关于实现这一点的最佳方法有什么建议吗?
谢谢。
I have a Core Data based application which is built around one main entity. There are several other entities which are connected to it, one of which is an Entity called "Notes".
This Notes entity has a Date (NSDate), a Description (NSString), and one other attribute. This attribute is to have 4 possible options, of which each entity will have at least 1 and possibly all 4.
I was thinking that when a Note is being created, there could be a Segmented Controller with the 4 possible options. (Is it even possible to select multiple buttons here?)
I further want to be able to sort all of these notes by this option. That is, to create a fetch request that returns only the Notes that had Option 3 selected, for example. (Even if they also had Option 2, or even all 4 options selected.)
Any suggestions on what the best way to implement this is?
Thanks.
发布评论
评论(3)
要使用掩码来存储多个选择,您可以执行如下操作:
对于多个选择,您仍然会将组合掩码存储为
NSNumber
,例如,此
mySelections
值将对于四个选项的某种组合来说是唯一的。您可以从这个组合掩码返回到单独的掩码,以便选择分段控件的不同按钮,例如:或者:
由于它存储为
NSNumber
,因此您将能够对其进行排序具有如上所述的NSSortDescriptor
实例。To use masks for storing multiple selections, you might do something like the following:
For multiple selections, you would still store your combined mask as an
NSNumber
, e.g.This
mySelections
value will be unique to some combination of the four options. You can go back from this combined mask to individual masks, in order to select different buttons of a segmented control, e.g.:Or:
As this is stored as an
NSNumber
, you will be able to sort on it with anNSSortDescriptor
instance as described above.首先,我不会尝试使用一个属性来收集四个可能的选项,而是使用四个单独的布尔属性。这也将允许您非常轻松地过滤您的获取请求。
要配置每个布尔值,我将使用 UIButton 或 UISwitch。 UISegmentedControl 不支持多项选择。
First, instead of trying to use one attribute to collect four possible options, I would use four separate boolean attributes. This will also allow you to filter your fetch requests very easily.
To configure each boolean, I would use a UIButton or UISwitch. The UISegmentedControl does not support multiple selection.
使用枚举来定义四个选项,例如:
这些选项的编号为 0 到 5。Core
Data 将整数存储为
NSNumber
实例。您也许可以在Note
实体中保留一个名为optionType
的属性,它存储与NoteOptionType
等效的NSNumber
> 价值。您可以通过诸如
[NSNumber numberWithInt:kNoteOptionTypeOne]
之类的方式将这些选项转换为NSNumber
选项。您可以编写一个方便的方法来将
NoteOptionType
转换为字符串以放入UISegmentedControl
中,例如:像这样引用它:
在您的获取中,您可以使用
NSSortDescriptor 将 >NSNumber 值放入核心数据存储中作为排序条件,例如:
Use an enumeration to define four options, e.g.:
These will be numbered 0 through 5.
Core Data stores integers as
NSNumber
instances. You could perhaps keep an attribute in yourNote
entity that is calledoptionType
, which stores theNSNumber
equivalent of aNoteOptionType
value.You can convert these to
NSNumber
options through something like, for example,[NSNumber numberWithInt:kNoteOptionTypeOne]
.You could write a convenience method to convert a
NoteOptionType
to a string to put into aUISegmentedControl
, e.g.:Reference it like so:
In your fetch, you can use the
NSNumber
values you put in your Core Data store as criteria on which to sort, through the use of anNSSortDescriptor
, e.g.: