欢迎加入QQ讨论群258996829
月之残骸 头像
苹果2袋
2
月之残骸

从Java到Swift

发布时间:2016-03-15 20:06  回复:1  查看:4767   最后回复:2016-03-17 22:12  
我们学习的新事物时,通常并不是从0开始,而是从已知开始,将新事物与已知的进行比较分析,从而快速全面地了解新事物。

而我熟悉Java,所以在学习Swift时,就会将Swift与Java进行比较,思考。(文中的示例代码均来自The Swift Programming Language

概览

从Java到Swift还是比较简单的,相比Object-C,Swift和Java的语法更加接近,和最近的Kotlin就更像了。Swift同时支持面向对象编程和函数式编程。Swift比Java功能更加强大,用法更加友好。网上有一份Java和Swift的粗略对比:
java vs swift

基础部分

1.Swift没有main函数,这个有点像脚本语言。Swift程序的默认入口是main.swift文件,在iOS应用中,则通常标记了@UIApplicationMain的AppDelegate.swift文件。可以类比到Android中,在AndroidManifest.xml中定义的Application。
2.Swift不需要定义行结束符,这个是像脚本语言一样。

3.Swift使用var定义变量,一般无需指定具体的数据类型,编译器会自行判断。遇到编译器无法判断的情况,需要自己显式指定。

//Java定义变量
int x = 0;

//Swift定义变量,编译器自动识别数据类型
var x = 0;

//Swift定义变量,显式指定类型,否则此时编译器会自动识别成Int
var y : Long = 0;

4.Swift用let定义常量,Java里面是static final。
5.array跟Java中的array是一样的概念。dictionary就是Java中的map。dictionary的取值的方式是dictionary[key],接口就像array一样,简洁方便。
6.nil在swift中就类似Java中的null。nil是没有初始化成功,是没有值。
7.optional value是指该value的值可以是nil,Swift默认一个var是不能赋值nil,除非它声明了optional。optional不能直接输出,而必须unwrap,形如optionalValue!。有点类似于Java中打包好的null判断。也可以用!代替?声明一个无需unwrap的var。

逻辑控制

1.Swift的switch 语法和Java及C++很像,但是它没有break,他命中一个case后会自动退出switch。对于几个不同case同样处理的情况,可以case后面连续几个condition,用逗号隔开。

switch value {
case condition1:
    response to condition 1
case condition2, condition3:
    resoponse to condition 2 or 3
default:
    otherwise, do something else
}
2.Swift的switch支持运算,运算的话,就是说不仅仅是equal,而是支持满足特定要求。
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
    print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
    print("(\(x), \(y)) is on the line x == -y")
case let (x, y):
    print("(\(x), \(y)) is just some arbitrary point")
}
// prints "(1, -1) is on the line x == -y"

3.while循环和Java或者C++中基本一致,不过while后面直接写condition,不需要用括号。
4.for循环和Java也基本一样,不过也是不需要括号。for循环中,..<的用法比较方便。下划线符号_(替代循环中的变量)能够忽略具体的值,并且不提供循环遍历时对值的访问。for-in则有点类似与Java中for each循环。

函数

1.函数的定义和Java很不一样。Swift函数的定义形如 func functionName(argName: Type) -> Return Type:

func sayHelloAgain(personName: String) -> String {
    return "Hello again, " + personName + "!"
}
2.Swift函数可以返回多个返回值,这个功能真是太猛了。
func minMax(array: [Int]) -> (min: Int, max: Int) {
    var currentMin = array[0]
    var currentMax = array[0]
    for value in array[1..<array.count] {
        if value < currentMin {
            currentMin = value
        } else if value > currentMax {
            currentMax = value
        }
    }
    return (currentMin, currentMax)
}
3.Swift函数可以接收不定参数,跟Java基本类似的用法。
4.函数可以嵌套,这个是Java或者C++都没有的,挺好用。例如经常有一段逻辑,用一个函数实现太长,在Java或者C++中,通常是会把它拆分成几个函数,保持每个函数短小,功能单一。但是这样拆分的函数并不能很好的表明他们是一个功能的,不够“内聚”。用这种Swift函数嵌套的方式就能较好实现。
func chooseStepFunction(backwards: Bool) -> (Int) -> Int {

    func stepForward(input: Int) -> Int { 
        return input + 1 
    }

    func stepBackward(input: Int) -> Int { 
        return input - 1 
    }

    return backwards ? stepBackward : stepForward
}
5.Swift支持函数类型,根据输入参数和返回值确定一个函数类型。函数类型可以让函数像,普通数据类型一样使用。例如函数的参数可以另外一个函数,注意,不是另外一个函数的返回值,而是另外一个函数,只要类型符合即可。这个相当于是函数级别的多态,真的有点猛。
//定义一个函数,类型是(Int, Int)->Int
func addTwoInts(a: Int, _ b: Int) -> Int {
    return a + b
}

//定义另一个函数,其中一个参数是(Int, Int) -> Int函数
func printMathResult(mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
    print("Result: \(mathFunction(a, b))")
}

//直接将addTwoInts()作为printMathResult()的参数
printMathResult(addTwoInts, 3, 5)

6.Swift支持闭包,我觉得可以理解成“匿名函数”,只需要描述输入输出,用in分开输入输出描述,已经函数体,无需定义函数名。

类与结构

1.类的构造函数,直接叫init()。类函数调用跟Java,C++基本一样。self相当于Java中的this。
2.在Swift中class的成员访问权限控制级别有public, internal, private,类似Java中的public, protected, private。
3.deinit是析构函数。Java中也有finalize()函数。不过Java的finalize()函数并不确保一定被调用,所以并不推荐override该函数。
4.类的继承跟C++有点像,使用:。

class SomeSubclass: SomeSuperclass {
    // subclass definition goes here
}
5.他的setter和getter函数跟Java不太一样,是隐式调用的。我觉得Swift的设计思想是,用户只需关心输入和输出,其他的不用关心。例如此处只需关心需要set或者get。具体的set和get函数则是封装的,无需使用者去关心。又譬如上面提到的method的type,只要定义了输入和输出,就定义了一类method,那就可以对这种type有多种具体实现。
struct Point {
    var x = 0.0, y = 0.0
}
struct Size {
    var width = 0.0, height = 0.0
}
struct Rect {
    var origin = Point()
    var size = Size()
    var center: Point {
        get {
            let centerX = origin.x + (size.width / 2)
            let centerY = origin.y + (size.height / 2)
            return Point(x: centerX, y: centerY)
        }
        set(newCenter) {
            origin.x = newCenter.x - (size.width / 2)
            origin.y = newCenter.y - (size.height / 2)
        }
    }
}

var square = Rect(origin: Point(x: 0.0, y: 0.0),
    size: Size(width: 10.0, height: 10.0))
let initialSquareCenter = square.center
square.center = Point(x: 15.0, y: 15.0)
print("square.origin is now at (\(square.origin.x), \(square.origin.y))")
6.Swift的枚举和Java类似,本质是一个类,里面可以包含函数。
7.Swift的struct和class写法基本一样,区别在于struct传递的是内容的copy,而class传递的是引用。这个厉害啊。
8.枚举还支持associated value,这个是Java没有的。
enum Barcode {
    case UPCA(Int, Int, Int, Int)
    case QRCode(String)
}

var productBarcode = Barcode.UPCA(8, 85909, 51226, 3)
9.protocol类似于Java中的interface。
10.extension比较强大,甚至变态,可以动态往某个类中增添函数以及成员变量,动态让某个类实现某个protocol,而无需修改该类源代码。Java新增成员变量,新增函数,实现某个interface,Java都只能通过继承实现。而这个直接实现,且对一切该类的对象生效,包括extend之前已经创建的对象。
extension Double {
    var km: Double { return self * 1_000.0 }
    var m: Double { return self }
    var cm: Double { return self / 100.0 }
    var mm: Double { return self / 1_000.0 }
    var ft: Double { return self / 3.28084 }
}
let oneInch = 25.4.mm
print("One inch is \(oneInch) meters")
// prints "One inch is 0.0254 meters"
let threeFeet = 3.ft
print("Three feet is \(threeFeet) meters")
// prints "Three feet is 0.914399970739201 meters"
11.Swift泛型和Java类似的,Swift的泛型支持where语句,可以在对类型控制之外,作更加精细的控制。
func allItemsMatch<
    C1: Container, C2: Container
    where C1.ItemType == C2.ItemType, C1.ItemType: Equatable>
    (someContainer: C1, _ anotherContainer: C2) -> Bool {

        // check that both containers contain the same number of items
        if someContainer.count != anotherContainer.count {
            return false
        }

        // check each pair of items to see if they are equivalent
        for i in 0..<someContainer.count {
            if someContainer[i] != anotherContainer[i] {
                return false
            }
        }

        // all items match, so return true
        return true

}

内存管理

Swift和Java类似,也无需自己管理内存,Swift是由ARC(Automatic Reference Counting)机制来回收内存的,Java是有垃圾回收机制来保证内存被及时回收。但是两者的回收机制有所区别。我的理解是Swift的ARC机制着眼于无效的对象,就是那些没有被任何人引用到的对象。因此,如果两个对象循环引用,就会无法被回收,引起泄露。此时就需要Weak Reference或者Unowned Reference来打破这个环。
下图中Person对象和Apartment对象由于互相强引用,无法被ARC回收。
screenshot

而Java的垃圾回收机制,从反面思考,着眼于哪些是有效的对象,即有被GC Root引用到的对象是有效的,其他的都是无效的。因此哪怕有对象相互引用,只要没有被GC Root引用到,都会被垃圾回收器回收掉(如下图所示)。从这此处来看,Java的策略更优。也由此可以看到换个角度看问题是多么重要。
screenshot

参考引用

  1. The Swift Programming Language (Swift 2.1)https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/index.html#//apple_ref/doc/uid/TP40014097-CH3-ID0
  2. what is the entry point of swift code execution? http://stackoverflow.com/questions/24105690/what-is-the-entry-point-of-swift-code-execution
  3. Swift程序入口深度分析 http://00red.com/blog/2014/11/20/swift-main-study/
  4. Swift 函数式编程实践 http://codebuild.me/2015/09/15/swift-functional-programming-intro/
  5. Java vs Swift http://slidenerd.com/2014/11/15/swift-vs-java/
  6. SwiftGuide https://github.com/ipader/SwiftGuide
  7. Swift https://developer.apple.com/swift/
  8. Learn the Essentials of Swifthttps://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson1.html
  9. What is an “unwrapped value” in Swift? http://stackoverflow.com/questions/24034483/what-is-an-unwrapped-value-in-swift
  10. 自动引用计数 https://numbbbbb.gitbooks.io/-the-swift-programming-language-/content/chapter2/16_Automatic_Reference_Counting.html


分享自:https://yq.aliyun.com/articles/7567
赢了现在输了未来 头像
苹果2袋
2
赢了现在输了未来   2016-03-17 22:12

写得不错,我也搞过Java。

您还未登录,请先登录

热门帖子

最新帖子