课程名称:Developing iOS 11 Apps with Swift
课程链接:https://itunes.apple.com/podcast/id1315130780
Stack
把不同的控件选中,然后放到一个stack中即可
layout
top space
按住ctrl 将控件拖到刘海的地方 选则top space to area
leading sapce to safe area
按住ctrl 将控件拖到最左边 选则leading sapce to safe area
trailing space to safe area
按住ctrl 将控件拖到最右边 选则trailing sapce to safe area
Bottom space to safe area
按住ctrl 将控件拖到最右边 选则Bottom sapce to safe area
控制控件间的距离
按住ctrl拖拽一个控件到另一个控件上,选择vertical spacing
,在inspect中选择合适的constraints即可。(如
控件大小问题
有时候会因为选择的控件大小导致在不同的设备上出现问题,这个时候通过View controller scene中的箭头进入,选择合适的fix方式即可
浮点数for问题
floating point countable range
1 | for i in stride(from: 0.5, through: 15.25, by: 0.3){ |
tuple
a grouping of values
1 | let x: (String, Int, Double) = ("hello", 5, 0.85) // the type of x is a "tuple" |
命名的tuple
1 | let x: (w: String, i: Int, v: Double) = ("hello", 5, 0.85) |
Tuple可以作为返回值
1 | func getSize() -> (weight: Double, height: Double) { |
Computed Properties
simple example
1 | lazy var game: Concentration = Concentration(numberOfPairsOfCards: (cardButtons.count + 1)/2) |
在上面这行代码中,每次都要计算 cardButtons.count + 1)/2
这个过程。为了让他看起来更加优雅,但是没有必要重新定义一个func,就可以采用computed properties的方式来传入这个值。
1 | lazy var game: Concentration = Concentration(numberOfPairsOfCards: numberOfPairsOfCards) |
相当于每次get这个numberOfPairsOfCards
的时候计算了这个(cardButtons.count + 1) / 2
这个值
complex example
see project Concentration -> file Concentration.swift -> indexOfOneAndOnlyFacedUpCard
Access Control
- internal: this is default, it means “usable by any object in my app or framework”
- private: this means “only callable from within this object” 类似于C++的prive
- private(set): this means “this property is readable outside this object, but not settable”表示这个var可以被外部get,无法被set
- fileprivate: accessible by any code in this source file 当前文件中可以访问
- public: (for frameworks only) this can be used by objects outside framework
- open: (for frameworks only) public and object outside framework can subclass this
Extensions
给一个class/struct/enum扩展属性/方法,但是有一些限制,如不能重载属性和方法,只能添加方法。添加的属性不能存储,只能计算。
1 | let randomIndex = Int(arc4random_uniform(UInt32(emojiChoices.count))) |
这段代码中let randomIndex
很不优雅,每次都要书写这样长的一段代码,并且可能重复使用,所以使用extension可以化简为更加优雅的方式
1 | emoji[card.identifier] = emojiChoices.remove(at: emojiChoices.count.arc4random) |
对于Int类型进行extension,直接返回一个满足条件的结果(在这里是随机数)。使用.arc4random
即可调用
Optionals
optionals是一个enum类型
1 | enum Optional<T> { |
具体信息请查看Slides
Data Structure
4 essential data structure-buildiing concepts in Swift
- class
- struct
- enum
- protocol
class
- Supports OOD
- 单继承 of both functioanlity and data
- 引用类型(类存储在heap中,通过指针传递)
- heap自动保持clean(通过reference counting,而非垃圾回收机制)
- e.g. ViewController, UIButton, Concentration
注:
reference counting: 系统通过计算引用的次数,当引用次数为0的时候,就会被toss
Memory Management:
- strong(缺省)
- weak
- unowned
Sturct
- value类型(struct不存在heap中,通过copy传递)
- 高效的拷贝和写入
- 无继承
- This copy on write behavior requires you to mark mutating methods
- Mutability controlled via
let
(e.g. 对于let定义的Array无法添加元素) - 支持functional programming design
- e.g. Card, Array, Dictionary, String …
enum
- 用于对于一些含有离散的值的变量
- 每一个离散的值都可以有”associated data”
- The associated data is the only storage that an enum can have(no instance variables)
- 值类型(通过拷贝传值)
- 可以有方法和计算的属性,并且只能有计算的属性
- e.g. PlayingCard(with
Rank
andSuit
enum)
protocol
- A type which is a declaration of functionality only
- No data storage of any kind (so it doesn’t make sense to say it’s a “value” or “reference” type)
- Essentially provides multiple inheritance (of functionality only, not storage) in Swift