Reputation: 12596
I need to have an array of NSNumber* as an attribute on an entity in Core Data. I am thinking I can just store them as string, then in my DAL I can treat them all as an array of NSNumber* but then parse the array into a string with a space in between each value. I could set and get the "intervals" as array of NSNumber*, but actually store them as string. Is this a bad idea?? Is there a better way to do this?
I got the idea to do this off of this answer: Store NSArray In Core Data Sample Code?
Upvotes: 1
Views: 496
Reputation: 539675
To store an array of numbers as a single attribute, you can simply define the property type as "Transformable". Then you can just assign the array to the property, for example
NSArray *arrayOfNumbers = …;
object.numbers = arrayOfNumbers;
The Core Data accessor methods will then automatically translate the array to some data blob which is stored in the SQLite file, and transform it back when you read the property.
One disadvantage is that you cannot access the array elements in search predicates. If you need that then you should define a to-many relationship instead.
Upvotes: 2
Reputation: 2369
You can keep your numbers as NSString separated by , and after get and array from this NSString with this code.
//Get your string of numbers from CoreData
NSString *stringNumbers = [[yourClassDAO instance] getNumbersString];
//Transform them in an array
NSArray *arraNumbers = [stringNumbers componentsSeparatedByString:@","];
Upvotes: 2