心辰·Dev

A Swift Tour 习题解答

Swift 初见

  • 创建一个常量,显式指定类型为 Float 并指定初始值为 4 。

    1
    let constvalue: Float = 4
  • 删除let widthLabel = label + String(width)一行中的 String ,错误提示是什么?

    1
    error: cannot invoke '+' with an argument list of type '(String, Int)'
  • 使用\()来把一个浮点计算转换成字符串,并加上某人的名字,和他打个招呼。

    1
    2
    let someonesName = "xbx"
    let helloString = "say hello to \(someonesName) \(constvalue) times"
  • 把 optionalName 改成 nil ,greeting 会是什么?添加一个 else 语句,当 optionalName 是 nil 时给 greeting 赋一个不同的值。

    1
    2
    3
    4
    5
    6
    7
    8
    >var optionalName: String? = nil
    >if let name = optionalName {
    greeting = "Hello, \(name)"
    } else {
    greeting = "Hello, nobody!"
    }
    >print(greeting)
    Hello, nobody!
  • 删除 default 语句,看看会有什么错误?

    1
    error: switch must be exhaustive, consider adding a default clause
  • 添加一个参数来表示今天吃了什么午饭。

    1
    2
    3
    func greet(name: String, meal: String) -> String { 
    return "Hello \(name), today's meal is \(meal)."
    }
  • 写一个计算参数平均值的函数。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    func averOf(numbers: Int...) -> Int {
    var sum = 0
    var i = 0
    for number in numbers {
    sum += number
    i += 1
    }
    return sum / i
    }
  • 重写闭包,对所有奇数返回 0 。

    1
    2
    3
    4
    5
    6
    7
    numbers.map({   
    (number: Int) -> Int in
    if (number % 2 == 1) {
    return 0
    }
    return number
    })
  • 使用 let 添加一个常量属性,再添加一个接收一个参数的方法。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    class Shape {
    var numberOfSides = 0
    let numberOfAngle = 180
    func simpleDescription() -> String {
    return "A shape with \(numberOfSides) sides."
    }
    func calSideLength(length: Int) -> Int {
    return length * numberOfSides
    }
    }
  • 创建 NamedShape 的另一个子类 Circle ,构造器接收两个参数,一个是半径一个是名称,实现 area 和 describe 方法。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    class Circle: NamedShape {
    var radiusLength: Double

    init(radiusLength: Double, name: String) {
    self.radiusLength = radiusLength
    super.init(name: name)
    }

    func area() -> Double {
    return 3.14 * radiusLength * radiusLength
    }

    override func simpleDescription() -> String {
    return "A circle with radius of \(radiusLength)."
    }
    }
  • 写一个函数,通过比较它们的原始值来比较两个 Rank 值。

    1
    2
    3
    func compare(one: Rank, two: Rank) -> Rank { 
    return one.rawValue > two.rawValue ? one : two
    }
  • 给 Suit 添加一个 color 方法,对 spades 和 clubs 返回“black”,对 hearts 和 diamonds 返回“red”。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    enum Suit { 
    case Spades, Hearts, Diamonds, Clubs
    func color() -> String {
    switch self {
    case .Spades:
    return "black"
    case .Hearts:
    return "red"
    case .Diamonds:
    return "red"
    case .Clubs:
    return "black"
    }
    }
    }
  • 写一个实现这个协议的枚举。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    >protocol ExampleProtocol { 
    var simpleDescription: String { get }
    mutating func adjust()
    }

    >enum TwoNumberEnum: ExampleProtocol {
    case One, Two
    public var simpleDescription: String {
    return "A very simple enum"
    }
    mutating func adjust() {
    switch self {
    case One:
    self = Two
    case Two:
    self = One
    }
    }
    }
  • 给 Double 类型写一个扩展,添加 absoluteValue 功能。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    extension Double: ExampleProtocol {  
    public var simpleDescription: String {
    return "The number \(self)"
    }

    var absoluteValue: Double {
    return self >= 0 ? self : -self
    }

    mutating public func adjust() {
    self += 42
    }
    }
  • 修改 anyCommonElements 函数来创建一个函数,返回一个数组,内容是两个序列的共有元素。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    func findCommonElements <T, U, V where T: SequenceType, U: SequenceType, T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element, V == T.Generator.Element> (lhs: T, rhs: U) -> [V] {
    var result = [V]()
    for lhsItem in lhs {
    for rhsItem in rhs {
    if lhsItem == rhsItem {
    result.append(lhsItem)
    }
    }
    }
    return result
    }

注:语法已升级为 Swift 2