objective-c – 为什么编译器在子类中重新声明基类的readwrite属性为readonly时会发出警告?

objective-c – 为什么编译器在子类中重新声明基类的readwrite属性为readonly时会发出警告?,第1张

概述后来引用的Apple文档似乎表明这是允许的,尽管我承认从来没有理由在子类中直到现在才这样做. 我有一个带有公共readwrite属性的基类和一个子类,我将该属性重新声明为readonly.子类还有一个类扩展,它再次将该属性重新声明为readwrite,以实现常见的“public readonly,private readwrite”Objective-C模式.但是,我得到以下编译器警告: warn 后来引用的Apple文档似乎表明这是允许的,尽管我承认从来没有理由在子类中直到现在才这样做.

我有一个带有公共reaDWrite属性的基类和一个子类,我将该属性重新声明为Readonly.子类还有一个类扩展,它再次将该属性重新声明为reaDWrite,以实现常见的“public Readonly,private reaDWrite”Objective-C模式.但是,我得到以下编译器警告:

warning: Semantic Issue: Attribute 'Readonly' of property 'foo' restricts attribute 'reaDWrite' of property inherited from 'Base'

我在10.7上使用带有LLVM 2.1的Xcode 4.1 build 4B110(尽管LLVM GCC4.2和GCC4.2给出了相同的警告).

这是一个展示编译器警告的精简示例:

#import <Foundation/Foundation.h>@interface Base : NSObject@property (nonatomic,reaDWrite) BOol foo;@end@implementation Base@dynamic foo;@end// Subclass@interface Sub : Base@property (nonatomic,Readonly) BOol foo;@end// Class extension @interface Sub ()@property (nonatomic,reaDWrite) BOol foo;@end@implementation Sub@dynamic foo;  // it warns with @synthesize as well@end

这是Apple The Objective-C Programming Language的相关段落:

Property Redeclaration

You can redeclare a property in a subclass,but (with the exception of
Readonly versus reaDWrite) you must repeat its attributes in whole in
the subclasses. The same holds true for a property declared in a
category or protocol—while the property may be redeclared in a category
or protocol,the property’s attributes must be repeated in whole.

If you declare a property in one class as Readonly,you can redeclare it
as reaDWrite in a class extension (see “Extensions”),in a protocol,or
in a subclass (see “Subclassing with PropertIEs”). In the case of a class
extension redeclaration,the fact that the property was redeclared prior
to any @synthesize statement causes the setter to be synthesized. The
ability to redeclare a read-only property as read/write enables two
common implementation patterns: a mutable subclass of an immutable class
(Nsstring,NSArray,and NSDictionary are all examples) and a property that
has a public API that is Readonly but a private reaDWrite implementation
internal to the class. The following example shows using a class extension
to provIDe a property that is declared as read-only in the public header
but is redeclared privately as read/write.

我一直在类扩展中重新声明公共Readonly属性reaDWrite,但我想我从来没有理由做一个子类.但是,除非我读错了,否则上面的段落似乎表明它是犹太人.任何人都可以让我直接和/或调和文档和编译器之间的明显冲突吗?

我为什么要这样做?当然,我的现实情况更复杂.如果需要的话,我可以进行设计更改以解决这个问题,但这似乎是最不摩擦的替代方案(完全需要这样做是由其他变化驱动).

解决方法 它表示你可以将Readonly属性重新声明为reaDWrite但是你正在做相反的事情.你不能/不应该这样做,因为它可以这样做:
Sub* s = [[[Sub alloc] init] autorelease];Base* b = s; b.foo = YES; //legal for `Base` objects,but not legal for `Sub` objects

这违反了the Liskov Substitution Priciple.

总结

以上是内存溢出为你收集整理的objective-c – 为什么编译器在子类中重新声明基类的readwrite属性为readonly时会发出警告?全部内容,希望文章能够帮你解决objective-c – 为什么编译器在子类中重新声明基类的readwrite属性为readonly时会发出警告?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1253976.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存