Assign and array or dictionary as a Core Data Attribute?

Hello, I'm very new to Core Data and have been trying to implement it in my SwiftUI app. I have been trying to learn and find a solution to assigning an array or dictionary as an attribute to an entity but have had no success. Is there an example somewhere that can be shared on the best way to accomplish this?

An example would be my object can have attribute like color, but some objects can have multiple colors.

Thanks!
hi,

there are a number of strategies, depending on the types and how you want to manage storage.

(1) you can mark an attribute in Core Data as Transformable, and supply a ValueTransformer that turns a custom value (e.g., a color) into, say, Data or string/JSON. (i have not ever needed to create a custom ValueTransformer.)

(2) Core Data seems to know how to transform certain types of data without needing a custom ValueTransformer. An example would be an array of String, or an array of Int, or an array of UUID (all of which i have used in projects) -- and i assume an array of Double (as in the RGB components of a UIColor) would also work.

for an array of Double, you might define an attribute rgbComponents in Core Data's model editor, mark its type as Transformable, and then set the Transformer to NSSecureUnarchiveFromData and its Custom Class to [Double]. then, in code, you can simply refer to your entity's rgb components as rgbComponents[0], rgbComponents[1], and rgbComponents[2] (attach opacity as rgbComponents[3] if you want).

when you initialize one of your objects, assuming you might want the fourth opacity value included, set rgbComponents to some default, e.g., [0.9, 0.9, 0.1, 0.6], which is a nice soft yellow. or, you can set it to [] to mean "no color yet assigned," and then change it later. but be sure to provide some value upon initialization, otherwise Core Data will throw an error when your context does a save().

(3) if you want an array of custom "colors," i'd recommend just creating a new entity in Core Data named, say, "ColorDescription." it could have red, green, and blue (and opacity) Double attributes, perhaps with an identifying name (String). set a relationship from your object that is one-to-many to ColorDescription (perhaps named colors). you'll get an NSSet type for colors, and you can work with it by using the custom accessors generated by Xcode in the Core Data code and by doing something like this in your own code to get the right type of things to work with

Code Block
func colorList() -> [ColorDescription] {
// return a list of all my colors, sorted by name
if let colors = colors as? Set<ColorDescription> {
return colors.sorted(by: { $0.name! <= $1.name! }
}
return [] // it's doubtful this will happen -- you know what the type of the objects are


(4) sorry, i haven't done a Dictionary, yet, but it would be interesting to see if the same strategy as (2) above could work by setting the Custom Class to something like [String : Int].

hope that helps,
DMG


Assign and array or dictionary as a Core Data Attribute?
 
 
Q