开发者

Custom Core Data entities

开发者 https://www.devze.com 2023-03-06 03:17 出处:网络
How does one create a Core Data Entity that has custom objects within it? 开发者_C百科 E.g. An entity that has the possibility of holding, e.g. images, audio clips, a custom godzilla object.

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

开发者_C百科

E.g. An entity that has the possibility of holding, e.g. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消