Stanford CS193p: Developing Applications For iOS Winter 2015
Stanford CS193p: Developing Applications For iOS Winter 2015
CS193p!
Winter 2015
Today
More Swift & the Foundation Framework
Optionals and enum
Array<T>, Dictionary<K,V>, Range<T>, et. al.
Data Structures in Swift
Methods
Properties
Initialization
AnyObject, introspection and casting (is and as)
Helpful Methods and Functions
String vs. NSString, Array vs. NSArray, Dictionary vs. NSDictionary
Property List
NSUserDefaults
CS193p!
Winter 2015
Optional
An Optional is just an enum
Conceptually it is like this (the <T> is a generic like as in Array<T>)
enum Optional<T> {
case None
case Some(T)
}
let x: String? = nil
is
let x = Optional<String>.None
let x: String? = hello
is
let x = Optional<String>.Some(hello)
var y = x!
is
switch x {
case Some(let value): y = value
case None: // raise an exception
}
CS193p!
Winter 2015
Array
Array
var a = Array<String>()
is the same as
var a = [String]()
let animals = [Giraffe, Cow, Doggie, Bird]
animals.append(Ostrich) // wont compile, animals is immutable (because of let)
let animal = animals[5] // crash (array index out of bounds)
// enumerating an Array
CS193p!
Winter 2015
Dictionary
Dictionary
var pac10teamRankings = Dictionary<String, Int>()
is the same as
var pac10teamRankings = [String:Int]()
pac10teamRankings = [Stanford:1, Cal:10]
let ranking = pac10teamRankings[Ohio State] // ranking is an Int? (would be nil)
CS193p!
Winter 2015
Range
Range
A Range in Swift is just two end points of a sensible type (not gonna explain right now)
Range is generic (e.g. Range<T>)
This is sort of a pseudo-representation of Range:
struct Range<T> {
var startIndex: T
var endIndex: T
}
Other Classes
NSObject
Base class for all Objective-C classes
Some advanced features will require you to subclass from NSObject (and it cant hurt to do so)
NSNumber
Generic number-holding class
let n = NSNumber(35.5)
let intversion = n.intValue // also doubleValue, boolValue, etc.
NSDate
Used to find out the date and time right now or to store past or future dates.
See also NSCalendar, NSDateFormatter, NSDateComponents
If you are displaying a date in your UI, there are localization ramifications, so check these out!
NSData
A bag o bits. Used to save/restore/transmit raw data throughout the iOS SDK.
CS193p!
Winter 2015
Similarities
Declaration syntax
class CalculatorBrain {
}
struct Vertex {
}
enum Op {
}
CS193p!
Winter 2015
Similarities
Declaration syntax
Properties and Functions
func doit(argument: Type) -> ReturnValue {
}
var storedProperty = <initial value> (not enum)
var computedProperty: Type {
get {}
set {}
}
CS193p!
Winter 2015
Similarities
Declaration syntax
Properties and Functions
Initializers (again, not enum)
init(argument1: Type, argument2: Type, ) {
}
CS193p!
Winter 2015
Similarities
Declaration syntax
Properties and Functions
Initializers (again, not enum)
Differences
Inheritance (class only)
Introspection and casting (class only)
Value type (struct, enum) vs. Reference type (class)
CS193p!
Winter 2015
Reference (class)
Stored in the heap and reference counted (automatically)
Constant pointers to a class (let) still can mutate by calling methods and changing properties
When passed as an argument, does not make a copy (just passing a pointer to same instance)
Methods
Obviously you can override methods/properties in your superclass
Precede your func or var with the keyword override
A method can be marked final which will prevent subclasses from being able to override
Classes can also be marked final
You declare a type method or property with a static prefix (or class in a class)
static func abs(d: Double) -> Double
CS193p!
Winter 2015
Methods
Parameters Names
All parameters to all functions have an internal name and an external name
The internal name is the name of the local variable you use inside the method
Methods
Parameters Names
All parameters to all functions have an internal name and an external name
The internal name is the name of the local variable you use inside the method
The external name is what callers will use to call the method
Methods
Parameters Names
All parameters to all functions have an internal name and an external name
The internal name is the name of the local variable you use inside the method
The external name is what callers will use to call the method
You can put _ if you dont want callers to use an external name at all for a given parameter
Methods
Parameters Names
All parameters to all functions have an internal name and an external name
The internal name is the name of the local variable you use inside the method
The external name is what callers will use to call the method
You can put _ if you dont want callers to use an external name at all for a given parameter
An _ is the default for the first parameter (only) in a method (but not for init methods)
Methods
Parameters Names
All parameters to all functions have an internal name and an external name
The internal name is the name of the local variable you use inside the method
The external name is what callers will use to call the method
You can put _ if you dont want callers to use an external name at all for a given parameter
An _ is the default for the first parameter (only) in a method (but not for init methods)
You can force the first parameters external name to be the internal name with #
Methods
Parameters Names
All parameters to all functions have an internal name and an external name
The internal name is the name of the local variable you use inside the method
The external name is what callers will use to call the method
You can put _ if you dont want callers to use an external name at all for a given parameter
An _ is the default for the first parameter (only) in a method (but not for init methods)
You can force the first parameters external name to be the internal name with #
For other (not the first) parameters, the internal name is, by default, the external name
Methods
Parameters Names
All parameters to all functions have an internal name and an external name
The internal name is the name of the local variable you use inside the method
The external name is what callers will use to call the method
You can put _ if you dont want callers to use an external name at all for a given parameter
An _ is the default for the first parameter (only) in a method (but not for init methods)
You can force the first parameters external name to be the internal name with #
For other (not the first) parameters, the internal name is, by default, the external name
Any parameters external name can be changed
func foo(first: Int, externalSecond second: Double) {
let local = internal
}
func bar() {
let result = foo(123, externalSecond: 5.5)
}
CS193p!
Winter 2015
Methods
Parameters Names
All parameters to all functions have an internal name and an external name
The internal name is the name of the local variable you use inside the method
The external name is what callers will use to call the method
You can put _ if you dont want callers to use an external name at all for a given parameter
An _ is the default for the first parameter (only) in a method (but not for init methods)
You can force the first parameters external name to be the internal name with #
For other (not the first) parameters, the internal name is, by default, the external name
Any parameters external name can be changed
Or even omitted (though this would be sort of anti-Swift)
func foo(first: Int, _ second: Double) {
let local = internal
}
func bar() {
let result = foo(123, 5.5)
}
CS193p!
Winter 2015
Properties
Property Observers
You can observe changes to any property with willSet and didSet
var someStoredProperty: Int = 42 {
willSet { newValue is the new value }
didSet { oldValue is the old value }
}
override var inheritedProperty {
willSet { newValue is the new value }
didSet { oldValue is the old value }
}
CS193p!
Winter 2015
Properties
Property Observers
You can observe changes to any property with willSet and didSet
One very common thing to do in an observer in a Controller is to update the user-interface
Lazy Initialization
A lazy property does not get initialized until someone accesses it
You can allocate an object, execute a closure, or call a method if you want
lazy var brain = CalculatorBrain() // nice if CalculatorBrain used lots of resources
lazy var someProperty: Type = {
// construct the value of someProperty here
return <the constructed value>
}()
lazy var myProperty = self.initializeMyProperty()
This still satisfies the you must initialize all of your properties rule
Unfortunately, things initialized this way cant be constants (i.e., var ok, let not okay)
This can be used to get around some initialization dependency conundrums
CS193p!
Winter 2015
Initialization
When is an init method needed?
init methods are not so common because properties can have their defaults set using =
CS193p!
Winter 2015
Initialization
What can you do inside an init?
You can set any propertys value, even those with default values
Constant properties (i.e. properties declared with let) can be set
You can call other init methods in your own class using self.init(<args>)
In a class, you can of course also call super.init(<args>)
But there are some rules for calling inits from inits in a class
CS193p!
Winter 2015
Initialization
What are you required to do inside init?
By the time any init is done, all properties must have values (optionals can have the value nil)
There are two types of inits in a class, convenience and designated (i.e. not convenience)
A designated init must (and can only) call a designated init that is in its immediate superclass
You must initialize all properties introduced by your class before calling a superclasss init
You must call a superclasss init before you assign a value to an inherited property
A convenience init must (and can only) call a designated init in its own class
A convenience init may call a designated init indirectly (through another convenience init)
A convenience init must call a designated init before it can set any property values
The calling of other inits must be complete before you can access properties or invoke methods
CS193p!
Winter 2015
Initialization
Inheriting init
If you do not implement any designated inits, youll inherit all of your superclasss designateds
If you override all of your superclasss designated inits, youll inherit all its convenience inits
If you implement no inits, youll inherit all of your superclasss inits
Any init inherited by these rules qualifies to satisfy any of the rules on the previous slide
Required init
A class can mark one or more of its init methods as required
Any subclass must implement said init methods (though they can be inherited per above rules)
CS193p!
Winter 2015
Initialization
Failable init
If an init is declared with a ? (or !) after the word init, it returns an Optional
init?(arg1: Type1, ) {
Initialization
Creating Objects
Usually you create an object by calling its initializer via the type name
let x = CalculatorBrain()
let y = ComplicatedObject(arg1: 42, arg2: hello, )
let z = [String]()
CS193p!
Winter 2015
AnyObject
Special Type (actually its a Protocol)
Used primarily for compatibility with existing Objective-C-based APIs
or as arguments to functions
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject)
func addConstraints(constraints: [AnyObject])
func appendDigit(sender: AnyObject)
AnyObject
How do we use AnyObject?
We dont usually use it directly
Instead, we convert it to another, known type
CS193p!
Winter 2015
AnyObject
Casting AnyObject
We force an AnyObject to be something else by casting it using the as keyword
Lets use var destinationViewController: AnyObject as an example
let calcVC = destinationViewController as CalculatorViewController
this would crash if dvc was not, in fact, a CalculatorViewController (or subclass thereof)
To protect against a crash, we can use if let with as?
if let calcVC = destinationViewController as? CalculatorViewController { }
CS193p!
Winter 2015
AnyObject
Casting Arrays of AnyObject
If youre dealing with an [AnyObject], you can cast the elements or the entire array
Lets use var toolbarItems: [AnyObject] as an example
for item in toolbarItems {
or
for toolbarItem in toolbarItems as [UIBarButtonItem] { // better be so, else crash!
// cant do as? here because then it might be for toolbarItem in nil (makes no sense)
CS193p!
Winter 2015
AnyObject
Another example
Remember when we wired up our Actions in our storyboard?
The default in the dialog that popped up was AnyObject.
We changed it to UIButton.
But what if we hadnt changed it to UIButton?
How would we have implemented appendDigit?
@IBAction func appendDigit(sender: AnyObject) {
if let sendingButton = sender as? UIButton {
let digit = sendingButton.currentTitle!
}
}
CS193p!
Winter 2015
AnyObject
Yet another example
It is possible to create a button in code using a UIButton type method
let button: AnyObject = UIButton.buttonWithType(UIButtonType.System)
CS193p!
Winter 2015
Casting
Casting is not just for AnyObject
You can cast with as (or check with is) any object pointer that makes sense
For example
let vc: UIViewController = CalculatorViewController()
CS193p!
Winter 2015
Functions
Some Array<T> Methods
+= [T]
first -> T?
last -> T?
// not += T
// note optional
// note optional
var a = [a,b,c]
append(T)
Functions
More Array<T> Methods
This one creates a new array with any undesirables filtered out
The function passed as the argument returns false if an element is undesirable
filter(includeElement: (T) -> Bool) -> [T]
CS193p!
Winter 2015
String
String.Index
In Unicode, a given glyph might be represented by multiple Unicode characters (accents, etc.)
As a result, you cant index a String by Int (because its a collection of characters, not glyphs)
So a lot of native Swift String functions take a String.Index to specify which glyph you want
You can get a String.Index by asking the string for its startIndex then advancing forward
You advance forward with the function (not method) advance(String.Index, Int)
var s = hello
let index = advance(s.startIndex, 2) // index is a String.Index to the 3rd glyph, l
s.splice(abc, index)
CS193p!
Winter 2015
String
String.Index
The method rangeOfString returns an Optional Range<String.Index>
As an example, to get whole number part of a string representing a double
let num = 56.25
if let decimalRange = num.rangeOfString(.) { // decimalRange is Range<String.Index>
let wholeNumberPart = num[num.startIndex..<decimalRange.startIndex]
}
(there are other (defaulted) arguments to removeRange, but Im not going to cover them now)
Theres also replaceRange(Range, String)
CS193p!
Winter 2015
String
Other String Methods
description -> String
// Printable
// ,.join([1,2,3]) = 1,2,3
componentsSeparatedByString(String) -> [String] // 1,2,3.csbs(,) = [1,2,3]
join(Array) -> String
CS193p!
Winter 2015
Type Conversion
Conversion between types with init()
A sort of hidden way to convert between types is to create a new object by converting
let d: Double = 37.5
let f: Float = 37.5
let x = Int(d)
// truncates
let xd = Double(x)
let cgf = CGFloat(d)
let a = Array(abc)
let s = String(52)
let s = \(37.5)
// no floats
// but dont forget about this mechanism
CS193p!
Winter 2015
Assertions
Debugging Aid
Intentionally crash your program if some condition is not true (and give a message)
assert(() -> Bool, message)
CS193p!
Winter 2015