Stefan
Stefan

Reputation: 214

two classes, same property name, different type -> gets overridden

I have the following setup: Two classes, one imports the other, both have a property that happens to have the same name but a different type.

When I try to access the someColor property in ClazzB, I get an error:

[[[ClazzB sharedInstance] someColor] CGColor]

Error: No visible @interface for 'NSString' declares the selector 'CGColor'

The error clearly suggest that it takes the property from ClazzA, the question is why? Is there some weird name overridden going on?

Here the setup:

@interface ClazzA : NSObject {
   @private
   NSString* _someColor;
}
@property (nonatomic, copy) NSString* someColor;

ClazzB imports ClazzA and has a property with the same name but with a different type.

#import "ClazzA.h"

@interface ClazzB : NSObject {
    UIColor* _someColor;
}

@property (nonatomic, strong) UIColor* someColor;

Any help is very much appreciated!

Thanks!

Upvotes: 0

Views: 169

Answers (1)

Vitaly
Vitaly

Reputation: 515

Above I asked you for a sharing declaration of your sharedInstance method but I suspect it is

+ (id)sharedInstance;

That's a reason of a problem. In this case compiler doesn't know what exact type you will get there and just chooses first matched selector for someColor which returns NSString *.

I'd recommend to change your sharedInstance methods declaration to

+ (instancetype)sharedInstance;

or

+ (ClazzA *)sharedInstance;
+ (ClazzB *)sharedInstance;

I'd prefer instancetype way.

Upvotes: 1

Related Questions