简体   繁体   中英

What will happen if my class conforms to two protocols having same property?

Let's say I have two protocols

@protocol Playlist<NSObject>
@property(nonatomic, copy) NSString *title;
@property(nonatomic, assign) NSUInteger trackCount;
@end

and another as

@protocol Album<NSObject>
@property(nonatomic, copy) NSString *name;
@property(nonatomic, assign) NSUInteger trackCount;
@end

and there is a class which conforms to these protocols

.h file

@interface MusicLibrary <Playlist, Album>
@end

.m file

@implementation MusicLibrary
@synthesize title;
@synthesize name;
@synthesize trackCount;
@end

Which trackCount property will it refer to? Can I use trackCount twice?

It surely do not give any compile time error.

It looks to me that you are modeling your data wrong. The way you have it setup a musicLibrary is BOTH a playlist and an album. I think a more correct model would have a MusicLibrary containing many playlist and many albums. Something like:

@property (nonatomic, strong) NSArray<Album>* albums;
@property (nonatomic, strong) NSArray<Playlist>* playlists;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM