William Liu

Swift Development

Setup

Make sure to download Xcode and it matches the iOS version. E.g. Xcode 9.1 matches iOS 11.1.

Apple Human Interface Guidelines: https://developer.apple.com/ios/human-interface-guidelines/overview/themes/

Apple Documentation on App Icon Dimensions and Requirements

https://developer.apple.com/library/ios/qa/qa1686/_index.html

Code Architecture

iOS uses MVC similar to web frameworks. We have:

Create New Project

Product Name: Name of App Organization Identifier: Reverse Domain Name (e.g. com.williamqliu)

Interface

Anything in a Storyboard you can modify using the visual ‘Interface Builder’ or open ‘As Source Code’ where you’ll see XML

Actions are linked with:

IB Connections

If everything is good, there’ll be a filled circle showing that the connection is Active If the circle is not filled, you’ll get an uncaught exception that a class is not key value coding compliant This might happen when you delete or change the name of an IBOutlet Make sure to break your connection by right clicking on the object and removing the IBOutlet, then recreate the connection (Ctrl and bring over your ImageView (should see a filled circle now)

UIKit

The UIKit gives you prepackaged functionality like calling arc4random_uniform() for random numbers.

Playgrounds

XCode has a playground, think a REPL for exploring around Swift and its libraries.

Side Loading

Apple limits the number of apps you can side load in a specific timeframe (think 3 per week). Can work around this by removing the app from XCode.

Swift Language

Statically typed; e.g. var myAge: Int = 33 camelCase

Strings

var myAge : Int = 32
myAge = 33
let SSN = 4343434343  // let creates constants
var myName: String = "Will"  // makes the variable a String type
print(myName)
var myDetails = "\(myName)"  // for string interpolation, use `\(myothervariable)`
var stringWithPotential = String()
var condition = "Hey" + " there"
var agreeWithMe : Bool = true
var floatingNumber : Float = 1.3
var lbsPerMonth: Double = 10.232313

String Methods and Properties

https://developer.apple.com/documentation/swift/string

Methods: There are a lot of built in methods for Strings

var eString = "Meet me in St. Louis"
for character in eString.characters {
    if character == "e" {
        print("Found an e!")
    } else {
    }
}

Properties: There are a lot of properties you can use as well

var theTruth = "Money can't buy me love"
theTruth.characters.count
theTruth.characters.reversed()

Debugging

print("Hey")

var lightSwitchOn: Bool = true
print(type(of: lightSwitchOn))

Arrays

var nounArray = ["puppy", "laptop", "ocean"]
var index1 = Int(random() % 9)

// Insert words into a sensible sentence
var sentence = "The \(nounArray[6]) spilled her \(nounArray[7])."

// Randomly choose words to create a silly sentence
var  sillySentence = "The \(nounArray[index1]) jumped over the \(nounArray[index2])."

Functions

Functions look like:

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

E.g.
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
}

Functions do not have to return anything, which looks like:

func reverseAndPrint(_ string: String) {  // notice no indication of return type
    var reversedString = ""
    for character in string.characters {
        reversedString = "\(character)" + reversedString
    }
    print(reversedString)  // notice no return
}

Three types of functions:

Type of Functions - Global Functions

Can be called anywhere (e.g. print)

Type of Functions - Methods

Methods are functions inside classes.

class Arithmetic {
    func sumOfStrings(...) {
        ...
        return sum
    }
}

var calculator = Arithmetic()
calculator.sumOfStrings(myArray)

Functions - External and Local Parameter Names

So why do you need an external parameter name (used externally) and an internal parameter name (used internally in function)? Notice that we don’t have the _ that we normally put. Here is the general form.

func functionName (externalParameterName localParameterName: parameterType) -> returnType {
    statements to execute
    return object
}

User Interface

Shift + Command + L to show Library (e.g. Labels)

Editor > Show Assistant to show code editor

Connections

Connections from your storyboard to a view controller comes in two ways: outlets and actions

An outlet connection ties an object in the storyboard (e.g. a label, text field) to a property of your view controller. An action connection allows a method of your view controller to be called when a specific event occurs (e.g. tap on a button).

Views

Views have properties; e.g. myImageView.image = UIImage(named: ) from Assets folder