具有 NSData 属性的核心数据
我正在开发一个基于 Core Data 的 iOS 应用程序,其中我将 coredata 中的数组字符串存储为 NSData(二进制数据)。 在获取数据时,我如何编写一个 NSPredicate
,它只会检索 NSData
值包含给定字符串的结果。
如果 name
属性是一个字符串,那么我可以编写如下谓词。
NSString *inputName = @"bbb";
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"name = %@",inputName];
我的问题是
那么谓词是什么
NSArray *anArray = [NSArray arrayWithObjects:@"aaa",@"bbb",@"ccc",nil];
NSData *arrayData = [NSArchiver archivedDataWithRootObject:anArray];
如果 name
是 NSData
并且来自arrayData
的这个 NSData
存储为 name<, Core Data 中的 /code> 属性。
我希望这些信息足以理解我的问题。 提前致谢。
I am working on a Core Data
based iOS application , in which i stored an array strings in coredata as NSData
(binary data).
While fetching the data , how can i write a NSPredicate
which will only retrieve the result with NSData
value contain given string .
if name
attribute is a string then i can write a predicate like below.
NSString *inputName = @"bbb";
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"name = %@",inputName];
my question is
what will be the predicate if name
is NSData
and this NSData
from
NSArray *anArray = [NSArray arrayWithObjects:@"aaa",@"bbb",@"ccc",nil];
NSData *arrayData = [NSArchiver archivedDataWithRootObject:anArray];
arrayData
is stored as name
attribute in Core Data .
I hope this information is enough to understand my problem.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了答案..将
NSArray
转换为NSSet
并将其存储在实体中i found the answer ..converted
NSArray
toNSSet
and store it in entity如果我正确理解您的问题和后续评论,并且您使用存储为数据的数组来表示层次关系,那么您显然没有按设计使用核心数据。
相反,我建议您创建一个单独的实体来代表员工,然后在他们和主管之间建立关系?
虽然它不是特定于 iOS 的,但这个 Apple 的教程 演示了这一概念。
If I understand your question and subsequent comments correctly, and you are using an array stored as data to represent a hierarchical relationship then you are clearly not using Core Data as designed.
Instead, I recommend you create a separate entity to represent the employees and then set up a relationship between them and the supervisor?
Although it's not iOS specific, this tutorial by Apple demonstrates that very concept.
根据您的评论,我认为您需要重新审视您的架构。如果 Supervisor 和 Supervisor 之间存在一对多关系;员工(许多员工可以有相同的主管),您应该创建一个实体
Employee
,并为其添加属性supervisorID。然后,您可以使用类似wheresupervisorID="123"
查询您的 Employee 表。如果您无法重新设计架构,可以将 NSData 转换为 NSString &使用
-initWithData:encoding:
将其保存在 CoreData 中。HTH,
阿克谢
Based on your comments, I think you need to revisit your schema. If you have a one-many relationship between Supervisor & Employee (Many employees can have the same supervisor), you should create an Entity
Employee
, and add an attribute supervisorID to it. Then you can query your Employee table with something like-where supervisorID="123"
.If you cannot redesign your schema, you can convert NSData to NSString & save it in CoreData using
-initWithData:encoding:
.HTH,
Akshay