- introduction to iOS environment
- initial files
- hello world
- function
- a calculator
- variable
- optional: Type
iOS system layers
- Cocoa Touch: UI/Objects: multi-touch, motion, view hierarchy, web view, camera;
- Media: OpenAL, Core Audio, PDF;
- Core Services: Core Services: collections, networking, file access;
- Core OS (bottom): Unix;
platform
- Tools: Xcode
- Language: 1. swift 2. obj-c
initial files
- assets.xcassets: Media files: icon, sounds, images
- ViewController.swift: Controller in MVC
- Main.storyboard: View in MVC, edit it just with mouse
ViewController.swift
import UIKitimport a module, like Java;Foundationis another important moduleclass ViewController: UIViewController:: UIViewControllerinherits formUIViewController; all MVC controllers inherits fromUIViewController, either directly or indirectly;
Main.storyboard
- Components: from

Hello world!
Clear the ViewController.swift, like this:
import UIKit
class ViewController: UIViewController {}
Add a button: In the view, components panel, drug a button to the “screen”
Show the assistant editor on the right: Click on

Add an action: holding down the control, drug the button to the code

Function editing: the connection should be action, the arguments should be sender(the button being touched itself); change the Type to UIButton, the sender is expected to be an UIButton; event for touch up inside means “after push down and loosen it”, simply a click.
First Function
@IBAction func touchDigit(_ sender: UIButton) {}
@IBAction: label, not a part of function;func: this is the function on a class;funccan be add outside of the{}(a class), to be a Globe Function;touchDigitis the function’s name;()is the parameters;_is used to define that the parameter has no external name (more on stackoverflow );senderis the para name,UIButtonis its type (use:to define its type);
Add a return. it can be like this:
@IBAction func touchDigit(_ sender: UIButton) -> Double{…}
add a -> Double;
Add another argument. like this:
(_ sender: UIButton, anotherPara: Int)
the Type here is a Integer.
Call this function
Object.touchDigit(someButton, anotherPara: 5)
Do clarify the name of the parameters! Like anotherPara: 5. the anotherPara: should not be ignored; but the first argument must be ignored. (see below)
Diff between (sender: UIButton) and (_ sender: UIButton):
- 1st MUST be called like
.funcName(sender: self, anotherPara: 5); - 2nd with
_MUST be called like.funcName((self, anotherPara: 5)
Call self: self.function(para, para), selfis THIS object.
Add statement:
@IBAction func touchDigit(_ sender: UIButton) {
print("hello world!")
}
do not need ; at the end of the line. If need two statements on the same line, you should add a ;;
Run!

A Calculator
Add a var: var digit = 1; var and let can define a variable.
var
Diff var & let:
varis a variable;letdefine a READ-ONLY variable- is clear to read: it’s a constant;
- if use the Array or Dictionary,
letmeans nothing can be taken out or into, it’s READ-ONLY.
Reference for Objects or Functions: press option and click.
get UIButton’s title: UIButton.currentTitle. it’s a property, can only be get;
The current title that is displayed on the button.
var currentTitle: String? { get }
Print something: print("touched \(digit) digit”). use \(variable_name_here) to as a replacer.
- when run the statement
print("touched \(digit) digit”), we get this output:touched Optional("5") digit. So what isOptional()?
we MUST always initialize a variable with a initial value. otherwise, we will get a ERROR. Except for Optional, it has a default value: “not set”, aka nil.
we can init a variable by:
var a: bool = false
// var a = false
2nd line is good enough cause false can only be a bool:

optional: Type
Back to the API doc:
var currentTitle: String? { get }
we get a ? after String. String? is a Type, like int or bool.
An optional has two values:
- Not set. in Swift, it’s
nil. means thisoptionalis not set.
- Set. it has an “associate value”, can be any Type, like
intordouble.
String? means this is an optional, whose “associate value” is a String; we call it “optional string”.
we let var = var: String?, it will be also a optional String:

Why we need an optional?
A button can have no title. When we get its title, it print like this:
touched Optional("3") digit
touched nil digit
it might have no value, aka a nil.
Why not a 0? If the variable has no value, create a ””(empty string) for a String) or a 0 for a int. But it’s not cover all the cases. “empty string” is different between “not set”. We can set a String to ””, we can not judge a String from “empty” from “either set”.
But we do not need a String?: Optional(“3”), we need a ”3”. We can simply put:
let digit = sender.currentTitle!
we add a ! after a ? Type, aka the optional Type, to unwrap it.
After !, we unzip a option Type. If the optional is currently a nil, the App will CRASH.
fatal error: unexpectedly found nil while unwrapping an Optional value
But it’s a good thing, because it’s very informative. So the the variable becomes a String:

Set an optional
we can set an option to nil by:
var1 = nil
we set an option’s “associate value” by:
var1 = 1
if the var1 is an optional, this will change its “associate value”.
Declare an optional
var display: UILabel!
// var display: UILabel?
you can either declare it with ! or ?.
Diff:
!will automatically and implicitly unwrap thisoptionaleverywhere. When we call avar2!, it should bevar2.
?will not implicitly unwrap. So we callvar2?byvar2!
if optional
let mathSymbol = sender.currentTitle! is too risky, it might cause crash. So we use:
if let mathSymbol = sender.currentTitle {
}
if statement do not have ()
In this case, if means “if this currentTitle has value(associate value)”, aka “if this currentTitle? is not nil” then let mathSymbol = sender.currentTitle!
outside this statement if .. {} this mathSymbol will not even be defined.
if equal
if mathSymbol == "π" {
display.text = String(M_PI)
}
String() initialize a new String Object.

Remove A Function From A Button
When we copy a button, we copy its parameters like title, size, and also, the functions it relates. But we may need to remove the linkers.
Right click on a button:

we can click on the “x” button to remove this button’s function.
Auto Shrink Font Size On Label

set the minimum font size. if the text is too long, it will automatically shrink the font to contain more numbers.
