Swift For Dummies (2015)
Part V. The Part of Tens
Chapter 21. Ten Swift Features That Are Not in C
Considering that Swift is a newly developed, object-oriented language, and that C was developed almost half a century ago (by Dennis Ritchie in 1969-1973), when the object-oriented paradigm wasn’t even in widespread use, you might think that comparing these two languages would be difficult and, in many ways, unfair to both languages.
That's one way of looking at things, but another way is to look at C’s impact on modern languages. C is still taught in computer-science courses in a variety of programs from primary schools to post-graduate courses. C is still being taught because it is still one of the most widely used languages. C may not be cutting-edge, but it has served a major role in the development of today's software and the people who design and develop it.
For those who are familiar with C and now learning Swift, this chapter explains the major distinctions between the two languages. I take for granted that you know that Swift has been developed for an environment that includes large and small computers (that is, Macs and mobile iOS devices) whereas C was developed for minicomputers and mainframes.
With that out of the way, here are ten Swift features that are not in C.
Swift is much more strongly typed than C. If you want to cast a value to another type, you have to do it rather than rely on it being done for you automatically. For example, consider this code:
var x = 4
var y = 4.0
var z = x + y
The last line produces an error because you can't add an Int and a Double.
However, either of the following two lines will work:
var z = x + Int(y)
var z = Double (x) + y
Swift's typing is so strict, that even the following line will not work:
var z = Float(x) + y
Swift infers the type Double for the value 4.0 unless you explicitly assign it the Float type when using it in an operation or when declaring it, as in the following line of code:
var y :Float = 4
If you're not used to a strongly typed language, this can take some getting used to.
Libraries Extend C
Libraries are the primary way of extending C, but you can extend Swift with libraries (in addition to the built-in Swift standard library) as well as with frameworks, classes and subclasses, extensions to classes, structures, and enumerations.
Switch Statements Fall through Cases in C
You can sometimes tell C programmers are writing Swift code by their switch statements. In C, a switch statement consists of case elements, as in the following:
Without the break statements, control passes through to the next case. This doesn't happen in Swift. There is some C-style code that takes advantage of the fact that without break statements, control passes to the next case statement(s). That has always been considered dubious programming style, but now it won't happen in Swift.
C Is an International Standard
C is an international standard (specifically ISO/IEC 9899:201x), and Swift is not. Whether or not this matters is up to you. A number of people (including the author) think that this is pretty much irrelevant at this time. Languages that aren’t governed by international standards organizations can sometimes evolve more quickly, and, if such standards become necessary, they can be added later. International standards don’t have much of a role to play in the initial development process of a language, when it’s more important to get as many people as possible to use the language. This is the stage Swift is in now.
Swift Is Tightly Linked to the Cocoa and Cocoa Touch Frameworks
In fact, it’s hard to tell where Swift leaves off and the frameworks take over. Even though this book focuses on the language, I’ve included examples that use the Cocoa and Cocoa Touch frameworks because certain language features can’t be described without reference to these frameworks.
Obviously, this is not the case with C.
Swift Includes Memory Management
Automatic Reference Counting (ARC) is built into Swift. In C, memory management is the developer's task.
Swift Is Designed to Function in a Multi-Threaded Environment
In Swift, certain language features, such as closures, are designed to support multi-threaded environments in which a number of different tasks may be executed simultaneously on several multiple-cored processors. Language features such as closures are specifically designed for the multi-threaded environment found on Macs and iOS devices.
Types Can Be Created Easily in Swift
Even in comparison with Objective-C, which in many ways is Swift’s closest predecessor, Swift’s ability to create new types (sometimes alongside or instead of custom classes) distinguishes it from all other languages, including C.
Swift Has Its Own IDE and Compiler
Swift is designed to be used with its own IDE (Xcode 6 or later) and its own compiler (LLVM). There is no apparent reason why another IDE couldn't be used and another compiler written, but at the moment, neither of these appear to be in the works, and developers seem to be generally content with the existing tools. (I say “generally” because developers are developers and envisioning different futures is part of the job description.) C and other languages are not tightly integrated with an IDE and compiler.
Types Can Be Classes, Structures, or Enumerations
In Swift, types can be classes, structures, or enumerations, and each of them can have properties and methods. Swift’s properties and methods are not just for classes. This sure isn't the case in C!