iOS
Model-View-Controller Design pattern ---> it is the way we organize code in xCode
1.1) Model
some kind of data structure, it is just the raw data
swift file, XML file, database, ...
1.2) View
the look of things
.storyboardfile
1.3) Controller
connect Model and View
ViewController.swiftfile
2) connect Button view to controller
right click button view and drag to controller code
outlet/action
outlet : view object itself as a variable
action : use view object to trigger an action
function
1. external para name VS internal para name
- 当calling 一个 function 时:
Default: 第一个参数不用写名字,其余参数的名字与内部名字相同
Convention:一般由函数名可以知道第一个参数的意义,其余参数的意义需要在调用的时候写出来才能明确
优点:在调用函数时,对所写参数有清晰的认识
2. return -> XXX: 函数返回值的类型为XXX
3. function can be assign
let integerAdder: (Int, Int) -> Int = addTwoIntegers
let sum = integerAdder(1, 2)
4. function can be passed by para
//defi
func a(aFunction: (Int,Int)->Int, x:Int, y:Int) -> Int {
return aFunction(x,y)
}
//calling
let res = a(integerAdder, a:10, b:8)
5. overloading function
- 虽然具有相同的函数名,但是由于参数、返回值类型的不同,因此函数的调用呈现出多种形态
6. closures --- a block of code
类似于函数,用尖括号{}
有para以及返回值
keyword
in表示 closure的body is coming
class
1. init() method
- 每个class必须至少有一个custom init() method
2. class instance property
1)get & set
manufactureName = ""
var model = ""
var makeAndModel: (make:String, model:String) {
get {
return (manufactureName, model)
}
set(newMakeAndModel) {
manufactureName = newMakeAndModel.make
model = newMakeAndModel.model
}
}
3. instance method
4. class inheritance
subclass 继承 superclass的所有东西
与正常定义方法一样
self&super
5. extension
- 对现有的class添加新的方法
protocol
- 用于对
1. protocol 定义requirement(property,method,subscript, type),却并未实现他们
2. property requirment
- 必须定义为
var - 必须explicit type
- 至少要有
get
3. type requirement
- 把prototype当做变量类型一样,对变量的类型进行限制
Auto layout
1) right click and drag view object to set constrains
2) use Automatic -> Preview to preview layout design
3) Pin : auto resize
4) Resolve auto layout issue
red line: lack of constrains
blue line: UI element layout is same as constrains
orange line: UI element layout is not same as constrains
update frame/contrains to resolve layout issue
String
- index type
//Char at index 7
let eightIndex = quote.startIndex.advancedBy(7)
let eightChar = quote[eightIndex]
//last Char
let lastChar = quote[quote.endIndex.predecessor()]
- replaceRange()
let range = quote.startIndex..<quote.endIndex
let replaceStr = "abcde"
quote.replaceRange(range, with:replaceStr)
Array
1. definition
//implicit type:由具体的赋值决定类型
var threeStr = ["abey","bob","cat"]
//explicit type: 明确指明元素类型
var emptyArray: [Int] = []
var emptyArray = [Int]()
//其它定义方法: [Int] 相当于 Arrat<Int> 的缩写
var emptyArray: Array<Int> = []
var emptyArray = Array<Int>()
//初始化
var Array(count:20, repeatedValue: 0)
//包含不同类型元素
var arrayOfAny: [Any] = [1, 2.0, "three"]
2. Accessing Arrays
- 区别于string,不需要special index type,可以用数字做index
//第一个(或最后一个)元素,对first(or last)的使用属于optinal,需要unwarp
var x = threeStr.first!
var x = threeStr.last!
//第2个
var x = threeStr[1]
3. Modifying Arrays
- Array is value type, passed by copy !
Dictionary ---> key-value pairs
1. definition
//implicit type:由具体的赋值决定类型
var stockPrices = [
"apple": 110.2,
"tesla": 234.2,
"amazon": 219.0
]
//explicit type: 明确指明元素类型
var stockPrices: [String: Double] = [:]
var stockPrices = Dictionary<String, Double>()
2. accessing
- key-value pairs 是没有确定的顺序的
//通过向Array()传递参数来得到key/value Array
//注意:得出的key和value没有顺序
let keyArray = Array(stockPrices.keys)
let valudeArray = Array(stockPrices.values)
//有可能没有value,因此optional,因此需要unwarp
let appleStockPrice = stockPrice["apple"]!
3. modify
//set key-value pairs
stockPrices["linkedin"] = 500.1
//remove one
stockPrices["linkedin"] = nil
//remove one & return value of it
let x = stockPrices.removeValueForKey("amazon")
4. nested dictionary
- 应用场景:server传输的数据格式
//逐级获取,若某一级为nil,返回nil
let firstPlaceName = marathonRes[1]?["name"]
Sets
元素唯一且无序
尖括号{}
Tuples
应用:常用于存储不同类型的数据
圆括号()
1. difinition
let httpStatus200 = (200, "OK")
var playerScores: ([Int], firstName: String, lastName: String) = ([123_000, 111_312], "Kevin", "Huang")
2. Access
- 使用dot notation来获取元素
let highScore = playerScores.0.maxElement()!
Segue
When you want more than one screen in your app
share data between view controllers
wiring up Segues
Ctrl + drag into view
Action Segue
1) Show — When the View Controllers are in a UINavigationController, this pushes the next View Controller onto the navigation stack. This allows the user to click the back button to return to the previous screen through the back button on the top left of the Navigation Bar. Basically the adaptive form of “push”
2) Show detail — When the View Controllers are in a UISplitViewController, this will replace the detail view side of the UISplitView with the next View Controller.
3) Present modally, Popover presentation, Custom...
- create a new Swift file to make this View Controller that type