gpt4 book ai didi

ios - 为什么CALayer的动态@property可以调用setter方法?

转载 作者:行者123 更新时间:2023-11-28 20:54:35 25 4
gpt4 key购买 nike

我在NSObject和CALayer中测试@dynamic,当DynamicTest类继承NSObject时,运行

test.title = @"title";

它崩溃是因为 DynamicTest 类中没有 setter/getter 方法。

@interface DynamicTest : NSObject

@property NSString *title;

@end

@implementation DynamicTest
@dynamic title;

@end

DynamicTest *test = [[DynamicTest alloc] init];
test.title = @"title";//crashed

但是当 DynamicTest 类继承 CALayer 时,它起作用了!

@interface DynamicTest : CALayer

@property NSString *title;

@end

@implementation DynamicTest
@dynamic title;

@end

DynamicTest *test = [[DynamicTest alloc] init];
test.title = @"title";//it works!

我知道CALayer为此做了一些事情,我想知道细节,期待你的帮助,谢谢!

最佳答案

阅读Core Animation Programming Guide: Key-Value Coding Extensions :

The CAAnimation and CALayer classes are key-value coding compliant container classes, which means that you can set values for arbitrary keys. Even if the key someKey is not a declared property of the CALayer class, you can still set a value for it as follows:

[theLayer setValue:[NSNumber numberWithInteger:50] forKey:@"someKey"];

虽然没有明确说明,但这种支持扩展到提供动态访问器。

关于ios - 为什么CALayer的动态@property可以调用setter方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53926901/

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