简体   繁体   English

属性不符合协议

[英]Property does not conform to protocol

I am having problems getting my property to conform to my self made protocol my property is declared like this: 我在使我的财产符合我自己制定的协议时遇到问题,我的财产被这样声明:

    @property(assign)id <MainViewDatasource> datasource

And I run this code to test if it conforms to the protocol: 然后运行以下代码来测试其是否符合协议:

    if ([datasource conformsToProtocol:@protocol(MainViewDatasource)])
    NSLog(@"datasource conforms to MainViewDatasource");

    if(datasource == nil)
    NSLog(@"datasource is nil");

And in the Console it says that datasource is nil. 并且在控制台中说数据源为零。 How do I fix this? 我该如何解决?

如果未设置数据源属性,则该属性将保持默认值0x0(无)。

The code: [datasource conformsToProtocol:@protocol(MainViewDatasource)] itself only returns a Boolean value after it is executed. 代码: [datasource conformsToProtocol:@protocol(MainViewDatasource)]本身仅在执行后返回布尔值。 As others have stated, it doesn't actually set up the datasource property. 正如其他人所述,它实际上并没有设置数据源属性。 If you wanted to do some set up only if the said property conforms to a protocol, you would add something to that if block: 如果您只想在上述属性符合协议的情况下进行一些设置,则可以在if块中添加一些内容:

if ([datasource conformsToProtocol:@protocol(MainViewDatasource)])
{    
    NSLog(@"datasource conforms to MainViewDatasource");
    // do additional set up code here that is needed, now that you know your datasource
    // conforms to the MainViewDatasource protocol.
}
if(datasource == nil)
NSLog(@"datasource is nil");

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

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