Apple’s Swift Programming Language – a Primer
Swift is best known as Apple’s new language for iOS and macOS (formerly OS X) software development. It’s a good general-purpose language, though, and well-suited for low-level systems programming. Apple has made its compiler and libraries open source, and it has created a website just for Swift. It could compete with Rust, Go, and other new-generation languages for memory-safe system programming.
The syntax has a family resemblance to C, but with important differences. Semicolons are optional where C requires them, and braces are required where C doesn’t. What’s really distinctive about Swift, though, is the way it discourages common programming blunders.
Null pointers or references are a big source of programming bugs. If a variable can have a null value, every line of code that handles it has to consider that possibility. Forgetting just once can cause an error that stops execution cold. Swift handles this by disallowing nil values by default, but allowing declaration of “optionals” which can have nil values. The type String can’t be nil, but the optional type String? can be. After the code checks for nil, it can assign an optional to a normal variable.
Other examples of blunder prevention include not allowing an assignment to return a value (so “if x = y” isn’t valid code) and not falling through to the next case of a switch statement.
Swift makes a strong distinction between constant and variable data. The keyword let declares a named constant, and var declares a variable. The way some data types are treated by reference and others by value makes this a bit confusing, though. A class and a struct are very similar things, but a class is a reference type, and a struct is a value type. This means you can modify the variables in a constant class, but not in a constant struct.
Apple has ported Swift to Linux, showing its seriousness about the language as more than a tool for Apple developers. The language is still in flux; Swift 3 has some incompatible differences with Swift 2. Even so, it looks like a language with a bright future.
I can help to connect you with development teams that know the latest software technologies. Please contact me for more information.