Delegation

AVAudioRecorder
Record VC

func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool){
	if flag {
		performSegue(withIdentifier: "stopRecording", sender: audioRecorder.url)
	} else {
		print("recording was not successful")
	}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
	if segue.identifier == "stopRecording"{
		let laySoundsVC = segue.destination as! PlaySoundsViewController
		let recordedAudioURL = sender as! url
		playSoundVC.recordedAudioURL = recordedAudioURL
	}
}

Stack Views: Horizontal, Vertical
-alignment, distribution, spacing

AVAudioEngine

var dollars:Int = 10
var cents:Int = 50
var amount = "$\(dollars).\(cents)"
print(amount)

dollars = 19
centsd = 99
amount = "$\(dollars).\(cents)"
print(amout)

func printMoneyString(dollars:Int, cents:Int){
	print("$\(dollars).\(cents)")
}

Tour of Xcode

Document Outline in Xcode

Model View Controller
view(UIView, UILabel, UIButton), controller, model

AutoLayout Basics

@IBOutlet weak var recordButton: UIButton!
@IBOutlet weak var recordingLabel: UILabel!
@IBOutlet weak var resumeButton: UIButton!
@IBOutlet weak var resumeLabel: UILabel!
@IBOutlet weak var pauseButton: UIButton!
@IBOutlet weak var pauseLabel: UILabel!
@IBOutlet weak var stopButton: UIButton!
@IBOutlet weak var stopLabel: UILabel!

@IBOutlet func recordButtonPressed(sender: UIButton){
	// ...
}

ViewController and Multiple Views
Not Running, Inactive, Active, Background, Suspended

Audio in iOS
Pitch Perfect App, AVFoundation, Core Audio, Audio Hardware

import AVFoundation

class ViewController: UIViewController {
	var audioRecorder: AVAudioRecorder!

	@IBAction func recordAudio(_ sender: AnyObject){
		recordingLabel.text = "Recording in progress"
		stopRecordingButton.isEnabled = true
		recordButton.isEnabled = false

		let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true)[0] as String
		let recordingName = "recordedVoice.wav"
		let pathArray = [dirPath, recordingName]
		let filePath = URL(string: pathArray.joined(separator: "/"))

		let session = AVAudioSession.sharedInstance()
		try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, with:AVAudioSessionCategoryOptions.defaultToSpeaker)

		try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
		audioRecorder.isMeteringEnabled = true
		audioRecorder.prepareToRecord()
		audioRecorder.record()
	}
}

Introduction X-code

import UIKit

var x: Int = 42
var x = 42
let y = 100

var myString = "Hello"

// Control Flow
if x < 50 {
	print("X is less than 50")
} else {
	print("X is greater than or equal to 50")
}

// Classes
class ViewController: UIViewController {
	// Instance Variables go here
	// Class functions go here
}

// Functions
func printHello(){
	print("Hello")
}

printHello()

func printHelloMessage(helloString: String){
	print(helloString)
}

printHelloMessage("Oi!")

Closures

Closures include:
global functions, nested functions, and closure expressions

Nested functions
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-ID178

closure expression- an unnamed, self contained block of code
closure expression are used to specify an action to be executed some time in the future.

var bids = [48, 75, 63, 52, 68]
var orderedBids = bids.sort( {(bid1: Int, bid2: Int) -> Bool in
	return bid2 > bid1
})

print(orderedBids)

Closures typically take the form:
{(parameters) -> return type in
statements to execute
}

var birthYears = [2004, 2011, 2007, 2005, 2002]
var reverseChronologicalYears = birthYears.sort({ (year1: Int, year2: Int) -> Bool interface EarthquakeActivity extends Parent {
	return year1 > year2
}
})

Protocols and Extensions

Protocols

Adopting a protocol is like signing a contract to implement every method on the list.

import UIKit

protocol Souschef {
	func chop(vegetable: String) -> String
	func rinse(vegetable: String) -> String
}

class Roommate: Souschef {
	var hungry = true
	var name: String

	init(hungry:Bool, name: String){
		self.hungry = hungry
		self.name = name
	}

	func chop(vegetable: String) -> String {
		return "She's choppin' \(vegetable)!"
	}

	func rinse(vegetable: String) -> String {
		return "The \(vegetable) is so fresh and so clean"
	}
}
class DinnerCrew {
	var members: [Souschef]

	init(members: [Souschef]){
		self.members = members
	}
}
protocol DirtyDeeds {
	func cheat()
	func steal()
}

class Minion: DirtyDeeds {
	var name: String

	init(name:String){
		self.name = name
	}

	func cheat(){
		println("Mwa haha!")
	}

	func steal(){
		println("Mwa haha!")
	}
}

class DinnerCrew {
	var members: [Souschef]

	init(members: [Souschef]){
		self.members = members
	}
}

protocol Souschef {
	func chop(vegetable: String) -> String
	func rinse(vegetable: String) -> String
}

var deviousDinnerCrew = DinnerCrew(members: [Minion]())

Andrew Bancroft’s Swift blog
http://www.andrewcbancroft.com/

Enums and Structs

Enums and Structs
{
case red
case blue
case green
}

import UIKit

enum PrimaryColor {
	case red
	case blue
	case yellow
}
enum Aunties {
	case Aime, Billie, Diane, Gail, Janie, Pam
}
enum AmericanLeagueWest: String {
	case As = "Oakland"
	case Astros = "Houston"
	case Angels = "Los Angeles"
	case Mariners = "Seatle"
	case Rangers = "Arlington"
}
enum CaliforniaPark {
	case Yosemite, DeathValley, Lesson, Sequoia
}

var warning = ""
var destination = CaliforniaPark.Yosemite

switch destination {
	case.Yosemite:
		warning = "Beware of agressive bears!"
	case.DeathValley:
		warning = "Beware of dehydration!"
	case.Lasson:
		warning = "Watch out for boiling pools!"
}
struct PictureFrame {
	var width = 5
	var height = 7
	var thickness: Double = 1.5

	var area: Int {
		get {
			return width * height
		}
	}
}
var frame = PictureFrame(width: 3, height: 5, thickness: 0.5)
var frameForMom = frame
frameForMom.width = 5
frameForMom.height = 7
frame.width
frame.height

class ClassyPictureFrame {
	var width = 5
	var height = 7
	var thickness: Double = 1.5

	var area: Int {
		get {
			return (width * height) / 2
		}
	}

	init(width: Int, height: Int, thickness: Double){
		self.width = width
		self.height = height
		self.thickness = thickness
	}
}

var classyFrame = ClassyPictureFrame(width: 3, height: 5, thickness: 0.5)
var classyFrameForMom = classyFrame
var darkColors = Laundry(temperature: 70, speed: "medium")
var darkDelicates = darkColors
darkDelicates.speed = "low"
darkColors.speed
 Triangle {
 	let angles = [30, 60, 90]
 	let sides = [3,4,5]
 }

 UIImagePickerControllerSourceType : Int {
 	case PhotoLibrary
 	case Camera
 	case SavedPhotosAlbum
 }

 struck Name {
 	var firstName: String
 	var lastName: String
 }

 enum Subject {
 	case Math
 	case English
 	case Spanish
 	case Science
 }

Classes, Properties, and Methods

import UIKit

class Movie {
	let title: String
	let director: String
	let releaseYear: Int

	init(title: String, director: String, releaseYear: Int){
		self.title = title
		self.director = director
		self.releaseYear = releaseYear
	}
}
class MovieArchive {
	var movies:[Movie]

	func filterByYear(year:Int) -> [Movie]{
		var filteredArray = [Movie]()
		for movie in self.movies {
			if movie.releaseYear == year {
				filteredArray.append(movie)
			}
		}
		return filteredArray
	}

	init(movies:[Movie]){
		self.movies = movies
	}
}
import UIKit

class ViewController: UIViewController {

	var count = 0
	var label:UILabel!

	override func viewDidLoad(){
		super.viewDidLoad()

		// Label
		var label = UILabel()
		label.frame = CGRectMake(150, 150, 60, 60)
		label.text = "0"
		self.view.addSubview(label)
		self.label = label
		// Button
		var button = UIButton()
		button.addTarget(self, action: "incrementCount", forControlEvents: UIControlEvents.TouchUpInside)
		self.view.addSubview(button)
		button.frame = CGRectMake(150, 250, 60, 60)
		button.setTitle("Click", forState: .Normal)
		button.setTitleColor(UIColor.blueColor(), forState:.Normal)
	}

	@IBAction func incrementCount(){
		self.count++
		self.label.text = "\(self.count)"
	}
}

Access Control
Level, Class, App/Framework, Work
public: Y Y Y
internal: Y Y N
private Y N N

class Movie {
	let title: String
	let director: String
	let releaseYear: Int

	init(title: String, director: String, releaseYear: Int){
		self.title = title
		self.director = director
		self.releaseYear = releaseYear
	}
}

var thursdayNightMovie = Movie(title: "Point Break", director: "Kathryn Bigelow", releaseYear: 1991)
thursdayNightMovie.title

Functions

Global Functions
the functions print, min, and abs are a few examples of global functions.

print("I'm a global function!", terminator:"")

var initialPrice = 50
var bestOffer = 45
var finalPrice = min(bestOffer, initialPrice)

var negativeSeven = -7
abs(negativeSeven)

Anatomy of a function

let array = ["A", "13", "B", "5", "87", "t", "41"]

class Arithmetic {
	func sumOfStrings(aBunchOfStrings: [String]) -> Int {
		let array = aBunchOfStrings
		var sum = 0
		for string in array {
			if Int(string) != nil {
				let intToAdd = Int(string)!
				sum += intToAdd
			}
		}
		return sum
	}
}

func functionName (parameterName: parameterType) -> returnType
statements to execute
return object
}

let stringToReverse = "Mutable or Immutable? That is the question."

func reverseString(stringToReverse: String){
	var reversedString = ""
	for character in stringToReverse.characters {
		reversedString = "\(character)" + reversedString
	}
	print(reversedString)
}

func functionName(externalParamName localParamName: paramType) -> returnType {
statements to execute
return object
}

func firstCharaterOf(word: String) -> Character {
	return word[word.startIndex]
}

firstCharacterOf("Mom")
func placeFirstLetterLast(var myString: String) -> String {
	myString.append(firstCharacterOf(word: myString))
	myString.removeAtIndex(myString.startIndex)
	return "placeholder"
}

placeFirstLetterLast("Mom")
class MovieArchive {
	func filterByYear(year: Int, movies: Dictionary<String, Int> -> [String]{
		var filteredArray = [String]()
		for (movie, releaseYear) in movies {
			if year == releaseYear{
				filteredArray.append(movie)
			}
		}
		return filteredArray
	})
}

var aiThemedMovies = ["Metropolis":1927, "2001:A Space Odessey":1968, "Blade Runner":1982, "War Games":1983, "Terminator":1984, "The Matrix":1999, "A.I.":2001, "Her":2013, "Ex Machina":2015]

var myArchive = MovieArchive()
myArchive.filterByYear(2013, movies: aiThemedMovies)

Switch statement

var birthYear = 1992

if birthYear == 1992 || birthYear == 1980 || birthYear == 1968 {
	println("You were born in the year of the monkey.")
} else if birthYear == 1991 || birthYear == 1979 || birthYear == 1967 {
	println("You were born in the year of the goat")
} else {
	println("You were born in some other animal's year.")
}

switch birthYear {
	case 1992, 1980, 1968:
	println("You were born in the year of the monkey.")
	case 1991, 1979, 1967:
	println("You were born in the year of the goat")
	default:
	println("You were born in some other animal's year.")
}