简体   繁体   中英

iOS Core Data: Adding attributes with Foundation objects

I'm building an iOS app using Core Data framework.

Now I've been familiar with Core Data entities and relationships, but I'm wondering whether there's a way to declare any Core Data entity having objects from iOS Foundation framework.

For example, I want a Core Data entity to have NSSet entity like:

Foo *foo = [NSEntityDescription insertNewObjectForEntityForName:@"Foo" inManagedObjectContext:managedObjectContext];
NSSet *set = foo.set;
for (NSString *string in set) {
    // do something
}

Is there any way to make this happen using Core Data framework?

There is a way to do this, but I'm not going to tell you because it does not sound like this would be the right solution to your problem.

You say you are familiar with Core Data entities and relationships, so use them!

Instead of a Foo object owning a set of strings, you can give it a set of Bar objects, where each Bar object has a string property. You can model this in Core Data by making the Foo entity have an unordered, to-many relationship to Bar with a cascade delete rule, where the inverse is a to-one relationship with a nullify delete rule.

数据模型

You'll really appreciate having your data explicitly modelled in Core Data when you find complex queries of your data set can be performed easily and efficiently using NSFetchRequest .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM