设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 技术文摘 查看内容

从Objective-C到Swift—Swift糖果

2014-6-30 14:50| 发布者: joejoe0332| 查看: 5873| 评论: 0|原作者: GoodLoser, chasehong|来自: oschina

摘要: Swift带来很多确实很棒的特性,使得很难再回到Objective-C。主要的特性是安全性,不过这也被看成是一种额外副作用。


Swift Switch语句

  就像你看到的,Swift中switch语句有很多优化。

   隐式的fall-through行为已经改为了显示的:

var (i, j) = (4, -1) // Assign (and create) two variables simultaneously
switch i {
case 1:
    j = 1
case 2, 3: // The case for both 2 and 3
    j = 2
case 4:
    j = 4
    fallthrough
case 5:
    j++
default:
    j = Int.max // The Swift version of INT_MAX
}


  就像前面看到的,Switch 语句可以访问枚举的关联值,不过它还可以做更多:

var tuple: (Int, Int) // Did I mention that Swift has tuples? :-)
var result: String

tuple = (1,3)

switch tuple {
case (let x, let y) where x > y:
    result = "Larger"
case (let x, let y) where x < y:
    result = "Smaller"
default:
    result = "Same"
}


  甚至可以使用String:

var s: String = "Cocoa"
switch s {
case "Java":   s = "High caffeine"
case "Cocoa":  s = "High sugar"
case "Carbon": s = "Lots of bubbles"
default: ()
}


  另外,如果你觉得他可以使你的代码更可读,你可以重载~=操作符来改变switch语句的行为。

func ~=(pattern: String, str: String) -> Bool {
    return str.hasPrefix(pattern)
}

var s = "Carbon"
switch s {
case "J":  s = "High caffeine"
case "C":  s = "No caffeine"
default: ()
}


  你可以从 Conditional Statements 这篇文章中了解更多关于switch语句的知识。


类与结构体

  类似于C++,Swift的类与结构体初看是一样的:

01class Apple {
02    var color = "green" // Property declaration
03    init() {} // Default initializer
04    init(_ color: String) { // '_' means no argument name
05        self.color = color
06    }
07    func description() -> String {
08        return "apple of color \(color)"
09    }
10    func enripen() {
11        color = "red"
12    }
13}
14 
15struct Orange {
16    var color = "green"
17    init() {}
18    init(_ color: String) {
19        self.color = color
20    }
21    func description() -> String {
22        return "orange of color \(color)"
23    }
24    mutating func enripen() { // NOTE: 'mutating' is required
25        color = "orange"
26    }
27}
28 
29var apple1 = Apple()
30var apple2 = apple1 // NOTE: This references the same object!
31apple1.enripen()
32apple2.description() // Result: "apple of color red"
33 
34var orange1 = Orange()
35var orange2 = orange1 // NOTE: This makes a copy!
36orange1.enripen()
37orange2.description() // Result: "orange of color green"


  主要的不同点在于类是(和块相似的)引用类型,而结构体是(和枚举相似的)数值类型。所以两个变量能够指向同一个(类的)对象,而把一个结构体赋给另外一个变量则必须做一个此结构体的(缓慢的)拷贝。关键词'mutating'告诉调用者enripen()方法不能被常结构体调用。把一个常引用mutating给一个类对象则没有问题。


  Swift中大多数内建类型实际上都是结构体。甚至Int型也是。通过点击Cmd你能够看到内建类型的申明,比如Int型的Swift(或者Playground)源码。数组和词典类型也是结构体,但是数组在某些方面表现得像是引用类型:赋值数组并不拷贝每一个元素,实际上你可以更新常数组只要元素的个数保持不变。


1let array1 = [1, 2, 3]
2let array2 = array1 // A copy, but references the same elements!
3array1[1] = 5 // Legal, as it doesn't modify the struct but a referenced element
4array2 // Result: [1, 5, 3]


  在苹果的文档中,你可以读到更多关于Collection Types的内容。



酷毙

雷人

鲜花

鸡蛋

漂亮
  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部