Inheriting properties, setter is not synthesized when readwrite in an inherited property from readonly

I found strange behavior when working with a property that was inherited as readonly and than redeclared in an inherited class like readwrite

In Ah

@interface A : NSObject

@property (nonatomic, strong, readonly) NSObject * someProperty;

@end

In bh

@interface B : A

// no matter if here
// @property (nonatomic, strong, readwrite) NSObject * someProperty;

- (void)foo;

@end

In bm

@interface B()

// no matter if here
@property (nonatomic, strong, readwrite) NSObject * someProperty;

@end

@implementation B

- (void)foo {

    NSLog(@"%@", self.someProperty);

    // crash here with unrecognized selector setSomeProperty:
    self.someProperty = [NSObject new];
}

@end

call

self.someProperty = [NSObject new];

causes code failure on unrecognized selector "setSomeProperty:"

the study showed that it seems that the setter did not receive synthesis, even if it is declared as readwrite

Why is this happening? The compiler did not indicate any warnings that this would happen, and I did not find that this behavior was logged.

+3
2

@synthesize B.m, :

@synthesize someProperty = _someProperty;

, readonly, . . readwrite . @synthesize B.

, !

enter image description here

+2

, , : , , .

readonly A, . B . , B , "" A ( ).

, "" , getter setter ( @dynamic). , .

, , , (private):

// A.h
@interface A : NSObject
@property (nonatomic, strong, readonly) NSObject * someProperty;
@end

// A.m
@interface A()
@property (nonatomic, strong, readwrite) NSObject * someProperty;
@end

@implementation A
@end

setter, getter A, B - .

+2

Source: https://habr.com/ru/post/1548328/


All Articles