Created
June 12, 2015 15:03
-
-
Save imack/65f78c75bf722e9d9f01 to your computer and use it in GitHub Desktop.
Protocols, delegates, and Categories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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