WTF is a Protocol ?

This part of the Swift language is pretty simple to explain. A “Protocol” is what is called a “Interface” in most of the other languages out there. It is basically a set of rules that your struct, class, or enum has to conform to.

You maybe asking your self “why would I ever need this weird construct ? I know whats what. I don’t need these things if I am doing it all myself. ” Well thing is that it keeps you honest, and it keeps you on track.

protocol lifeOfABear {
var fishEatenToday: Int {get}
func eatFish()
func eatHoney()
func ClimbTree()
func sleep()
// now all you have to do is make sure you have variable and functions implemented
class PhilTheBear: lifeOfABear {
var fishEatenToday: Int = 200;
func eatFish() {
print("Go down to the east river today")
func eatHoney() {
print("Walk over to the bee hive near the old tree")
func ClimbTree() {
print("climb up the first tree that has a blue jay")
func sleep() {
print("get a sold 12 hours")

view raw
hosted with ❤ by GitHub

More importantly it opens the door to things like delegates. More on this in my next blog post.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s