Swift: Apple’s New Programming Language For iOS and OS X

swift_header

Apple unveiled a new programming language, called Swift, to its developers during its opening keynote at WWDC. Craig Federighi, Apple’s senior vice president of Software Engineering, introduced the language as “Objective-C without the C”.

Let me explain what he means. Swift has been designed by Apple to be a safe programming language, eliminating entire classes of unsafe code. For example, arrays and integers are checked for overflow and memory is managed automatically.

Swift has been designed to use Apple’s high performance LLVM (Low Level Virtual Machine) compiler, allowing your apps to take full advantage of the device’s hardware. Since Swift uses the LLVM compiler, developers can write apps that use Swift, Objective-C, and C within the same app. The new language from Apple also gains all of the best features from Objective-C and C.

swift_features

The Swift programming language combines the power of Objective-C with the speed and simplicity of a modern scripting language, like Python, allowing your code to be easier to read and maintain. New features like type inferences, closures, generics, multiple return types, and namespaces bring modern programming to Xcode, which come along side other features that makes your code cleaner and less prone to mistakes.

Best of all, you don’t need to type semi-colons at the end of each statement!

To show you how Swift makes programming faster and easier, below is a line of code written in Objective-C followed by the exact same statement in Swift. As you will be able to see, Swift reduces the code from multiple lines down to one line.

Objective-C:

if (myDelegate != nil) {
if ([myDelegate respondsToSelector:
   @selector(scrollViewDidScroll:)]) {
[myDelegate scrollViewDidScroll:myScrollView];
}
}

Swift:

myDelegate?.scrollViewDidScroll?(myScrollView)

Apple has introduced a new feature in Xcode 6 designed specifically for Swift, called Playgrounds. Interactive Playgrounds allow developers to write code and see results instantly without the need for manually compiling and running the code.

Playgrounds is a great way for developers to learn the new language and experiment with Apple’s 4,000 new APIs that are included in iOS 8. The new interactive feature is great for education, allowing teachers and students to learn by doing, which in my opinion is the best way to learn a new programming language.

swift_1Above is a screenshot of Apple’s Balloons example shown on the WWDC stage. Playgrounds allows you to view real time output and even scrub through your program.

In addition, Apple has added a feature they call Read-Eval-Print-Loop (REPL) to the debug console in Xcode. Similar to scripting languages like Python users can now write new code and see how it works in a script-like environment from within Xcode or in Terminal.

repl_swift_terminal

Even though Swift looks to be a major part of Apple’s future, developers should still learn Objective-C because it will not be going away anytime soon.

Swift has been in development by Apple for the past four years. Developers will be able to submit apps for iOS and OS X written in Swift this fall, alongside the releases of iOS 8 and OS X Yosemite

Developers can get started using Swift by downloading the latest Xcode 6 beta from Apple’s developer site. Apple has provided extensive documentation of the language, including a free iBook which is available now as a free download from the iBook Store.

[via Apple]

P.S. - Like our news? Support the site with a coffee/beer. Or shop with our Amazon link. We use affiliate links when possible--thank you for supporting independent media.