Sage Washabaugh
Sage Washabaugh

Reputation: 297

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?

Upvotes: 0

Views: 712

Answers (2)

5StringRyan
5StringRyan

Reputation: 3634

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");

Upvotes: 0

Richard J. Ross III
Richard J. Ross III

Reputation: 55563

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

Upvotes: 3

Related Questions