Question

Is it possible to have NSSet of NSString objects in a core data object?

For example I have ClassA that is a subclass of NSManagedObject, I want that ClassA will hold a set of strings.

I know I can create some kind of ManagedString that is a subclass of NSManagedObject and use it, but I prefer not.

Was it helpful?

Solution

If you set the type of an attribute to "Transformable" then you can simply assign an NSSet to it:

e.setOfStrings = [NSSet setWithObjects:@"a", @"b", @"c", nil];

Core Data will use the NSCoding methods to transform the set into a data blob and store that in the SQLite store.

A disadvantage might be that you cannot search for strings in the set in a fetch request. If you need that feature then you have to use a to-many relationship instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top