您的位置:首页 > 产品设计 > UI/UE

core data UIColor转换为 自定义数据类型 (其他类型数据 转换同理)

2012-07-27 09:38 501 查看
转载说明:(谢谢)
http://blog.csdn.net/a21064346/article/details/7792074
点击打开链接

不好意思,因为上传这段代码之后,就忙着写项目,当时也没有注意到代码不好复制什么的。

现在过来补充说明一下:下面这个其实就是自己去 写一个NSValueTransformer的类

它的思想是 将coredata关键字的属性 设置为tansformable的type,这样你才好用其他格式的来进行替换

装载它的容器 是一个NSData的数据格式。下面是例子

对于其他类型的数据,你也可以这样用。比如 数组类型的数据。

#import <Foundation/Foundation.h>

@interface UIColorRGBValueTransformer : NSValueTransformer

@end

#import "UIColorRGBValueTransformer.h"

@implementation UIColorRGBValueTransformer

+ (Class)transformedValueClass
{
return [NSData class];
}

+ (BOOL)allowsReverseTransformation
{
return YES;
}

- (id)transformedValue:(id)value
{
UIColor* color = value;
const CGFloat* components = CGColorGetComponents(color.CGColor);
NSString* colorAsString = [NSString stringWithFormat:@"%f,%f,%f,%f", components[0], components[1], components[2], components[3]];
return [colorAsString dataUsingEncoding:NSUTF8StringEncoding];
}

- (id)reverseTransformedValue:(id)value
{
NSString* colorAsString = [[[NSString alloc] initWithData:value encoding:NSUTF8StringEncoding] autorelease];
NSArray* components = [colorAsString componentsSeparatedByString:@","];
CGFloat r = [[components objectAtIndex:0] floatValue];
CGFloat g = [[components objectAtIndex:1] floatValue];
CGFloat b = [[components objectAtIndex:2] floatValue];
CGFloat a = [[components objectAtIndex:3] floatValue];

return [UIColor colorWithRed:r green:g blue:b alpha:a];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息