We love using (Taylor) Swift for developing on iOS
Swift is a new language made by Apple for building apps on iOS and mobile guru Leo shares his love for coding in Swift.
Swift is a new programming language created by Apple for iOS and OS X development. Before Swift, the main language for Apple developer was Objective-C which has been more than 30 years old. Swift inherits much from Objective-C, but also adds more modern features for developers to write code in a simpler, cleaner way.
Swift has been available to the public for about two years now, but, as far as I know, most original projects don’t use Swift because large amounts of Objective-C code still need consistent maintenance and the new language is still not very stable. New projects coming out are much more likely to use Swift, but you can also use a mix of the two languages for your projects like me. Our project code is a mix of the two languages, bu we’re going to migrate the existing Objective-C code to Swift and transition to executing projects en in the future.
As a developer, I love Swift very much, especially its functional programming, which can make your code much more effective than Objective-C. Let’s get to know Swift and you’ll see why I love it (you probably will, too).
Safety
Swift is a pretty safe language for programmers: the compiler will check and infer the type for you and all unsafe code will be eliminated at when it’s time to compile. Moreover, with the development of Swift 2.0, the exception handler provides a way to deal with exceptions happening at runtime. All of these make developers feel so much more confident with their own code.
Further, and more importantly, Swift has a new syntax called Optional
, which means there may be a value or there may be nil
. The main purpose of Optional
is to take the value check from runtime to compile time, while most other programming language would always give an error at runtime, the awful NullPointerException
.
The Optional
significantly increases security; at the same time, it does bring much more complex code. To avoid this, Swift offers a range of syntactic sugar, which can use Optional
more conveniently, such as optional binding and optional chaining. You can see more details in The Swift Programming Language guide.
Readability and Scalability
Beyond the stability of Swift code, I prefer the language for its readability and scalability, Swift absorbs lots of modern features from other programming languages, such as Protocol
and Generic
; all these modern changes result in a language that is easy and fun to use. Swift 2.0 even made a great improvement with Protocol Extension
, which means you can define default behavior for a protocol. Apple even uses Protocol Extension
to refactor lots of Swift standard libraries, and with the new libraries, the Swift code is more readable and easier to use.
If you have some experience with Object-Oriented Programming
, you will be pretty familiar with Inheritance
and Polymorphism
, which have greatly enhanced the scalability and reusability of the code, but not all cases are suitable with Inheritance
which represents a “is-a” relationship between classes. For these “isn’t-a” cases, the protocol is much more powerful.
“Talk is cheap. Show me the code.”
protocol Ordered {
func precedes(other: Self) -> Bool
}
struct Number: Ordered {
var value: Double = 0
func precedes(other: Number) -> Bool {
return self.value < other.value
}
}
So, we define a protocol named Ordered
which has a precedes
function. After this, we can create lots of types for implementing this protocol and make types capable of being ordered. Moreover, if you want to order some original types created by someone else, you can use Protocol extension
, just like this:
extension Ordered where Self: Comparable {
func precedes(other: Self) -> Bool {
return self < other
}
}
extension Int: Ordered {}
extension String: Ordered {}
By using Protocol
and Protocol Extension
, we extend the default Int
and String
to be ordered, which is just the same way as Swift standard libraries did.
The other improvement of Swift is Generic
, which is really hard to explain in words, but is very helpful and useful for your code. Just remember this, we want to have a binary search function for the integer array, so we may have some code looking like this:
func binarySearch(sortedKeys: [Int], forKey k: Int) -> Int {
// ....
}
What if I want the binary search function also be able to work with a string array? I may need to add a new function like this:
func binarySearch(sortedKeys: [String], forKey k: String) -> Int {
// ....
}
Well, what if I want it to be usable for more and more types? The next step is (witness the miracle!) to make a binary search function which can be used on all ordered arrays.
func binarySearch<T: Ordered>(sortedKeys: [T], forKey k: T) -> Int {
var lo = 0
var hi = sortedKeys.count
while hi > lo {
let mid = lo + (hi - lo) / 2
if sortedKeys[mid].precedes(k) { lo = mid + 1 }
else { hi = mid }
}
return lo
}
With the generic type T
and additional restrictions like T: Ordered
, you can make the function to work with all the types you want. You’ll also see that the Protocol
and the Generic
work together very well and make our code more readable and scalable.
Fun
Enough of all the boring stuff. Let’s just talk something about fun now.
I like programming and it brings me a lot of joy, but writing the same code every day will always be boring. I like new things, especially new programming languages with a new paradigm, and Swift is just that.
I can’t easily say Swift is an Object-Oriented Programming
language, even though it evolved from Objective-C. It’s a language which many programming paradigms, for example, you can try to use Functional Programming
with Swift.
Swift has many features good for Functional Programming
. Check out first-class functions
and higher-order functions
. You can send a function to another function as parameter or return a function from another one. You can also easily use map
, filter
, and reduce
to deal with arrays.
struct City {
let name: String
let population: Int
}
let paris = City(name: "Paris", population: 2241)
let madrid = City(name: "Madrid", population: 3165)
let amsterdam = City(name: "Amsterdam", population: 827)
let berlin = City(name: "Berlin", population: 3562)
let cities = [paris, madrid, amsterdam, berlin]
extension City {
func cityByScalingPopulation() -> City {
return City(name: name, population: population * 1000)
}
}
let result = cities.filter { $0.population > 1000 }
.map { $0.cityByScalingPopulation() }
.reduce("City: Population") { result, c in
return result + "\n" + "\(c.name): \(c.population)" }
/*
City: Population
Paris: 2241000
Madrid: 3165000
Berlin: 3562000
*/
The code demo above is an excerpt from Functional Swift, which is formatted to output the data with the specific requirements. You’ll find that the code is much shorter and much easier to read than using the traditional method with Object-Oriented Programming
.
Console
These aren’t the only advantages of using Swift. It’s hard to see all of Swift’s perks in one page, but if you really want, I think the best source of info would be the Apple’s official documentation for Swift: The Swift Programming Language.
By the way, Swift is open-source now, so you can find all the information you could want at the Swift website.
I hope you love Swift as much as I do. Have fun, you nerds!!
Tweet us @wiredcraft with your thoughts on Swift!