美文网首页
Lecture3 Swift Programming Langu

Lecture3 Swift Programming Langu

作者: 快乐捣蛋鬼 | 来源:发表于2019-07-14 23:41 被阅读0次
    Lecture3_Review.png
    Lecture3_Today.png

    Notes

    1.Range vs. CountableRange

    Range:范围。0.25...15.5,浮点数不按整数递增,而是按浮点值递增。
    CountableRange:可数范围。0...5,按照整数值递增

    for i in 0...5 {
    
    }
    for i in stride(from: 0.5, to: 15.25, by: 0.25) {
    
     }
    

    2.Tuples 元组

    元组不可以用来index,如果需要index,使用数组,使用元组一般都是为了可以通过name获取特定的值。

    let x: (String, Int, Double) = ("hello", 5, 0.85)
    let (word, number, value) = x // 给元组中的每一个元素命名
    print(word)  //hello
    print(number)  //5
    print(value)  //0.85
    
    // 或者在声明这个元组的时候就命名
    let x: (word: String, number: Int, value: Double) =  ("hello", 5, 0.85)
    print(x.word)  //hello
    print(x.number)  //5
    print(x.value)  //0.85
    let (wrd, num, val) = x // 重命名
    

    3.Computed properties

    属性的值除了可以被储存之外也可以被计算

    //一个典型的储存类型的属性
    var foo: Double
    //一个可计算的属性
    var foo: Double {
        get {
            // 每当需要使用foo时,就用get{}里面的代码计算foo的值
        }
        set(newValue) {
            // 每当设定foo的值时,set{}里面的代码会被执行,foo被设置成新的值
        }
    }
    

    可以只有get没有set,这样的就只可读, 可以简写成:

    var foo: Double {
        get {
            return xxxx
        }
    }
    var foo: Double {
        return xxxx
    }
    
    
    
    lecture3_ComputedProperties.png

    4.Access Control

    A good strategy is to just mark everything private by default. Then remove the private until you're sure you want someone else to call this method somewhere.
    先全部标记 private,如果你确定需要别人在某处调用时再remove掉 private。


    lecture3_AccessControl.png

    5.Assertion 断言

    An assertion is just a function that you call where you assert something to be true and if it's not, your program crashes and print out an error.
    当你在某处断言something is true时,assertion是你在此处调用的一个函数,如果断言不是真的,程序将崩溃并打印出一个错误。
    断言在发送到AppStore时是被忽略的,但是当你在开发中使用断言,这可能会使程序崩溃,这很好。断言是保护API的好方法。

    func chooseCard(at index: Int) {
        // 如果选中的index不包含在cards.indices里,程序直接崩溃,并打印出后面的message
        assert(cards.indices.contains(index), "ConcentrationRule.chooseCard(at index: \(index): chosen index not in the cards.")
        // 选中的卡片没有匹配
        if !cards[index].isMatched {
            if let matchIndex = indexOfOneAndOnlyFacedUp, matchIndex != index {
                // check if cards match
                if cards[matchIndex].identifier == cards[index].identifier {
                    cards[matchIndex].isMatched = true
                    cards[index].isMatched = true
                }
                cards[index].isFacedUp = true
            } else {
                // either no cards or two cards
                indexOfOneAndOnlyFacedUp = index
                
            }
        }
    }
    

    6.enum

    enum里的每个case都可以有一个associated data
    1.声明一个enum with associated data(关联值)

    enum FastFoodMenuItem {
        case hamburger(numberOfPatties: Int)
        case fries(size: FryOrderSize)
        case drink(String, ounces: Int) // 有两个关联值,其中没有命名的String代表品牌,e.g."Coke"
        case cookie
    }
    
    enum FryOrderSize {
        case large
        case small
    }
    

    2. assign a enum
    如果那个item有关联值,在assign的时候必须提供。你唯一设置enum的关联值的机会就是在你assign它的时候,也就是你只有在assign的时候才可以设置关联值,你之后都不可以再对该item的关联值进行改动。

    let menuItem: FastFoodMenuItem = FastFoodMenuItem.hamburger(numberOfPatties: 2)
    var otherItem: FastFoodMenuItem = FastFoodMenuItem.cookie
    

    3.check an enum's state
    an euum's state is checked with a switch statement
    如何判断当前的enum是否等于某个case?不能使用 == 来检查,要使用switch语句
    (可以忽略associated data,看case1,2,3)

    var menuItem: FastFoodMenuItem = FastFoodMenuItem.hamburger(numberOfPatties: 2)
    
    switch menuItem {
    case FastFoodMenuItem.hamburger:
        print("burger")  // 忽略关联值
    case FastFoodMenuItem.hamburger(numberOfPatties: 2):
        print("burger with 2 patties") // 指定关联值为 2
    case FastFoodMenuItem.hamburger(let pattyCount):
        print("burger with \(pattyCount) patties") // 不忽略关联值,得到关联值,名称可以与之前的不同
    case .fries, .cookie:
        print("Fries & Cookie") // 可以合并
    case FastFoodMenuItem.fries(size: FryOrderSize.large):
        break // 如果找到匹配的item,但是什么也不想做,使用break
    default:
        break // default 对应所有其他的cases
    }
    
    

    7. Optionals are enums

    enum Optional<T> {  // 泛型,类似于Array<Element> 或 Dictionary<Key, Value>
        case none  // not set, 没有设置
        case some<T>  // 有T类型的关联值
    }
    

    8.Data structrues

    class
    1.支持面向对象的设计
    2.具有功能和数据的单一继承
    3.引用类型,存在于heap堆中,使用ARC自用引用计数进行清理。
    struct
    1.值类型
    2.没有数据继承
    enum
    protocol

    相关文章

      网友评论

          本文标题:Lecture3 Swift Programming Langu

          本文链接:https://www.haomeiwen.com/subject/yecwkctx.html