简体   繁体   中英

Custom Core Data entities

How does one create a Core Data Entity that has custom objects within it?

Eg An entity that has the possibility of holding, eg images, audio clips, a custom godzilla object.

How are these saved and loaded as well? Using NSData?

The best way would be to use a 'transformable' attribute. See the Core Data documentation here for more information:

Non-Standard Persistent Attributes

If the custom classes conform to the NSCoding protocol you can make use of Transformable Attributes . A short quote from Apple's chapter Non-Standard Persistent Attributes pf Core Data Programming Guide :

The idea behind transformable attributes is that you access an attribute as a non-standard type, but behind the scenes Core Data uses an instance of NSValueTransformer to convert the attribute to and from an instance of NSData. Core Data then stores the data instance to the persistent store.

Yes, you can do it using NSData

for example converting a UIImage to NSData

UIImage *img = [UIImage imageNamed:@"some.png"];

NSData *dataObj = UIImageJPEGRepresentation(img, 1.0);

and then you can save it in the directory using

[dataObj writeToFile:fileName atomically:YES];

Here fileName is the path of file in the directory

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