I have an Entity called "Item" with an attribute called "colorArray" with type "Transformable".
colorArray could be, for example:
[["Red", "Blue", "Green"], ["Red"], ["Blue", "Green"], ["Green"], ["Blue"], ["Blue", "Green", "Red"]]
I then save colorArray to core data using:
newEntry.colorArray = colorArray as NSObject
I want to retrieve the colorArray from core data (in the same array format it is saved in), what is the best way to do this?
I'm not sure why you're using as NSObject
or for as! [NSArray]
as! [NSArray]
, because neither are necessary or useful for the example you give.
With a colorArray
attribute configured as follows:
It's possible to assign the array from your question to the attribute with
myObject.colorArray = [["Red", "Blue", "Green"], ["Red"], ["Blue", "Green"], ["Green"], ["Blue"], ["Blue", "Green", "Red"]]
Likewise it's possible to retrieve the value with
if let colorArray = myObject.colorArray {
print("Color array: \(colorArray)")
colorArray.forEach { (entry) in
print("Array entry: \(entry)")
}
}
The value persists in Core Data, so if you kill the app and relaunch it, the data is still there.
You may be overcomplicating this by using those typecasts, and causing errors as a result.
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.