Joined the Tesla
However, Swift’s progress since its WWDC debut in June 2014 has been noticeable: not only has it improved its syntax features and API specifications, but it also opened source and supported the Linux platform in 2015. The figure below is a comparison of the TIOBE programming language index trend between Objective-C and Swift. Swift has seen a steady rise while Objective-C has seen a sharp decline, indicating that the promotion and adoption of Swift has been relatively successful in the past two years.
So what’s so good about Swift? As a language that debuted in 2014, Swift has absorbed many of the features of good programming languages. Strong typing, type inference, multiple paradigms, object-oriented… My personal favorite is that Swift is designed to be a multiparadigm programming language: it has both the necessary features for imperative programming (mutable variables and C-like operators and control declarations) and a rich set of operators for functional programming. At the same time, Swift supports generic programming, that is, you can define generic classes and interfaces using type parameters. Whether you come from a C, Java, or Scala background, you can pick up the language quickly. (Of course, for large and complex projects, the programming paradigm is better unified. This is only about starting difficulty.)
The following is the implementation of the classical algorithm quicksort by imperative and functional respectively in Swift. This example is mainly intended to illustrate the multi-paradigm characteristics of Swift (note that Swift handles mutable variables).
func quickSort(array: [Int]) -> [Int] {
if array.count <= 1 {
return array
} else {
let pivot = array[array.count / 2]
let left = quickSort(array: array.filter({$0 < pivot}))
let right = quickSort(array: array.filter({$0 > pivot}))
return left + [pivot] + right
}
}
Copy the code
func quickSortImperative(mutableArray: inout [Int], l: Int, r: Int) {
if r <= l {
return
}
let pivot = mutableArray[(l + r) / 2]
var i = l
var j = r
while (i <= j) {
while (mutableArray[i] < pivot) {
i += 1
}
while (mutableArray[j] > pivot) {
j -= 1
}
if (i <= j) {
let t = mutableArray[i]
mutableArray[i] = mutableArray[j]
mutableArray[j] = t
i += 1
j -= 1
}
}
if (l < j) {
quickSortImperative(mutableArray: &mutableArray, l: l, r: j)
}
if (i < r) {
quickSortImperative(mutableArray: &mutableArray, l: i, r: r)
}
}
Copy the code
Type variables, which should be familiar to developers familiar with Java or C++ templates:
func findIndex<T: Equatable>(of valueToFind: T, in array:[T]) -> Int? {
for (index, value) in array.enumerated() {
if value == valueToFind {
return index
}
}
return nil
}
Copy the code
The Optional type is represented directly by a question mark, which is very convenient to use
class Person {
var residence: Residence?
}
Copy the code
Using Extension not only separates the functionality of a bloated class, but also extends already encapsulated types
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}}Copy the code
Interested students can use Xcode Playground to experience the characteristics of Swift language by themselves, which will not be repeated here.
Also, Swift compatibility isn’t too disappointing, and you can use any Objective-C library. Libraries written in Swift have a way of being called by Objective-C projects. Soon after the open source of Swift language, swift-based server frameworks, such as Kitura and Perfect of IBM, appeared, which made Swift have a broader development prospect.
However, every coin has two sides, as more and more developers start to adopt Swift, there are also more and more people making fun of Swift, which is mainly focused on the toolchain is not very good, the main points are: IDE index is slow, IDE index is unstable, build is slow/unstable, e.
(Yes you read that correctly, the compiler has its own segmentation fault)
(Xcode crashes in the middle of indexing swift code and becomes notepad)
These issues make the Swift development experience less than it should be, especially for medium or large projects, where IDE index failures can seriously affect the speed of reading and developing code. And compiler glitches from time to time will directly make already worked overtime hard programmer vomit blood. Posts poking fun at the Swift compiler are everywhere:
(Someone actually put together a Github repo to collect all the examples that could cause the Swift compiler to fail)
(For Swift build time optimization, everyone is also very busy.)
Even worse (a very senior head of Mobile engineering at my former employer) started asking, Apple, why don’t you use Swift for your own app? Are you going to let us, the unfortunate ones, report bugs to you? According to his actual measurement, only 5 Apple apps were developed by Swift, which were WWDC, Music, Swift Playground, Apple Store and Calculator. These apps are either relatively simple or use very low frequencies, which raises the question of why Apple uses so little of its own language. How does this make it safe to develop important or large projects with Swift?
In short, the Swift debate has been going on and on, with people still believing Swift can become the mainstream of iOS in the future to a certain extent, but hating Swift for its current shortcomings. Development experience problems that big project director before choosing Swift will also have many concerns, such as development efficiency is restricted by the development environment and compilation speed, as well as the instability of the language and tool chain will lead to additional development cycle, in this era of fast product iteration, Swift, seems to make a lot of people pause.
Swift, what’s the way forward?