Property does not conform to protocol

≡放荡痞女 提交于 2019-12-11 16:44:55

问题


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?


回答1:


If you don't set your datasource property, it will remain at the default value, 0x0 (nil).




回答2:


The code: [datasource conformsToProtocol:@protocol(MainViewDatasource)] itself only returns a Boolean value after it is executed. 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 ([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");


来源:https://stackoverflow.com/questions/8450051/property-does-not-conform-to-protocol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!