Skip to content

Commit

Permalink
Initial commit of code from the book
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjgalloway committed May 21, 2013
1 parent c6ccb00 commit d7ad0bf
Show file tree
Hide file tree
Showing 52 changed files with 3,532 additions and 0 deletions.
1 change: 1 addition & 0 deletions chapter_1/item_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Item 1
60 changes: 60 additions & 0 deletions chapter_1/item_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Item 2

// Simple EOCPerson class
// EOCPerson.h
#import <Foundation/Foundation.h>

@interface EOCPerson : NSObject
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;
@end

// EOCPerson.m
#import "EOCPerson.h"

@implementation EOCPerson
// Implementation of methods
@end


// Requiring an import
// EOCPerson.h
#import <Foundation/Foundation.h>

@interface EOCPerson : NSObject
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;
@property (nonatomic, strong) EOCEmployer *employer;
@end


// Forward declaring
// EOCPerson.h
#import <Foundation/Foundation.h>

@class EOCEmployer;

@interface EOCPerson : NSObject
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;
@property (nonatomic, strong) EOCEmployer *employer;
@end

// EOCPerson.m
#import "EOCPerson.h"
#import "EOCEmployer.h"

@implementation EOCPerson
// Implementation of methods
@end


// Importing a header with a protocol in it
// EOCRectangle.h
#import "EOCShape.h"
#import "EOCDrawable.h"

@interface EOCRectangle : EOCShape <EOCDrawable>
@property (nonatomic, assign) float width;
@property (nonatomic, assign) float height;
@end
50 changes: 50 additions & 0 deletions chapter_1/item_3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Item 3

NSString *someString = @"Effective Objective-C 2.0";

NSNumber *someNumber = [NSNumber numberWithInt:1];
NSNumber *someNumber = @1;

NSNumber *intNumber = @1;
NSNumber *floatNumber = @2.5f;
NSNumber *doubleNumber = @3.14159;
NSNumber *boolNumber = @YES;
NSNumber *charNumber = @‘a’;

int x = 5;
float y = 6.32f;
NSNumber *expressionNumber = @(x * y);

NSArray *animals = [NSArray arrayWithObjects:@"cat", @"dog", @"mouse", @"badger", nil];
NSArray *animals = @[@"cat", @"dog", @"mouse", @"badger"];

NSString *dog = [animals objectAtIndex:1];
NSString *dog = animals[1];

id object1 = /* … /*;
id object2 = /* … /*;
id object3 = /* … /*;

NSArray *arrayA = [NSArray arrayWithObjects:object1, object2, object3, nil];
NSArray *arrayB = @[object1, object2, object3];

NSDictionary *personData =
[NSDictionary dictionaryWithObjectsAndKeys:
@"Matt", @"firstName",
@"Galloway", @"lastName",
[NSNumber numberWithInt:28], @"age",
nil];
NSDictionary *personData =
@{@"firstName" : @"Matt",
@"lastName" : @"Galloway",
@"age" : @28};

NSString *lastName = [personData objectForKey:@"lastName"];
NSString *lastName = personData[@"lastName"];

[mutableArray replaceObjectAtIndex:1 withObject:@"dog"];
[mutableDictionary setObject:@"Galloway" forKey:@"lastName"];
mutableArray[1] = @"dog";
mutableDictionary[@"lastName"] = @"Galloway";

NSMutableArray *mutable = [@[@1, @2, @3, @4, @5] mutableCopy];
65 changes: 65 additions & 0 deletions chapter_1/item_4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Item 4

// EOCAnimatedView.h
#import <UIKit/UIKit.h>

@interface EOCAnimatedView : UIView
- (void)animate;
@end

// EOCAnimatedView.m
#import "EOCAnimatedView.h"

static const NSTimeInterval kAnimationDuration = 0.3;

@implementation EOCAnimatedView
- (void)animate {
[UIView animateWithDuration:kAnimationDuration
animations:^(){
// Perform animations
}];
}
@end


// In the header file
extern NSString *const StringConstant;

// In the implementation file
NSString *const StringConstant = @"VALUE";


// EOCLoginManager.h
#import <Foundation/Foundation.h>

extern NSString *const EOCLoginManagerDidLoginNotification;

@interface EOCLoginManager : NSObject
- (void)login;
@end

// EOCLoginManager.m
#import "EOCLoginManager.h"

NSString *const EOCLoginManagerDidLoginNotification = @"EOCLoginManagerDidLoginNotification";

@implementation EOCLoginManager

- (void)login {
// Perform login asynchronously, then call `p_didLogin`.
}

- (void)p_didLogin {
[[NSNotificationCenter defaultCenter]
postNotificationName:EOCLoginManagerDidLoginNotification
object:nil];
}

@end


// EOCAnimatedView.h
extern const NSTimeInterval EOCAnimatedViewAnimationDuration;

// EOCAnimatedView.m
const NSTimeInterval EOCAnimatedViewAnimationDuration = 0.3;
86 changes: 86 additions & 0 deletions chapter_1/item_5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Item 5

enum EOCConnectionState {
EOCConnectionStateDisconnected,
EOCConnectionStateConnecting,
EOCConnectionStateConnected,
};

enum EOCConnectionState state = EOCConnectionStateNotConnected;

enum EOCConnectionState {
EOCConnectionStateDisconnected,
EOCConnectionStateConnecting,
EOCConnectionStateConnected,
};
typedef enum EOCConnectionState EOCConnectionState;

EOCConnectionState state = EOCConnectionStateNotConnected;

enum EOCConnectionStateConnectionState : NSInteger { … };

enum EOCConnectionStateConnectionState : NSInteger;

enum EOCConnectionStateConnectionState {
EOCConnectionStateDisconnected = 1,
EOCConnectionStateConnecting,
EOCConnectionStateConnected,
};

enum EOCMethodOptions {
EOCMethodOptionOne = 1 << 0,
EOCMethodOptionTwo = 1 << 1,
EOCMethodOptionThree = 1 << 2,
};

enum EOCMethodOptions options = EOCMethodOptionOne | EOCMethodOptionThree;
if (options & OptionOne) {
// OptionOne is set
}

typedef NS_ENUM(NSUInteger, EOCConnectionState) {
EOCConnectionStateDisconnected,
EOCConnectionStateConnecting,
EOCConnectionStateConnected,
};
typedef NS_OPTIONS(NSUInteger, EOCMethodOptions) {
EOCMethodOptionOne = 1 << 0,
EOCMethodOptionTwo = 1 << 1,
EOCMethodOptionThree = 1 << 2,
};

typedef enum EOCConnectionState : NSUInteger EOCConnectionState;
enum EOCConnectionState : NSUInteger {
EOCConnectionStateDisconnected,
EOCConnectionStateConnecting,
EOCConnectionStateConnected,
};

typedef enum EOCMethodOptions : int EOCMethodOptions;
enum EOCMethodOptions : int {
EOCMethodOptionOne = 1 << 0,
EOCMethodOptionTwo = 1 << 1,
EOCMethodOptionThree = 1 << 2,
};

EOCMethodOptions options = EOCMethodOptionOne | EOCMethodOptionTwo;

typedef NS_ENUM(NSUInteger, EOCConnectionState) {
EOCConnectionStateDisconnected,
EOCConnectionStateConnecting,
EOCConnectionStateConnected,
};

// …

switch (_currentState) {
EOCConnectionStateDisconnected:
// Handle disconnected state
break;
EOCConnectionStateConnecting:
// Handle connecting state
break;
EOCConnectionStateConnected:
// Handle connected state
break;
}
42 changes: 42 additions & 0 deletions chapter_2/item_10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Item 10

// C early binding
#import <stdio.h>

void printHello() {
printf("Hello, world!\n");
}
void printGoodbye() {
printf("Goodbye, world!\n");
}

void doTheThing(int type) {
if (type == 0) {
printHello();
} else {
printGoodbye();
}
return 0;
}


// C late binding
#import <stdio.h>

void printHello() {
printf("Hello, world!\n");
}
void printGoodbye() {
printf("Goodbye, world!\n");
}

void doTheThing(int type) {
void (*fnc)();
if (type == 0) {
fnc = printHello;
} else {
fnc = printGoodbye;
}
fnc();
return 0;
}
Loading

0 comments on commit d7ad0bf

Please sign in to comment.