gpt4 book ai didi

objective-c - 键/值观察未触发定义为 : @property (readwrite, 的属性 retain) __attribute__((NSObject)) CGImageRef thumbnailImage

转载 作者:搜寻专家 更新时间:2023-10-30 20:07:00 25 4
gpt4 key购买 nike

我有一个属性定义如下的类:

// Interface
@property (retain) __attribute__((NSObject)) CGImageRef thumbnailImage;

// Implementation
@synthesize thumbnailImage;

问题是 self.thumbnailImage = newCGImageRef; 不会触发键/值观察事件,并且永远不会调用 observeValueForKeyPath:...

我用同一个类的另一个属性 (BOOL) 测试了代码,它对那个属性工作得很好。这不应该也适用于上面的属性吗?

注意:我尝试手动设置该属性([self setValue:newValue forKey:@"thumbnailImage"],导致以下错误:

setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key thumbnailImage.

最佳答案

__attribute__((NSObject))告诉编译器将结构指针视为对象,因此这允许您保留/释放对象(通常用于 block )。此属性对 CFImageRef 是一个结构指针这一事实没有影响,尽管 KVC 会包装结构,但它不会自动包装结构指针。要解决您的问题,您需要拦截未定义键上的 KVC 以设置和返回正确的对象,或者只使用 UIImage 代替。

KVC Documentation:

Automatic wrapping and unwrapping is not confined to NSPoint, NSRange, NSRect, and NSSize—structure types (that is, types whose Objective-C type encoding strings start with {) can be wrapped in an NSValue object.

测试CF_KVC.h

#import <Foundation/Foundation.h>

typedef struct
{
int x, y, z;
} AStruct;

@interface TestCF_KVC : NSObject {}

@property (nonatomic, retain) NSString *text;
@property (nonatomic, assign) AStruct aStruct;
@property (nonatomic, retain) __attribute__((NSObject)) CFStringRef cftext;

-(void)test;

@end

测试CF_KVC.m

#import "TestCF_KVC.h"

@implementation TestCF_KVC

@synthesize text, aStruct, cftext;

-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
NSLog(@"undefined key set for %@", key);
if([key isEqualToString:@"cftext"])
{
self.cftext = (CFStringRef)value;
}
else
{
[super setValue:value forUndefinedKey:key];
}
}

-(id)valueForUndefinedKey:(NSString *)key
{
NSLog(@"undefined key get for %@", key);
if([key isEqualToString:@"cftext"])
{
return (NSString*)self.cftext;
}

return [super valueForUndefinedKey:key];
}

-(void)test
{
NSString *txt = @"text worked";
AStruct astr = { .x=1, .y=5, .z=10 };
CFStringRef cftxt = (CFStringRef)@"cftext worked";

//Test a normal NSString for KVC
[self setValue:txt forKey:@"text"];
txt = [self valueForKey:@"text"];
NSLog(@"text[%s]: %@", @encode(NSString), [self valueForKey:@"text"]);

//Test a struct for KVC
NSValue *value = [NSValue value:&astr withObjCType:@encode(AStruct)];
[self setValue:value forKey:@"aStruct"];
[[self valueForKey:@"aStruct"] getValue:&astr];
NSLog(@"aStruct[%s]: %d %d %d", @encode(AStruct), aStruct.x, aStruct.y, aStruct.z);

//Test a Core Foundation type for KVC
[self setValue:(NSString*)cftxt forKey:@"cftext"];
cftxt = (CFStringRef)[self valueForKey:@"cftext"];
NSLog(@"cftext[%s]: %@", @encode(CFStringRef), (NSString*)cftxt);
}

@end

记录调用 -test 的输出:

text[{NSString=#}]: text worked
aStruct[{?=iii}]: 1 5 10
undefined key set for cftext
undefined key get for cftext
cftext[^{__CFString=}]: cftext worked

关于objective-c - 键/值观察未触发定义为 : @property (readwrite, 的属性 retain) __attribute__((NSObject)) CGImageRef thumbnailImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7363808/

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