ksnowlv

回顾过去,总结以往;立足现在,铭记当下;技术为主,笔记而已.

Swift泛型

| Comments

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/// 交换两个对象的值
///
/// - Parameters:
///   - aValue: 对象a
///   - bValue: 对象b
func swapValues<T>(_ aValue: inout T, _ bValue: inout T) {
    let temp = aValue
    aValue = bValue
    bValue = temp
}

/// 栈模板
struct QStack<T> {

    var items = [T]()
    mutating func push(_ item: T) {
        items.append(item)
    }

    mutating func pop() -> T {
        return self.items.removeLast()
    }

    func isEmpty() -> Bool {
       return (0 == self.items.count)
    }

    var count: Int {
        return self.items.count
    }

    subscript(i: Int) -> T {
        return self.items[i]
    }
}

Comments

comments powered by Disqus
Included file 'custom/after_footer.html' not found in _includes directory