1 Basics
Check out the Swift Guide.
IOS layers:
- Cocoa Touch
- Web View, Controls, Camera, Image Picker, etc
- Media
- Audio, JPEG, etc
- Core Services
- Collections, Networking, File, Threading, etc
- Core OS
- OSX Kernel
Demo
- Edit UI in storyboard.
- Connect UI and controller functions or variables by hold Control and drag.
- Connection can be
- Action: a function
- Outlet: a special var of a thing in UI
- Outlet collection: an array var of UI things
- Use yellow View Controller to add into the array.
- Connection can be
- Right click on UI component to check connections.
- Hold Option and click function names to check document.
- Command click connection var to jump to def or refactor.
- Get real value from optional:
let n = arr.index(of: x)!
- better:
if let n = arr.index(of: x) {}
- better:
class ViewController: UIViewController // inheritance
{
var count : Int = 0 { // instance var
didSet {
// to be executed when set this var
flipCountLabel.text = "Flips: \(count)" // embed variables in string
}
}
var num = 2.0 // works too
// Each argument has two name: external and internal; can be only one
func flipCard(withEmoji emoji: String, on button: UIButton) {
// ...
}
}
2 MVC
A single MVC can only manage at most a single screen.
- Model is what the application is; UI independent.
- Controller: interpret/format model info for the view.
- Controllers are almost always the data source.
- View action to controller target.
- Delegation: should, will, did.
- Data source
-
View: generic UI components.
- Controller can talk to model.
- Model can use Notification & KVO to broadcast changes to Controller.
- Controller can outlet to view.
- View to controller
- View action to controller target.
- Delegation: should, will, did.
- Data source
Create a class: File -> New -> File -> Swift File
- class
- Reference type
- Initializer is needed when we have uninitialized vars.
- struct
- Value type, get copied when assigned or passed
- No inheritance
- Include free initializer
// class
class Concentration
{
var cards = Array<Card>() // init; or [Card]()
func chooseCard(at index: Int) {
}
init(numCards: Int) {
for id in 0..<numCards { // ..< means not including, ... means including
let card = Card(id: id)
cards.append(card)
cards.append(card) // copied
// or
cards += [card, card]
}
}
}
// struct
struct Card
{
var isFaceUp = false
var isMatched = false
var id: Int
static var idFactory = 0
static func getUniqId() -> Int {
Card.idFactory += 1
return Card.idFactory
}
init() {
self.id = getUniqId()
}
}
// in ViewController
class ViewController: UIViewController
{
// lazy allows init this var only when using it
// no didSet() for lazy
lazy var game = Concentration() // create a model
var emoji = Dictionary<Int, String>() // dict
// or
var emoji = [Int: String]()
func updateViewFromModel() {
for index in cardButtons.indices {
// ...
}
}
func emoji(for card: Card) -> String {
if emoji[card.id] == nil {
let randInd = Int(arc4random_uniform(UInt32(emojiFactory.count)))
emoji[card.id] = emojiFactory.remove(at: randInd)
}
return emoji[card.id] ?? "?"
}
}