Control Flow

-(Move)generateMove {
	NSUInteger randomNumber = arc4random_uniform(3);

	swtich(randomNumber){
		case 0:
			return Rock;
			break;
		case 1:
			return Paper;
			break;
		case 3:
			return Scissors;
			break;
		default:
			return Invalid;
			break;
	}

	// placeholder
	return Rock;
}
-(BOOL)defeats:(RPSTurn *)opponent {
	if ((self.move == Paper && opponent.move == Rock) ||
		(self.move == Scissors && opponent.move == Papper) ||
		(self.move == Rock && opponent.move == Scissors))
	{
		return true;
	} else {
		return false;
	}
}

Present Video Camera

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSMutableArray *gifs;
@end

#import "UIViewController+Record.h"
#import "GifEditorViewController.h"

#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>

#import "GifMaker_Objc-Swift.h"

@implementation UIViewController (Record)

static int constt kFrameCount = 16;
static const float kDelayTime = 0.2;
static const int kLoopCount = 0;

- (IBAction)presentVideoOptions:(id)sender {
	if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
	{
		[self launchPhotoLibrary];
	} else {
		UIAlertController *newGifActionSheet = [UIAlertController
			alertControllerWithTitle:@"Create new GIF"]
	}
}

@implementation UIViewController (Record)

static int const kFrameCount = 16;
static const float kDelayTime = 0.2;
static const int kLoopCount = 0;

- (IBAction)presentVideoOptions:(id)sender{
	if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
	{
		[self launchPhotoLibrary];
	} else {
		UIAlertController *newGifActionSheet = [UIAlertController
			alertControllerWithTitle:@"Create new GIF"
				message:nil
				preferredStyle:
				UIAlertControllerStyleActionSheet];

		UIAlertAction *recordVideo = [UIAlertAction actionWithTitle:@"Record a Video"
			style:UIAlertActionStyleDefault
			handler:^(UIAlertAction * action){
				[self launchCamera];
			}];
}

Method Definition Syntax

#import "RPSTurn.h"

@implementation RPSTurn

-(instancetype)initWithMove:(Move) move{
	self = [super init];
	if (self){
		_move = move;
	}
	return self;
}
#import "RPSGame.h"

@implementation RPSGame

-(instancetype)initWithFirstTurn:(RPSTurn*) playerTurn
	secondTurn: (RPSTurn*)computerTurn {
		self = [super init];

		if(self){
			_firstTurn = playerTurn;
			_secondTurn = computerTurn;
		}
		return self;
	}

The Method throwDown

#import "RPSController.h"
#import "RPSTurn.h"

@implementation RPSController

-(void)throwDown:(Move) playersMove {
	
	// Here the RPSTurn class generates the opponent's move
	RPSTurn *playerTurn = [[RPSTurn alloc]initWithMove:playersMove];
}

Book Class

@interface Book : NSObject

@property (nonatomic) NSString *title;
@property (nonatomic) NSString *author;
@property (nonatomic) int yearOfPublication;

-(instancetype)initWithTitle:(NSString*)title
	author:(NSString*)author
		year:(init)year;

@end

// Book.m
@implementation Book

-(instancetype)initWithTitle:(NSString*)title
			author:(NSString*)author
			year:(int)year {

			self = [super init];
			if(self){
				_title = title;
				_author = author;
				_yearOfPublication = year;
			}
			return self;
}

@end

Weak references for:
1. delegates
2. subviews of the main view

Person.h

@interface Person : NSObject

@property (nonatomic) NSString *name;
@property (nonatomic) NSDate *birthday;

@end

Person.m

// Person.m

@implementation Person

-(instancetype)initWithName:(NSString*)name birthday:(NSDate*)birthday {
	self = [super init];

	if(self){
		_name = name;
		_birthday = birthday;
	}
	return self;
}
@end

Book.h

#import "Person.h"

@interface Book : NSObject

@property (nonatomic) NSString *title;
@property (nonatomic) Person *author;
@property (nonatomic) int yearOfPublication;

-(instancetype)initWithTitle:(NSString*)title
	author:(Person*)author
		year:(int)year;

@end

Custom Initializer

-(instancetype)initWithAddress: (NSMutableString*)address {
	self = [super init];

	if(self){
		_address = [address copy];
		_numberOfBedrooms = 3;
		_hasHotTub = false;	
	}
	return self;
}

Class initialization

#import "House.h"

@interface House()
@property (nonatomic, readwrite) int numberOfBedrooms;
@end

@implementation House

-(instancetype)initWithAddress:(NSString*)address {
	self = [super init];

	if(self){
		_address = [address copy];
		_numberOfBedrooms = 2;
		_hasHotTub = false;
	}
	return self;
}
@end

House Object Diagram

House
*address
int numberOfBedrooms
bool hasHotTub

-> address

#import <Foundation/Foundation.h>

@interface House : NSObject
@property (nonatomic) NSString *address
@property (nonatomic) int numberOfBetrooms;
@property (nonatomic) BOOL hasHotTub;

@end
#import <Foundation/Foundation.h>
#import "House.h"

#import <Foundation/Foundation.h>
#import "House.h"

int main(int argc, const char * argv[]){
	@autoreleasepool {
		House *myHouse = [[House alloc] init];

	}
	return 0;
}

Dynamic Method Resolution

#import <Foundation/Foundation.h>
#import "Puppy.h"
#import "Dealer.h"
#import "Drum.h"

int main(int argc, const char * argv[]){
	@autoreleasepool {

		Puppy *spot = [[Puppy alloc] init];
		Drum *drum = [[Drum alloc] init];
		Dealer *cardDealer = [[Dealer alloc] init];

		NSArray *array = @[spot, drum, cardDealer];
		NSUInteger index = arc4random_uniform(3);
		id item = [array objectAtIndex:index];
		[item play];
	}
	return 0;
}

NSMutableArray *attendance = [NSMutableArray arrayWithArray: @[@”Diego”]];
[attendance addObject:@”Rahul”]

NSArray *invited = @[@”Diego”, @”Rahul”, @”Elana”]

Differences:Handling Nil

Objective-C: Any object can be nil
Swift: Optionals can be nil

#import <Foundation/Foundation.h>
#import "Messengaer.h"
#import "Package.h"

int main(int argc, const char * argv[]){
	@autoreleasepool {

		Messenger +spike = [[Messenger alloc] init];
		Package *importantDocuments = [spike pickUpPackage];

		NSLog(@"package contents: %@", importantDocuments.contents);
	}
	return 0;
}

Objective-C and Swift approaches to mutability differ
– how mutability is achieved for a given type
– the probability of an unintended mutation propagating through a program

#import <Foundation/Foundation.h>
#import "Messengaer.h"
#import "Package.h"

int main(int argc, const char * argv[]){
	@autoreleasepool {

		NSArray *kayakingGear = [NSMutableArray arrayWithArray:@[@"halmet",@"paddle",@"boat",
			@"lifejacket"]];
			[kayakingGear addObject:@"booties"];

			NSLog(@"%@", kayakingGear);
	}
	return 0;
}

Design pattern

Protocols Enable Shared Functionality Across Classes
pets, dogs, birds, flight, bats, insects

UITextFieldDelegateProtocol in Swift

func textField(textField: UITextField,
	shouldChangeCharactersInRange range:NSRange,
		replacementString string: String) -> Bool {
	
		var newText = textField.text! as NSString
		newText =
		newText.stringByReplacingCharactersInRange(range,
			withString: string)

		return newText.length <= 5
}

UITextFieldDelegateProtocol in Objective-C

-(BOOL)textField:(UITextField *)textField
	shouldChangeCharactersInRange:(NSRange)range
		replacementString:(NSString *)string{

		NSString *nexText = textField.text;
		newText = [newText
		stringByReplacingCharactersInRange:range
			withString:string];

		return (newText.length <=5);
}

Objective-C

Objective-C: Any object can be nil, dynamic typing
Swift: Optionals can be nil, limited mutability
iOS Frameworks Design-Patterns: static typing

// Swift
override func viewWillAppear(animated: Bool){
	super.viewWillAppear(animated)
	displayResult()
}

// Objective-C
- (void)viewWillAppear:(BOOL) animated {
	[super viewWillAppear:animated];
	[self displayResult];
}

AppDelegate Methods

func application(application: UIApplication, didFinishLaunchingWithOptions
	launchOptions: [NSObject: AnyObject]?) -> Bool {
	return true
}
func applicationWillResignActive(application: UIApplication){
}
func applicationDidEnterBackground(application: UIApplication){
}
func applicationWillEnterForeground(application: UIApplication){
}
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
	(NSDictionary *)launchOptions {
	return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application{
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
}