-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit of code from the book
- Loading branch information
1 parent
c6ccb00
commit d7ad0bf
Showing
52 changed files
with
3,532 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// Item 1 |
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
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 |
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
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]; |
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
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; |
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
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; | ||
} |
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
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; | ||
} |
Oops, something went wrong.