I'm reading up on protocols (and thinking I might be able to rewrite some code using them) but I seem to be getting stuck on what exactly makes it different than a class?
For example, if I have a class ColorController:
#import <Foundation/Foundation.h>
@interface ColorController : NSObject {
UIColor* colorToReturn;
}
- (UIColor* ) setColor : (float) red : (float) green : (float) blue;
@end
and the .m
#import "ColorController.h"
@implementation ColorController
- (UIColor* ) setColor : (float) red : (float) green : (float) blue {
float newRed = red/255;
float newGreen = green/255;
float newBlue = blue/255;
colorToReturn = [UIColor colorWithRed:newRed green:newGreen blue:newBlue alpha:1.0];
return colorToReturn;
}
@end
and then import it into another class:
ColorController* colorManager = [ColorController new];
UIColor* newColor = [colorManager setColor:66.0:66.0:66.0];
this seems to make perfect sense to convert into a protocol, since a lot of other classes could use the setColor method. But I am not sure if my understanding of protocols is off, I thought that once a protocol was declared it would be available to other classes, but since you have to still include it in the .h files, the only discernible difference to me was that with a protocol the setColor method could be called directly in whatever class I have imported the protocol to, whereas with importing a class I would have to call back to the class and method [colorManager setColor:66.0:66.0:66.0]; What exactly would be the gain here?
Now my view is probably because I'm a newbie/inexperienced with protocols and have a limited view on them and their usage so if someone could give me a brief (other than "go read the docs" : D ) reply on the benefits of protocols and maybe an example of use I'd really appreciate it.