Swift is a language announced by Apple Inc. in 2014 for developing iPhone applications. Why has it become the mainstream language in iPhone app development? What kind of grammar do you specifically write? What are the advantages and disadvantages of Swift? We will explain Swift in an easy-to-understand manner for beginner engineers.
table of contents
- 1. [For beginners] What is Swift?
- 2. Features of Swift
- 3. Advantages and disadvantages of Swift
- 4. Swift use case
- 5. [Basic] How to use Swift / Sample code
- 6. Typical frameworks that can be used with Swift
- 7. Summary
1. [For beginners] What is Swift?
What kind of language is Swift, which is often used in iOS application development? This is a simple and easy-to-understand explanation for beginner engineers who want to know why it is mainstream in iOS application development and what kind of grammar it is.
1.1 Apple programming language mainly used for application development for iOS / Mac
Swift is an open source programming language developed by Apple Inc.
It is a language developed mainly for application development for iOS and Mac, and all applications with OS of iOS 7 or later and OS X version 10.9 or later can be developed with Swift.
Until now, a programming language called Objective-C , also developed by Apple Inc., was used for iOS / Mac application development, but it was a complicated and difficult language to learn due to the unique code writing method. .. This language called Swift was born to solve such problems.
1.1.1 iOS
iOS is the familiar OS (operating system) for the iPhone.
It is an OS that is not installed on smartphones other than the iPhone, and it lacks flexibility such as not being able to sideload apps, but it is characterized by a very simple structure compared to Android .
■ Latest OS version: iOS 14 (as of November 2020)
■ Official website: https://www.apple.com/jp/ios/ios-14/
1.1.2 Mac
macOS is an OS for Mac. It has a simple design similar to iOS, and features an intuitive GUI . Unlike Windows, there are few types of OS, and it can be said that it is an OS that is easy for users to handle.
■ Latest OS version: macOS v11.0 Big Sur (as of November 2020)
■ Official website: https://www.apple.com/jp/macos/big-sur/
1.1.3 Apple TV
tvOS is an OS developed for Apple TV (a media streaming terminal sold by apple) in 2015. Since a dedicated App Store has been set up, you can not only watch videos but also install apps.
■ Latest OS version: tvOS14.2 (as of November 2020)
■ Official site: https://www.apple.com/jp/tv/
1.1.4 Apple Watch
watchOS is an OS dedicated to Apple Watch. Developed based on iOS, it features many functions in common with iOS. By pairing the iPhone and Apple Watch, you can also process on the iPhone side and display it on the screen on the Apple Watch.
■ Latest OS version: watchOS 7 (as of November 2020)
■ Official site: https://www.apple.com/jp/watchos/watchos-7/
1.2 Announced at WWDC in 2014
Swift was announced at WWDC (Apple World Wide Developers Conference) * in 2014, replacing Objective-C, which was the development language for OS X and iOS. In fact, the reason why Swift is attracting attention is that it was announced in a “replacement” form. In terms of providing an alternative to Objective-C, Apple has added a variety of support, including Java support in Xcode. However, the birth of the new language Swift had a considerable impact, as all remained to the extent that it was possible to develop in another language while leaving Objective-C. From the beginning, many developers have favorably accepted the language because it is more modern and can be expected to improve performance.
* WWDC: Apple’s annual event for developers in San Francisco
1.2.1 Server-side adoption of Swift is also progressing
The year after it was announced in 2014, Swift became open source and was repeatedly validated for server-side availability by developers around the world. And the first server-side framework to appear is IBM’s Kitura. It was developed with the aim of attracting mobile developers to the cloud, just as Node.js once attracted web-based developers as cloud developers. Currently IBM has announced that it will not develop server-side Swift, but server-side Swift development itself is still widespread in the industry. In recent years, Vapor is often adopted as a server-side framework and is popular.
2. Features of Swift
So what are the specific characteristics of Swift as a programming language?
Let’s compare it with the conventional Objective-C.
2.1 Modern
Code in modern programming languages is easy to write and read . Coding with Swift is interactive and fun. If you use the Playground function of Xcode, which is a development tool, the code will be executed immediately like an imprinter language, and the execution result will be displayed in real time.
In addition, the syntax is very simple, and you can write code comfortably because you don’t need a semicolon at the end of the sentence and type inference is performed. Creating header files (.h files), which was required for class definition in Objective-C, is no longer necessary in Swift.
2.2 Lightweight operation
Swift means “fast and fast” in Japanese, and applications developed with Swift perform well as the name implies .
According to Apple’s official announcement, searching for 10,000 integers in a single graph using a depth-first search algorithm can be processed up to 8.4 times faster than Python 2.7.
2.2.1 Difference in processing speed from Objective-C
When comparing the processing speed under the same conditions as Python earlier, processing is possible up to 2.6 times faster than Objective-C. Since iOS application development handles a large amount of data such as video and audio, Swift can work more comfortably.
However, Swift is not superior in all processing, and Objective-C may be superior in compilation processing speed.
Reference: https://www.apple.com/jp/swift/
2.3 Differences between Swift and Objective-C
First of all, Objective-C and Swift have very different linguistic ideas.
Objective-C is a language that is extended while leaving the description of C language, so it is said to be “macro” or “metalanguage”, and it is a very maniac and special language. Older iOS applications were evaluated for their simplicity and single function, so they were developed in Objective-C, but the application gradually became more complicated, such as the screen becoming larger, and it became simpler. Swift was born because of the demand for fast-moving languages .
3. Advantages and disadvantages of Swift
Next, I will introduce the advantages and disadvantages of Swift.
【merit】
- Low learning cost
Since it is based on modern languages such as Python and Ruby, the grammar is simple and easy for beginners to understand. There are also plenty of tutorials for learning.
- Fast processing speed and light weight
As introduced earlier, it can process 8.4 times faster than Python 2.7 and 2.6 times faster than Objective-C.
- Easy to notice errors
Since it is a compiled language, not a scripting language, you can notice errors at build time.
- Complex operations can be realized
Making full use of the terminal performance of smartphones, complex applications can be executed comfortably.
【Demerit】
- Compile takes a long time
Since it is a compiled language, it takes time to compile. It is not suitable for engineers who want to develop comfortably because the waiting time for work increases.
- Mainly iOS app development
Server-side frameworks have emerged to broaden the scope of Swift development, but their use is still narrow.
- The basic development environment is Mac
Basically, development is done in the development environment called Xcode on Mac. The release of the Swift toolchain image for Windows was announced in September 2020, but it is an environment compared to Mac because it requires preparation of various components together with the toolchain installer and integrated development environment (IDE) “Visual Studio 2019”. The construction cost is a little high.
4. Swift use case
With the advent of Swift, more and more companies are migrating apps made in Objective-C to Swift. Here are some examples of companies that have adopted Swift.
4.1 Mercari
Mercari, known for its flea market apps, is also one of the services that is migrating from Objective-C to Swift. In the JP version of Mercari, instead of rewriting the conventional Objective-C at once, we moved to Swift in the form of partial renewal. We are developing using the A / B test function so that even if a problem occurs after the renewal, we can immediately return to the old screen.
Also, at present, MVVM and Reactive Swift are adopted. In addition, MicroViewController is being considered to keep the complicated screen of JP version Mercari simple.
Reference: From Objective-C to Swift, 4 migration points-Learn the optimal method from practical examples of Mercari | Engineer Hub
4.2 NewsPicks
NewsPicks is a company that provides social economic media “NewsPicks”. The recruitment site “Wantedly” is looking for iOS engineers to support the NewsPicks platform, and lists Swift 5.0 (some Objective-C) as the development language.
Reference: NewsPicks Products | Take your app to the next level! Recruitment of iOS engineers | Wantedly
4.3 Wantedly
Wantedly is a company that provides services such as the job posting site “Wantedly Visit” and the business card management application “Wantedly People”. Wantedly is looking for iOS developers on its job site and lists Swift as the technology stack they are looking for.
Reference: Would you like to develop iOS that is involved in analysis, hypothesis verification, and implementation as well as implementation according to specifications | Wantedly
5. [Basic] How to use Swift / Sample code
From here, for those who are thinking of starting learning Swift, I will introduce the basic sample code of how to write Swift.
5.1 Learn basic grammar
First of all, be sure to keep the following basic grammar in mind.
6. Typical frameworks that can be used with Swift
Frameworks that can be used on the server side are also appearing, so I will introduce some.
6.1 Vapor
Vator is one of the most popular open source web frameworks in Swift. As officially announced, it is designed to be developed very efficiently, inspired by the PHP framework “Laravel” .
The operating environment of Vator is on Ubuntu of MacOS and Linux. A full range of packages such as ORM, JSON, and Validate are installed as standard. It is often used mainly for the development of web applications, APIs, real-time applications, etc.
■ Latest version: v4.x series (as of November 2020)
■ Official site: https://vapor.codes/
6.2 Perfect
Perfect is a web framework released around the same time as Swift was announced. The feature is that it can be used on
both the front end and the back end .
■ Latest version: Version 1.0 (as of November 2020)
■ Official website: https://www.perfect.org/
A framework developed by IBM in 2016. It was gaining popularity with the theme of making development fast, flexible, and easy, but in December 2019, the developer IBM announced that it would stop being involved in server-side Swift development in the future. increase. As of September 2020, Kitura has moved to a community-run project.
7. Summary
With the advent of Swift, which enables complex operations, iOS application development will accelerate further. In addition to iOS application development, server-side Swift development is also progressing steadily. And because it’s a very modern language compared to traditional Objective-C, learning costs are low for beginners. If you are just starting to learn, please try Swift.