Swift 2.0-Study(-)

  1. 命令行工具:swiftc - - help 获取到Swift源码编译到各个阶段的结果

  2. 在 * / % 前加& 忽略溢出;在 + - 前加&表示溢出

  3. identity operator(===) 恒等于,检测两个对象引用是否指的是同一个对象实例。
    ~= 模式匹配


  4. let implicitDouble = 70.0
    let explicitDouble: Double = 70 // 显式的,明确的

  5. 如果要隐式转换为某个类型,需要写明要转的类型

    let label = "The width is "
    let width = 94
    let widthLabel = label + String(width)

  6. optionals 意味着有值或者值为 nil

    let optionalInt: Int? = 9

  7. 得到一个可选的基本类型的值,需要拆包,更直接的方式是强制拆包操作符(!)必须确认要拆包的值不能为 nil
    let actualInt: Int = optionalInt!

    var myString = "7"
    var possibleInt = Int(myString) // possibleInt 为可选类型
    print(possibleInt) // 7

    myString = "banana"
    possibleInt = Int(myString)
    print(possibleInt) // nil

  8. Array

    var ratingList = ["Poor", "Fine", "Good", "Excellent"] // 类型:var ratingList: [String]
    ratingList[1] = "OK"
    ratingList
    // Creates an empty array.
    let emptyArray = String

  9. 在 if 语句中使用可选绑定来检查一个可选类型是否有值, 如果不初始化,默认是 nil

    var optionalName: String? = "John Appleseed"
    var greeting = "Hello!"
    if let name = optionalName {
    greeting = "Hello, (name)"
    }

    // 可以使用单个 if 语句进行多个值得可选绑定,使用 where 条件语句,如果满足所有条件,则执行语句
    var optionalHello: String? = "Hello"
    if let hello = optionalHello where hello.hasPrefix("H"), let name = optionalName {
    greeting = "(hello), (name)"
    }

  10. Switch 语句支持任何一种数据和各种各样的比较操作,不局限于整数类型,当代码块里的条件满足时,程序自动退出switch 语句,不需要添加 break

    let vegetable = "red pepper"
    switch vegetable {
    case "celery":
    let vegetableComment = "Add some raisins and make ants on a log."
    case "cucumber", "watercress":
    let vegetableComment = "That would make a good tea sandwich."
    case let x where x.hasSuffix("pepper"):
    let vegetableComment = "Is it a spicy (x)?"
    default:
    let vegetableComment = "Everything tastes good in soup."
    }


    default 语句是被要求的,除非表达很清楚,并所有可能的条件都满足,比如应用于 enumeration 类型时,可以省略
    enum InitalVegetable: String {
    case VegetableA = "red pepper"
    case VegetableB = "celery"
    case VegetableC = "cucumber"
    case VegetableD = "watercress"
    }

    var vegetabelType: InitalVegetable = InitalVegetable.VegetableA
    switch vegetabelType {
    case .VegetableB:
    let vegetableComment = "Add some raisins and make ants on a log."
    case .VegetableC, .VegetableD:
    let vegetableComment = "That would make a good tea sandwich."
    case .VegetableA:
    let vegetableComment = "Is it a spicy ?"
    }

半开区间操作符(..<),不包含操作符右边的值

var firstForLoop = 0
for i in 0..<4 { // 0-3
firstForLoop += i
}
print(firstForLoop)

闭区间操作符(...)包含区间内所有值

var secondForLoop = 0
for _ in 0...4 { // 0-4 ()为通配符,如果不需要知道当前循环的值时可以使用
secondForLoop += 1
}
print(secondForLoop)

class NamedShape { // 定义一个类:class + 类名
var numberOfSides = 0 // 类属性,此处是变量类型
var name: String
init(name: String) { // 初始化方法,参与为每一个属性设置初始值并执行其他的一些设置
self.name = name
}
func simpleDescription() -> String { // 定义一个返回值为 String 类型的 simpleDescription() 类方法
return "A shape with (numberOfSides) sides."
}
}
shape.numberOfSides = 7 // 使用点语法访问类属性/方法
var shapeDescription = shape.simpleDescription() // 这个类方法返回值 shapeDescription 是一个 String 类型的值
let namedShape = NamedShape(name: "my named shape") // 可以不通过写 init, 通过 类名()来方便的调用初始化方法获取类的实例变量

  1. 继承
    class Square: NamedShape {
    var sideLength: Double
    init(sideLength: Double, name: String) { // 子类初始化方法
    self.sideLength = sideLength // 设置子类的属性
    super.init(name: name) // 调用父类初始化方法
    numberOfSides = 4 // 改变父类定义的属性值 Any additional setup work that uses methods, getters, or setters can also be done at this point.
    }
    func area() -> Double {
    return sideLength * sideLength
    }
    override func simpleDescription() -> String { // 重写父类的方法
    return "A square with sides of length (sideLength)."
    }
    }
    let testSquare = Square(sideLength: 5.2, name: "my test square")
    testSquare.area()
    testSquare.simpleDescription()

注:可选向下转型 as? 如果不确定向下转型是否成功,可以使用这个,不成功会返回 nil
强制向下转型 as! 如果确定向下转型会成功, 使用这个;如果你尝试 downcast 到一个错误的类型会导致 runtime 错误

你可能感兴趣的:(Swift 2.0-Study(-))