自动属性合成不会合成协议中声明的属性,即使它已实现

时间:2013-06-07 06:28:26

标签: objective-c xcode4.5

我收到了这个错误:

/business/Dropbox/badgers/BadgerNew/BGProfileView.m:56:17: Auto property synthesis will not synthesize property declared in a protocol

我知道自动属性合成不会合成协议中声明的属性

所以我把自己合成了。

这是协议:

@protocol BGIhaveNavigationController <NSObject>

@property (readonly)UINavigationController * navigationController;//This is the problematic property

@end

@protocol BGCommonProtocol <NSObject>


@end

至于实施

@interface BGProfileView : UIViewController

@end

是一个UIViewController,我们知道UIViewController有一个navigationController属性。那有什么不对?

当我使用它时,事情会出现问题:

@interface BGProfileView () <BGReviewsTableDelegateProtocol>

BGReviewsTableDelegateProtocol协议继承BGIhaveNavigationController协议

我可以通过添加以下内容来删除警告:

-(UINavigationController *) navigationController
{
    return self.navigationController;
}

但那太荒谬了。从某种意义上说

-(UINavigationController *) navigationController
    {
        return self.navigationController;
    }

-(UINavigationController *) navigationController已经通过UINavigationController存在

1 个答案:

答案 0 :(得分:10)

使用

@dynamic navigationController;

这告诉编译器属性实现是“其他地方”,它应该相信该需求将在运行时完成。实际上,这意味着它在超类中。

如果你试图自己实现它,你最终会得到重复的存储(因此可能无法按预期工作)或递归。