可转换属性的奇怪警告

时间:2011-10-15 19:28:17

标签: cocoa core-data

我使用的是可转换的属性,一切正常,而不是那些警告:

15.10.11 22:14:07,191 cdtool: CoreData: warning: no NSValueTransformer with class name 'ArrayToDataTransformer' was found for attribute 'directions' on entity 'DatabaseConnections'
15.10.11 22:14:07,191 cdtool: CoreData: warning: no NSValueTransformer with class name 'ArrayToDataTransformer' was found for attribute 'updateChoices' on entity 'DatabaseConnections'
15.10.11 22:14:07,193 cdtool: CoreData: warning: no NSValueTransformer with class name 'ArrayToDataTransformer' was found for attribute 'testingResult' on entity 'DestinationsListWeBuy'

任何人都可以告诉你如何移出这个警告。 当然,一切都是正确的,属性是可选的,可转换的。 bellow是一个类实现:

#import <Foundation/Foundation.h>

@interface ArrayToDataTransformer : NSValueTransformer {

}

@end



#import "ArrayToDataTransformer.h"


@implementation ArrayToDataTransformer

+ (BOOL)allowsReverseTransformation {
    return YES;
}

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

- (id)transformedValue:(id)value {
    //Take an NSArray archive to NSData
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:value];
    return data;
}

- (id)reverseTransformedValue:(id)value {
    //Take NSData unarchive to NSArray 
    NSArray *array = (NSArray*)[NSKeyedUnarchiver unarchiveObjectWithData:value];
    return array;
}

@end

1 个答案:

答案 0 :(得分:1)

在调用自定义变压器之前,您必须注册:

+[NSValueTransformer setValueTransformer:forName:]

...否则,运行时不知道变换器是否存在。