gpt4 book ai didi

objective-c - 为什么我的 NSColor 类 NSConcreteData?

转载 作者:行者123 更新时间:2023-12-03 17:02:36 25 4
gpt4 key购买 nike

在数据模型中,我将颜色定义为可转换属性,并将 ColorTransformer 分配给它。它可以工作,在同一个变压器的帮助下,我可以将它绑定(bind)到 ColorPicker,保存和打开文档。 Screenshot from data model但是当我尝试从代码访问它时:

NSColor * color = corner.color;
NSLog(@"Corner Color = %@\n%@", color, color.class);

我有 NSMutableConcreteData 对象:

bezierMesh[22164:730708] Corner Color = <040b7374 7265616d 74797065 6481e803 84014084 8484074e 53436f6c 6f720084 84084e53 4f626a65 63740085 84016301 84046666 66660183 6eb7613d 8357703e 3e8352b8 1e3f86>
NSConcreteMutableData

修改 getter 和 setter 没有帮助:

- (NSColor *)color {

[self willAccessValueForKey:@"color"];
NSData *colorData = [self primitiveValueForKey:@"color"];
[self didAccessValueForKey:@"color"];
if (colorData == nil) {
return nil;
}


NSColor * color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
return color;
}

- (void)setColor:(NSColor *)color {
NSLog(@" -------------- > Setting color %@", color);
NSData * theData=[NSArchiver archivedDataWithRootObject:color];
[self willChangeValueForKey:@"color"];
[self setPrimitiveValue:theData forKey:@"color"];
[self didChangeValueForKey:@"color"];
}

它会导致错误:

[NSKeyedUnarchiver initForReadingWithData:]: non-keyed archive cannot be decoded by NSKeyedUnarchiver

如何获取颜色NSColor?

最佳答案

这是我不久前编写的一个 NSString <-> NSColor 转换器,它旨在使用 NSUserDefaults 中的 RGB 值存储颜色因为我不喜欢默认使用 NSData 对象:

StringColourTransformer.h:

@interface StringColourTransformer : NSValueTransformer

+ (NSString *)toString:(NSColor *)value;
+ (NSColor *)fromString:(NSString *)value;

@end

StringColourTransformer.m:

#import "StringColourTransformer.h"

@implementation StringColourTransformer

+ (NSString *)toString:(NSColor *)value {
StringColourTransformer *transformer = [[StringColourTransformer alloc] init];
NSString *str = [transformer reverseTransformedValue:value];
return str;
}

+ (NSColor *)fromString:(NSString *)value {
StringColourTransformer *transformer = [[StringColourTransformer alloc] init];
NSColor *color = (NSColor *)[transformer transformedValue:value];
return color;
}

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

+ (BOOL)allowReverseTransformation {
return YES;
}

- (id)transformedValue:(id)value {
CGFloat r = 0.0, g = 0.0, b = 0.0, a = 1.0;

// Only NSString classes are reverse-transformed
if ([value isKindOfClass:[NSString class]]) {
NSString *stringValue = (NSString *)value;
sscanf([stringValue UTF8String],
#ifdef __x86_64
"%lf %lf %lf %lf",
#else
"%f %f %f %f",
#endif
&r, &g, &b, &a);
}

return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];
}

- (id)reverseTransformedValue:(id)value {
CGFloat r = 0.0, g = 0.0, b = 0.0, a = 1.0;

// Only NSColor classes are transformed
if ([value isKindOfClass:[NSColor class]]) {
NSColor *colourValue = (NSColor *)value;
NSColor *converted = [colourValue colorUsingColorSpaceName:@"NSCalibratedRGBColorSpace"];
[converted getRed:&r green:&g blue:&b alpha:&a];
}

return [NSString stringWithFormat:@"%.3f %.3f %.3f %.3f", r, g, b, a];
}

@end

如您所见,我提供了几个类方法,以便更轻松地从代码中使用它(toStringfromString)。

它需要在应用程序生命周期的早期注册,以便可以用于转换 IB 内的值(这是从单例中调用的,其中 _stringColourTransformer 是实例变量):

_stringColourTransformer = [[StringColourTransformer alloc] init];
[NSValueTransformer setValueTransformer:_stringColourTransformer
forName:@"StringColourTransformer"];

转换非常简单:

NSColor *colour = [StringColourTransformer fromString:@"1.0 1.0 0.0 1.0"];

注意:现在查看代码,我可能会在 toString/fromString 方法中使用 [NSValueTranformer valueTransformerForName:],而不是创建每次都有一个新实例。可能更便宜。

关于objective-c - 为什么我的 NSColor 类 NSConcreteData?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26585988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com