I previously wrote Core Data Summary series. Today I will review it a little and make some supplements.
In the first article in this series "Infrastructure" (the articles in February are coming soon !), There is a simple Attribute with Entity:
The data type and Boolean value are all expressed by NSNumber; the string type is represented by NSString; the time type is represented by NSDate; the binary data type is represented by NSData; the non-standard type is represented by Transformable;
Attribute also has its own Properties. For example, Transient indicates that the disk does not need to be persisted, and Optional indicates a non-required field (if the required field is empty, an error occurs when saving it ), indexed indicates the index field.
Here we mainly discuss Binary Data and Transformable.
Taking the storage of UIImage as an example, if the Binary Data type is used for storage, set the field to the corresponding type:
@property (nonatomic, retain) NSData * image;
Before assigning values, you must convert the UIImage type to the NSData type. This step may be convenient for the UIImage type and can be called directly like
UIKIT_EXTERN NSData *UIImagePNGRepresentation(UIImage *image);
But for other types, especially custom types, it is very troublesome to perform such operations each time, using the Transformable type, you can use NSValueTransformer and its extension to optimize this step:
#import
@interface UIImageTransformer : NSValueTransformer@end
A custom Person structure may be called DIYPersonTransformer.
With such a Transformer, you can specify it in the panel, but several methods need to be provided before it can work smoothly:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD48cHJlIGNsYXNzPQ = "brush: java;"> + (BOOL) allowsReverseTransformation {return YES ;}
It indicates that two-way conversion can be performed. Both write-in and read-out are available.
+ (Class)transformedValueClass { return [NSData class];}
Indicates the actual storage type.
- (id)reverseTransformedValue:(id)value{ //}- (id)transformedValue:(id)value{ // }
Indicates the actual conversion method.
However, the topic of using Core Data to store UIImage is further discussed here:
Http://stackoverflow.com/questions/4158286/storing-images-in-core-data-or-as-file
Http://stackoverflow.com/questions/16685812/how-to-store-an-image-in-core-data
There are also links to the content discussed above:
Http://stackoverflow.com/questions/14256762/what-is-the-difference-between-transformable-and-binary-data
Http://stackoverflow.com/questions/6783091/when-not-to-use-core-data-type-transformable
Http://stackoverflow.com/questions/1562676/best-practice-array-dictionary-as-a-core-data-entity-attribute
Http://stackoverflow.com/questions/3014498/what-can-i-do-with-an-transformable-attribute-type-in-core-data-on-the-iphone
Brief Talk About Core Data Series, Part 10: About NSEntityDescription's Attributes
Jason Lee @ Hangzhou
Blog: http://blog.csdn.net/jasonblog
WEAVER: http://weibo.com/jasonmblog