CS193P
1/12/2015:
Concepts: Optionals
- Optionals are just enums
- Enumerate
- Range: a psuedo-representation of Range:
struct Range<T> {
var startIndex: T
var endIndex: T
} // note an array's range would be Range<Int>
- Special syntax for range:
let array = ["a","b","c","d","e"]
let subArray1 = array[2...3] // ["c", "d"]
let subArray1 = array[2...3] // ["c"]
for i in [27...104] { } //range is enumerable
- NSobject is the base class for all Objective-C classes (what about best practices in Swift?)
- It seems that Swift classes are inherently linked to these NSObject classes.
- NSNumber - generic number-holding class
- NSDate - used to find out date and time; be sure to check localization ramifications here...
- NSData - a "bag o' bits" used to save/restore/transmit raw data
Classes, Structures and Enumerations
- Similarities: declaration syntaxes, properties and functions, initliazers.
- Only classes have inheritance
- Introspection and casting are classes only
- Structs and Enums are passed by value but Classes are passed by reference
- Mark a function that can mutate a struct/enum with the keyword mutating
- Reference (stored in the heap). Note that you can still use
let
to mutate class properties with methods - You can override methods/properties in superclass with keyword
override
- Both types and instances can have methods/properties:
var d: Double = ...
if d.isSignMinus {
d = Double.abs(d)
}
- See above:
- here, isSignMinus is an instance property of a Double (you send it to particular Double)
- abs is a type method of a Double (sent to the type itself not a particular Double).
- You have to declare a type method/roerty with a
static
prefix or withclass
in a class:static func abs(d: Double) -> Double
- Paramters to all functions have an internal and external name
- internal name : name of local variable used inside method
- external name is what callers use to call the method
- Use
_
if no callers should use external name for given parameter
- Property Observers:
willSet
anddidSet
will be called before and after setting a property withnewValue
andoldValue
as their respective parmaters Lazy initialization: initialized only when someone acceses it
- can be used when there are cyclized dependencies between parent and child classes
When is
init
method needed?- You can set any property's value ...few asleep for 10 minutes
- Casting works with the
as
keyword.
1/12/2015:
Continuation of Calculator Demo - Assignment #2 will be based on the demo today
Concepts: Operand/operator stack
- Conventions: camelcase convention for names of files (e.g.
CalculatorBrain.swift
) - We're defining an Op object to refer to an operand or an operator: should it be a class or function or an enum
- We can create an empty array of objects with:
var opStack = Array<Op>()
, but the preferred syntax isvar opStack = [Op]()
- Functions are just types in Swift (no different from strings)
- Creating an enum (used to enumerate different possibilities): In swift, we can associate data with any of the cases in this enum
enum Op {
case Operand(Double)
case UnaryOperation(String, Double -> Double)
case BinaryOperation(String, (Double, Double) -> Double)
}
- An initializer is called by invoking a class with parentheses: (e.g.
var knownOps = [String:Op]()
). - We can also intiialize it with the
init()
method - Looking up something in a dictionary will always return an optional
- To make variables private to a class, add
private
- Sometimes you can't return an actual return value, you can make it an optional return value:
func evaluate() -> Double? {
}
- Swift passes structs by value and clases by references: Arrays, Dictionaries are structs
- There's an implicit
let
in from of all structs as parameters (read-only!) so the struct in this case is immutable - Hacky way of making a mutable copy for use inside a function (see the var that replaces the implicit let in the parameter):
func evaluate(var ops: [Op]) {
}
- Swift is actually smart enough to not actually copy a struct until a change is made. Swift might even make just a partial copy or just kept track of the actual changes made.
- We use
switch
to pull associated values out of enums - Underbars
_
are used when we don't care about the variable and don't intend on using it
1/7/2015:
Continuation of Calculator Demo
Concepts: Typed Functions, Arrays, Sets
Review
- Optionals are automatically assigned to be nil (not set)
- Note it's not necessary to declare the type of a variable:
var operandStack: Array<Double> = Array<Double>()
is equivalent tovar operandStack = Array<Double>()
- Creating arrays are just like:
- Computed properties : you can define properties that are computed and updated anywhere it is used
- Switch statement is much more powerful in Swift. You have to cover every possibility (make sure to include a Default case)
- Closures allow you to define functions as parameters:
performOperation({(op1,op2) in op1 * op2)
is equivalent toperformOperation {$0 * $1}
MVC - 3 camps
- Model - what your application (i.e. what is a program?)
- The model is completely UI independent, should never talk to the view
- Controller - how your model is presented to the user
- Controllers can always talk directly to their Model
- Controllers can also talk directly to the view (outlet)
- View - what the user sees
- Comunicates with the contorller through actions
- Sometimes the view needs to be synchronized with the controller (i.e. should I allow the user to scroll?
shouldScroll
willScroll
didScroll
- we solve this with delegations
- delegate is set via a protocol ("blind" to class)
- controllers provide a data source to the view
1/5/2015: Introduction to Swift
Introduction to the course and the start of the calculator demo to build basic intuition about programming in Swift
Concepts: Creating a project, Tying/connecting button components to their corresponding methods in the controller, Swift basics, Types in Swift (Optional types)
Logistics:
- Each class will feature a Demo
- Enrollment survey deadline is Tuesday
- Paul will likely have a "second start" to the class for the iTunesU taping (2 week delay)
Overview + Prerequisites:
- Real life object-oriented programming
- Databases, graphics, multimedia, animation in a real-world application
- Prior coursework assumes object-oriented programming
Four Layers
- Core OS:
- UNIX-based system
- Sockets, Kernel, Permissions, near hardware level
- Core Services
- Accessing lower level services through object-oriented interfaces
- Media (little time spent in this area)
- Capturing, editing media
- Audio mixing, recording
- Cocoa Touch (Vast majority of time spent here, 70%+)
- Buttons, sliders, multi-touch
- Build the interactivity with the application here
Platform Components:
- Xcode 6 is the code editor, compiler, debugger
- Language(s): We'll learn Swift in this class
- Frameworks - core motion, map kit, gyroscope
- Design Strategy - MVC
Demo:
- We're building a calculator from scratch
- Topics include:
- Creating a project in Xcode
- Building a UI
- iOS Simulator
- println
- defining a class in Swift
- Connecting method interactions to their components (CTRL + Drag method dots to their corresponding components in the view)