當系統提供的類型不能達到我們的使用要求時,比如我想在CoreData中存儲UIColor,該怎麼辦呢?
這時候就要用到CoreData中非常強大的一個存儲類型了:Transformable 下面將通過一個例子簡單介紹下如何存儲像UIColor這種數據 1、創建實體模型 2、根據模型生成數據模型,並且創建用於轉換Color的類,繼承自NSValueTransform 3、在.m文件中有4個必須實現的方法 復制代碼 1 // 2 // ColorTransformer.m 3 // CoreData自定義模型數據 4 // 5 // Created by Liu Feng on 14-3-1. 6 // Copyright (c) 2014年 Liu Feng. All rights reserved. 7 // 8 9 #import "ColorTransformer.h" 10 11 @implementation ColorTransformer 12 13 /** 允許轉換 */ 14 + (BOOL)allowsReverseTransformation 15 { 16 return YES; 17 } 18 19 /** 轉換成什麼類 */ 20 + (Class)transformedValueClass 21 { 22 return [NSData class]; 23 } 24 25 /** 返回轉換後的對象 */ 26 - (id)transformedValue:(id)value 27 { 28 // 將color轉成NSData 29 UIColor *color = (UIColor *)value; 30 31 CGFloat red, green, blue, alpha; 32 [color getRed:&red green:&green blue:&blue alpha:&alpha]; 33 34 CGFloat components[4] = {red, green, blue, alpha}; 35 36 NSData *dataFromColor = [[NSData alloc] initWithBytes:components length:sizeof(components)]; 37 38 return dataFromColor; 39 } 40 41 /** 重新生成原對象 */ 42 - (id)reverseTransformedValue:(id)value 43 { 44 NSData *data = (NSData *)value; 45 46 CGFloat components[4] = {0.0f, 0.0f, 0.0f, 0.0f}; 47 48 [data getBytes:components length:sizeof(components)]; 49 50 UIColor *color = [UIColor colorWithRed:components[0] green:components[1] blue:components[2] alpha:components[3]]; 51 52 return color; 53 } 復制代碼 4、完成了以上步驟,我們就可以順序的將UIColor對象存到數據庫中,並且從中取出,下面是測試用代碼 創建數據庫管理類 4.0 實現.m文件,代碼如下 復制代碼 1 #import "CoreDataManager.h" 2 #import "Flower.h" 3 #import <CoreData/CoreData.h> 4 5 @implementation CoreDataManager 6 7 - (id)init 8 { 9 if (self = [super init]) { 10 // 創建數據庫 11 [self openDB]; 12 } 13 return self; 14 } 15 16 - (void)openDB 17 { 18 // 1. 合並模型 19 NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil]; 20 21 // 2. 永久存儲 22 NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model]; 23 24 NSURL *fileURL = [NSURL fileURLWithPath:@"/Users/liufeng/Desktop/CoreData自定義模型數據/abc.db"]; 25 26 NSError *error = nil; 27 [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:fileURL options:nil error:&error]; 28 29 if (error) { 30 NSLog(@"數據庫創建失敗"); 31 }else { 32 NSLog(@"數據庫創建成功"); 33 } 34 35 if (!_ctx) { 36 _ctx = [[NSManagedObjectContext alloc] init]; 37 } 38 39 _ctx.persistentStoreCoordinator = store; 40 } 復制代碼 4.1 最後是ViewController.m文件中的測試代碼 復制代碼 1 #import "ViewController.h" 2 #import "Flower.h" 3 #import "CoreDataManager.h" 4 5 @interface ViewController () 6 7 @end 8 9 @implementation ViewController 10 11 - (void)viewDidLoad 12 { 13 [super viewDidLoad]; 14 15 CoreDataManager *manager = [[CoreDataManager alloc] init]; 16 17 Flower *flower = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([Flower class]) inManagedObjectContext:manager.ctx]; 18 19 flower.name = @"liufeng"; 20 flower.color = [UIColor grayColor]; // 如果出現警告,請將flower模型內的 color 類型 從id 改成 UIColor * 21 22 if ([manager.ctx save:nil]) { 23 NSLog(@"保存成功"); 24 } 25 26 NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([Flower class])]; 27 28 NSError *error = nil; 29 NSArray *array = [manager.ctx executeFetchRequest:request error:&error]; 30 31 for (Flower *flower in array) { 32 NSLog(@"---%@-%@", flower.name, flower.color); 33 } 34 }