Skip to content

Instantly share code, notes, and snippets.

@imack
Created June 12, 2015 15:03
Show Gist options
  • Save imack/65f78c75bf722e9d9f01 to your computer and use it in GitHub Desktop.
Save imack/65f78c75bf722e9d9f01 to your computer and use it in GitHub Desktop.
Protocols, delegates, and Categories
//
// CreatureProtocol.h
// Classroom
//
// Created by Ian MacKinnon on 2015-06-12.
// Copyright (c) 2015 Ian MacKinnon. All rights reserved.
//
@protocol CreatureProtocol <NSObject>
@required
-(void) sayHello;
@end
//
// Dog+Pooping.h
// Classroom
//
// Created by Ian MacKinnon on 2015-06-12.
// Copyright (c) 2015 Ian MacKinnon. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Dog.h"
@interface Dog(Pooping)
-(void) po_doBusiness;
@end
//
// Dog+Pooping.m
// Classroom
//
// Created by Ian MacKinnon on 2015-06-12.
// Copyright (c) 2015 Ian MacKinnon. All rights reserved.
//
#import "Dog+Pooping.h"
#import "Dog.h"
@implementation Dog(Pooping)
-(void) po_doBusiness{
NSLog(@"I'm going to make eye contact with you while I poop");
}
@end
//
// Dog.h
// Classroom
//
// Created by Ian MacKinnon on 2015-06-12.
// Copyright (c) 2015 Ian MacKinnon. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "CreatureProtocol.h"
@protocol DogOwnerProtocol <NSObject>
-(void) takeDogForWalk;
@end
@interface Dog : NSObject<CreatureProtocol>
@property (nonatomic, strong) NSString *nickname;
@property (nonatomic, assign) id<DogOwnerProtocol> delegate;
-(void) needsToWalk;
@end
//
// Dog.m
// Classroom
//
// Created by Ian MacKinnon on 2015-06-12.
// Copyright (c) 2015 Ian MacKinnon. All rights reserved.
//
#import "Dog.h"
@implementation Dog
-(void) sayHello{
NSLog(@"Woof, bow wow.");
}
-(void) needsToWalk{
[self.delegate takeDogForWalk];
}
@end
//
// Instructor.h
// Classroom
//
// Created by Ian MacKinnon on 2015-06-12.
// Copyright (c) 2015 Ian MacKinnon. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Dog.h"
@interface Instructor : NSObject<DogOwnerProtocol>
@end
//
// Instructor.m
// Classroom
//
// Created by Ian MacKinnon on 2015-06-12.
// Copyright (c) 2015 Ian MacKinnon. All rights reserved.
//
#import "Instructor.h"
@implementation Instructor
-(void)takeDogForWalk{
NSLog(@"Instuctor takes dog for walk");
}
@end
//
// main.m
// Classroom
//
// Created by Ian MacKinnon on 2015-06-12.
// Copyright (c) 2015 Ian MacKinnon. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
#import "Instructor.h"
#import "Dog.h"
#import "Dog+Pooping.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"The program has started");
NSNull *a = [[NSNull alloc] init]; //Isn' this the most insane thing ever?
NSDictionary *testDictionary = @{@"one": @1, @"two": @2, @"three":@3};
NSDictionary *subDictionary = [testDictionary valuesForKeys:@[@"one", @"four"]];
if (![testDictionary objectForKey:@"five"]){
NSLog(@"this is testing nil and will work");
}
if (![subDictionary objectForKey:@"four"]){
NSLog(@"this is testing NSNUll and will not fire");
}
if ([[subDictionary objectForKey:@"four"] isKindOfClass:[NSNull class]]){
NSLog(@"this is testing for NSNUll");
}
Student *student1 = [[Student alloc] init];
student1.firstName = @"Arsalan";
if (!student1.nickname){
NSLog(@"Student 1 has no nickname");
}
if (student1.nickname){
NSLog(@"Student 1 has nickname");
}
Dog *dog1 = [[Dog alloc] init];
dog1.nickname = @"Ella";
NSArray *officeCreatures = @[student1, dog1];
for ( id<CreatureProtocol> creature in officeCreatures){
[creature sayHello];
}
//Protocol/delegate examples
Instructor *instructor = [[Instructor alloc] init];
dog1.delegate = instructor;
[dog1 needsToWalk];
//From here, let's assume, for some bizarre reason, we can't extend Dog
[dog1 po_doBusiness];
}
return 0;
}
//
// Student.h
// Classroom
//
// Created by Ian MacKinnon on 2015-06-12.
// Copyright (c) 2015 Ian MacKinnon. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "CreatureProtocol.h"
#import "Dog.h"
@interface Student : NSObject<CreatureProtocol, DogOwnerProtocol>
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *nickname;
@end
//
// Student.m
// Classroom
//
// Created by Ian MacKinnon on 2015-06-12.
// Copyright (c) 2015 Ian MacKinnon. All rights reserved.
//
#import "Student.h"
@implementation Student
-(void) sayHello{
NSLog(@"Hello, my name is %@", _firstName);
}
-(void)takeDogForWalk{
NSLog(@"Student takes dog for walk");
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment