swift

2019-02-20  本文已影响14人  liboxiang
if x = y {
        // 此句错误, 因为 x = y 并不返回任何值
}
var optionalInteger: Int?
var optionalInteger: Optional<Int>

当声明一个可选类型的时候,要确保用括号给?操作符一个合适的范围。例如,声明可选整数数组,应该写成(Int[])?;写成Int[]?会报错。

  • 如果你给一个新常量赋值42并且没有标明类型,Swift 可以推断出常量类型是Int
  • 如果你没有给浮点字面量标明类型,Swift 会推断你想要的是Double
  • 如果表达式中同时出现了整数和浮点数,会被推断为Double类型
func vari<N>(members: N...){
    for i in members {
        print(i)
    }
}
vari(members: 3,4,5)
vari(members: "Google", "Baidu", "W3CSchool")
func swapTwoInts(a:inout Int, b:inout Int){
    
    let t = a
    a = b
    b = t
}

var x = 0,y = 100
print("x = \(x) ;y = \(y)")
swapTwoInts(a:&x, b:&y)
print("x = \(x) ;y = \(y)")
enum DaysofaWeek {
    case Sunday
    case Monday
    case TUESDAY
    case WEDNESDAY
    case THURSDAY
    case FRIDAY
    case Saturday
}

var weekDay = DaysofaWeek.THURSDAY
weekDay = .Saturday

注:和 C 和 Objective-C 不同,Swift 的枚举成员在被创建时不会被赋予一个默认的整型值。在上面的DaysofaWeek例子中,Sunday,Monday,……和Saturday不会隐式地赋值为0,1,……和6。相反,这些枚举成员本身就有完备的值

恒等运算符(===),如果两个常量或者变量引用同一个类实例则返回 true
不恒等运算符(!==),如果两个常量或者变量引用不同一个类实例则返回 true

let quotation = """
I said "I have \(apples) apples."
And then I said "I have \(apples + oranges) pieces of fruit."
"""
let nickName: String? = nil
let fullName: String = "John Appleseed"
let informalGreeting = "Hi \(nickName ?? fullName)"
上一篇下一篇

猜你喜欢

热点阅读