使用NSValueTransformer设置NSManagedObject实例的值 [英] Using an NSValueTransformer to set values of an NSManagedObject instance

查看:165
本文介绍了使用NSValueTransformer设置NSManagedObject实例的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义NSValueTransformer将颜色信息存储在我的Core Data存储中。一旦颜色数据已经存储在商店中(即,一旦应用程序运行并退出,就可以在Transformable数据和UIColor实例之间进行转换)。但是,当我第一次运行该应用程序并从文本文件中加载这些值时,它们卡住为NSCFStrings。

I am using a custom NSValueTransformer to store color information in my Core Data store. The transformation between Transformable data and a UIColor instance works great once the color data is in the store already (ie once the app has been run and quit once already). However when I first run the app and am loading in these values (from a text file) they "stuck" as NSCFStrings.

在这行代码中, attributes是字典中的键是NSManagedObject属性名称,值是这些属性的期望值。在我的颜色示例中,键值对是颜色: 1,1,1,0.5

In this line of code "attributes" is a dictionary has keys which are NSManagedObject attribute names and values that are the expected values for those attributes. In my color example the key value pair is "color":"1,1,1,0.5"

[object setValue:[attributes valueForKey:attribute] forKey:attribute];

在此实例中, color的值现在将保留为字符串,直到通过我的NSValueTransformer对其进行转换为止然后再次运行该应用程序时,将其重新转换为UIColor。

The value for "color" will now remain a string in this instance until it's get transformed via my NSValueTransformer and then retransformed into a UIColor when the app gets run again.

我可以在此处执行与NSValueTransformer中相同的转换,但这是在我写的实用程序类理论上可以用于任何变压器。我还想过要找到一种方法,将所有新创建的NSManagedObject实例从内存中取出,从而强制进行转换,但这似乎很容易。

I could just do the same transform here that I'm doing in the NSValueTransformer, but this is in a utility class I wrote that could theoretically be used for any transformer. I also thought of finding a way to get all newly created NSManagedObject instances out fo memory thereby forcing the transformation to go through, but that just seems like a hack.

注意:此 hack对我有用,让我继续,但仍然很难看。如果您遇到类似的问题/正在寻找可行的解决方案,请使用NSManagedObjectContext的reset方法。

Note: This "hack" works for me and let's me continue, but still feels ugly. Use NSManagedObjectContext's reset method if you're having similar problems / looking for a "just work" solution.

有什么想法吗?

(我有一种直觉,这类似于 为什么我的可变形Core Data属性未使用我的自定义NSValueTransformer?,但是在标题之外,我们的问题似乎有所不同)

(I have a hunch this is similar to " Why is my transformable Core Data attribute not using my custom NSValueTransformer? " but outside of the title our problems seem to be different)

这是我的NSValueTransformer

Here is my NSValueTransformer

@implementation UIColorRGBValueTransformer

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

+ (BOOL)allowsReverseTransformation
{
    return YES;
}

- (id)transformedValue:(id)value
{
    return [value 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];

    return nil;
}

@end


推荐答案

您实际上并没有按预期使用transformable属性。您的方法在理论上应该可以使用,但是很丑陋。

You're really not using the transformable attribute as intended. Your method should work in theory but it is an ugly kludge.

当您要执行非标准操作时,通常只需为系统定义的类编写自定义值转换器。在绝大多数情况下,首选方法是使用默认的 NSKeyedUnarchiveFromDataTransformerName 。任何实现NSCoding协议的类都可以使用该值转换器。

You usually only have to write a custom value transformer for a system defined class when you want to do something non-standard. In the vast majority cases, the preferred method is to use the default NSKeyedUnarchiveFromDataTransformerName. Any class that implements the NSCoding protocol can use that value transformer.

由于UIColor确实实现了NSCoding协议,因此您可以将属性设置为 transformable,而 NSKeyedUnarchiveFromDataTransformerName 自动填充转换器名称(至少在Xcode 3.x中是这样)。在使用中,Core Data将创建适当的访问器,以便您可以像设置其他任何键一样设置和获取UIColor属性:

Since UIColor does implement the NSCoding protocol, you can just set the attribute to 'transformable' and the NSKeyedUnarchiveFromDataTransformerName will populate the transformer name automatically (at least it did in Xcode 3.x.) In use, Core Data will create the appropriate accessors so you can set and get the UIColor attribute just like any other key:

[aMoObject setValue:aUIcolorObj forKey:@"colorAttributeName"];

根据经验,如果遇到以下情况,API将为您完成大部分工作: API类。如果您发现自己正在努力学习系统课程,则可能错过了一些东西。

As a good rule of thumb, the API will do most of the work for you in the case of API classes. If you find yourself working hard with a system class, you've probably missed something.

这篇关于使用NSValueTransformer设置NSManagedObject实例的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆