简体   繁体   English

如果我的课程符合具有相同属性的两个协议,将会怎样?

[英]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 .h文件

@interface MusicLibrary <Playlist, Album>
@end

.m file .m文件

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

Which trackCount property will it refer to? 它将引用哪个trackCount属性? Can I use trackCount twice? 我可以两次使用trackCount吗?

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. 设置musicLibrary的方式既是播放列表又是专辑。 I think a more correct model would have a MusicLibrary containing many playlist and many albums. 我认为更正确的模型应该是一个MusicLibrary,其中包含许多播放列表和许多专辑。 Something like: 就像是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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