Why non-Apple developers should care about Swift

Why non-Apple developers should care about Swift

[51CTO.com Quick Translation] With the ability to support Linux after being open source, Swift has become a powerful programming language option that cannot be ignored by cross-platform and server-side developers.

Just last week, Apple announced a commitment: its Swift language is now officially open source. As a programming language that was just released a year ago, Swift is part of the C language family and is designed to achieve general application development tasks. When Apple first released Swift, it was mainly aimed at new desktop and mobile application development, and used the LLVM compiler. Swift draws on the ideas of many modern language design solutions and combines the complexity and depth of C with the ease of use of interpreted languages ​​such as Python. In addition, it runs very fast and can smoothly interface with large numbers and collections.

Swift's open source release will continue to develop, adding Linux support and server-side language features. Swift is already on the fast track: in its release year, Swift has been upgraded to version 2.2, and it also adds LLVM and clang compiler docking capabilities. Swift is not available for Windows yet, but Microsoft has added Swift support for iOS and OS X applications in its latest Visual Studio version.

If you have used Swift on OS X, you will find that the way Swift is used in Xcode is very similar to the development environment that Objective-C developers have been familiar with for many years, and the two languages ​​can use the same compiler. Apple even directly describes Swift as "Objective-C without C elements."

That’s because Swift is an abstraction of Objective-C that uses the same underlying LLVM compiler. In fact, Swift is so similar to Objective-C that you could describe it as a new Objective-C language that simplifies certain key constructs and makes the code more readable. Swift builds on the syntactic foundation of Objective-C, while adding modern constructs to simplify the language and help developers who are used to creating iOS or OS X apps in JavaScript or C# to get started. These developers will find a lot of familiar features, including Swift’s control flow handling mechanism and how to use functions.

The commonality between Swift and Objective-C simplifies the process of updating apps to the new language. You can mix and match Swift and Objective-C code in the same app, allowing existing libraries to be reused in new apps. If you move from one language to the other, you don’t have to rewrite the code that you need to reuse—unless you deliberately do so.

Apple pays great attention to the principle of safety in language design, and one of the core features of Swift is that it supports type inference. In this way, although Swift is not strongly typed, it still has type safety. Specifically, if you fail to declare the type of a variable, but assign a value to the variable that has been declared, Swift can automatically infer its declared type. In this case, our code will be safer - that is, the risk of crashing is reduced. Swift also uses its own memory management mechanism to restrict access to pointers.

Type safety also means that you will never pass an int to a string; the Swift compiler will always flag this as an error. There is a guard statement that prevents a piece of code from executing if the value is nil, which allows us to protect our code from common user and API errors (nil usually causes compiler-time errors). Alternatively, you can use optionals to allow a specific variable to be assigned null regardless of its actual type.

Writing Swift apps is easy: you just start. There is no need to set up a main function or import references. This approach is designed to make your code more accessible. Although the user interface of OS X or iOS is currently handled by libraries, you can still develop the core of your app before setting up its look and feel.

A key element of Swift is what Apple calls "protocol-oriented programming." Protocols are a key way to access other languages, using class-specific methods so that you can use defined methods in any class with the help of that protocol. Protocols also allow you to extend any class—including core language elements—without editing its source code. This means you can use all kinds of third-party code and libraries at the same time.

Another big advantage: you can use a whole set of protocols to help your development team implement coding standards. The protocol itself is also an extremely powerful tool and we can use it as a key factor in our development strategy.

One of the most important innovations of Swift is the addition of Playgrounds to Xcode. Apple is using Xcode and Swift to get the LLVM compiler into the hands of more developers and to make sure that users can see the code function directly within the IDE. Playgrounds allow developers to quickly try out the functionality of a code snippet to see if it works as expected without having to switch outside of the IDE. You don't have to compile the code and then run its build or debugging tools; everything can be verified immediately after the code is written. This approach undoubtedly greatly encourages developers to try new things and makes coding a more entertaining experience.

Bringing these features to other platforms is an equally important step. Programming languages ​​need to be available to developers on any platform, from client to server to cloud, and a consistent language that spans all elements of the modern stack is the core role in this development scenario. However, it should be emphasized that the Playgrounds feature in the Swift compiler cannot be enjoyed on the Linux platform at present.

This lack of functionality makes sense, though: as a development environment primarily for server applications, you probably don't need this kind of quick function visualization in Linux - here, we can just pipe the output to the console. Apple provides a REPL-based debugging tool in its Linux toolset that allows you to quickly run code interactively, so you can use the REPL (read-evaluate-print-loop) tool as an expression evaluation mechanism.

With the source code now officially available on GitHub, anyone can now fork the original Swift library, experiment with the code, and pull the results into Swift as contributed code. The most important element is the Swift core library, which promises to provide options to extend Apple's development frameworks to other platforms. This means that we only need to build Swift code once and deliver it to multiple platforms, and developers will have another set of alternatives to Microsoft's open source .Net - in addition, it also allows cross-platform tool vendors such as Xamarin to support a wider group of developers.

Original title: Why non-Apple developers should care about Swift

[Exclusively translated by 51CTO.com. Please indicate the source when reprinting on partner sites]

<<:  Google's Utopia: Which World Does the Future Belong to?

>>:  Microsoft withdraws first update of Windows 10 Mobile due to numerous bugs

Recommend

5 marketing tips to make users fall in love with your promotions

Have you ever been troubled by the fact that no o...

Today, to all the parents at home! Please share "A loving family"

Expert in this article: Yang Jing, PhD in Immunol...

Teach you how to write hit copy with zero basic knowledge!

My sharing this time is mainly divided into 2 par...

How to deal with this skin disease that sounds like a plant name?

Urticaria, a disease name that sounds like a plan...

The fission gameplay of Weibo traffic diversion!

I have been in the Internet circle for 6 years an...

Yutu-2 discovers mysterious cabin on the moon

October 29, 2021, afternoon "Yutu-2" fr...

What adjustments can be made if advertising volume is not reaching the target?

01. The advertising volume is not satisfactory, a...

How to build a personal brand to increase traffic?

1. Why should we build a personal brand? Many peop...

Acer Swift S5-371 review: All-round upgrade of the ultimate spirit

When it comes to thin and light notebooks, the &q...