Swift For Dummies
Book image
Explore Book Buy On Amazon

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 list explains the major distinctions between the two languages. 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.

Strong typing

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:

switch(choice) {
  case choice1:
    break;
  case choice2:
    break;
}

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. Certain language features can’t be described without reference to the Cocoa and Cocoa Touch 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. 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!

About This Article

This article is from the book:

About the book author:

Jesse Feiler is a developer, consultant, and author specializing in Apple technologies. He is the creator of Minutes Machine for iPad, the meeting management app, and Saranac River Trail and is heard regularly on WAMC Public Radio for the Northeast’s The Roundtable.

This article can be found in the category: