Protocol on method declaration?

187 Views Asked by At

I'm starting to use the Nimbus framework and I just ran across this syntax for the first time. It looks like they are using some kind of protocol in the method declaration and then when declaring a variable. I've only seen protocols used in the header file right after the class name so this is completely new to me.

- (UIView<NIPagingScrollViewPage>*)pagingScrollView:(NIPagingScrollView *)pagingScrollView pageViewForIndex:(NSInteger)pageIndex {

Also:

UIView<NIPagingScrollViewPage>* pageView = nil;

What exactly does this mean? Why are they using this format?

2

There are 2 best solutions below

0
On BEST ANSWER

That declaration makes sure that the UIView returned conforms to the NIPagingScrollViewPage protocol. The compiler will emit a warning if the method tries to return an object that isn't declared to conform.

A more common usage of that syntax would be a delegate, as you'll allow any class that conforms to the protocol to be the delegate, so that syntax is used to make sure the class conforms to the protocol.

-(void)setDelegate:(id<SampleDelegate>)del //Makes sure that del conforms to the protocol SampleDelegate, the compiler will emit a warning
0
On

This is just the way to declare that it is confirming to that protocol. Otherwise warnings will be shown. Then you have to use id.

So It is always a good practice to use (datatype<protocol>*)variableName