Important notice about this site

I graduated from the University of Texas at Austin in December 2020. This blog is archived and no updates will be made to it.

A swift introduction to Swift

July 11, 2020

There's more dad jokes where that came from.

Swift is used primarily for iOS app development, although in a few years this will be true for macOS development as well, with macOS's move from Intel to ARM.

I learned Swift from CS 371L and if you're an undergrad in UTCS, I highly recommend the course as taught by Dr. William Bulko. Any accidental borrowing of phraseology is attributed to that course -- please don't sue me.

Swift is a language described by some as C++ changed to be more like Python. I completely agree with that characterization. It'll be familiar to people who know Java and Python. And if you know both, that's even better; it'll make learning Swift relatively easy for you.

So rather than focusing too much on the fine-grained details, I'll present a block of Java and an equivalent block of Swift for comparison, to highlight some basic differences.

Java:

public class Circle {
    private static final double PI_APPROX = 3.14;
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return this.radius;
    }

    public double getDiameter() {
        return this.radius * 2;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public void setDiameter(double diameter) {
        this.radius = diameter / 2;
    }

    public double getCircumference() {
        return this.radius * 2 * PI_APPROX;
    }

    public boolean sampleOne(String one, String two) {
        System.out.println(one + " and " + two);
        return one == two;
    }

    public static void main(String[] args) {
        Circle circ = new Circle(3);
        System.out.println("Circumference is " + circ.getCircumference());
        System.out.println(sampleOne("yeah", "nah"));
    }
}

Swift:

class Circle {
    let piApprox = 3.14
    var radius: Double?
    var diameter: Double? {
        radius * 2
    }
    var circumference: Double? {
        radius * 2 * piApprox
    }

    init(intendedRadius radius: Double) {
        self.radius = radius
    }

    func sampleOne(outerLabel innerLabel: String, _ noLabel: String) -> Bool {
        print("\(innerLabel) and \(noLabel)")
        return innerLabel == noLabel
    }
}

var circ = Circle(3)
print("Circumference is \(circ.circumference!)")
print(circ.sampleOne(outerLabel: "yeah", "nah"))

Other than differences in syntax, such as the absence of semicolons at the end of lines, some differences stand out, such as the lack of a main method (just type everything in there and it works!), as well as the tendency to manipulate variables directly rather than requiring accessors and mutators (that's the proper term for the slang "getters and setters"). Also, parameters are required to be labeled and they have a caller label and an internal function label too, unless you use the underscore. Variable values can even be derived from other variables; these are called computed properties.

Then, there are the concepts unique to Swift that Java, Python, and C++ all lack. One thing that's a good addition is something called optionals. They're used when a value might not exist yet. In other words, if a variable can be null (Swift calls it nil) then you have to take extra care to deal with it. If a type has a question mark (?) or an exclamation point (!) after it, that means it's an optional and might be nil. A question mark means it's most likely starting off as nil, while an exclamation point means it's likely starting off declared but it could be nil. When using optionals, adding an exclamation point after it means you're force unwrapping it. This is like using a variable in Java knowing that it's possible it's null but you're very sure there's a value in it, and if there happens to not be, you'll receive a null pointer exception. In Swift, there's ways to check to see if it is nil or not, and add failsafes to make sure that you're not going to encounter a pesky null pointer exception. Optionals make this possible.

Optionals in Swift are most like from C#, TypeScript, or Swift's predecessor, Objective-C.

Back to blog

Back to top