0

I test @dynamic in NSObject and CALayer,When DynamicTest class inherits NSObject, run

test.title = @"title";

it crashed because there is no setter/getter method in the DynamicTest class.

@interface DynamicTest : NSObject

@property NSString *title;

@end

@implementation DynamicTest
@dynamic title;

@end

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

but When DynamicTest class inherits CALayer, it works!,

@interface DynamicTest : CALayer

@property NSString *title;

@end

@implementation DynamicTest
@dynamic title;

@end

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

I knew that CALayer has do something for this,I want to know the detail,Looking forward to your help,thank you!

1 Answer 1

2

Read the 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"];

Although it’s not explicitly stated, this support extends to providing dynamic accessors.

1
  • 1
    Do you know that why animating custom Layer properties should make properties as @dynamic?
    – lengain
    Dec 26, 2018 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.