DEV Community

Cover image for Diving into SWIFT
Avery Berkowitz
Avery Berkowitz

Posted on

Diving into SWIFT

After spending the last month working on a native application in React Native, I figured it would be interesting to dive deeper into mobile development and learn a little bit more about Apple's very own programming language: Swift. In this post, I will explore why Swift has swiftly (sorry, had to do that once) become the most popular language for iOS development and dive into some of the basics including:

  • Variables
  • Collections
  • Functions

What is Swift?

Swift is a programming language for the suite of apple devices first released in 2014. It is meant to be a higher-level, more modern take on Objective-C and has certainly lived up to expectations by becoming the most popular language for iOS development by 2018. It is open source and has a comprehensive, 500+ page manual available for free download here.

Why learn Swift?

Well, if you like Apple products (I shamelessly do), Swift is the way to go. As previously mentioned, it is currently the most popular programming language across apple devices and its popularity is continuing to rise. Check out Swift's position on the 2019 developers survey from stack overflow:
Alt Text
If you are hunting for jobs, Swift isn't so bad either. A quick search on ZipRecruiter currently shows 3336 open positions posted across the country (as of today, Dec. 14, 2019) posted within the last 30 days.

Lastly, Swift is a language that isn't too hard to pick up if you are already familiar with another language or two (I am coming from a JavaScript and Python background). Let's see some Swift basics:

Variables in Swift

Running example code: for simplicity, I recommend trying out repl.it's Swift compiler

Variables can be declared with either the var or let keyword. If the value of the variable is going to be reassigned, var should be used.
Constants (variables that will only be assigned once) should use the let keyword (like const in Javascript). Variables must be declared before use!

var changeMe = 10
let dontChangeMe = 99

If we want to log values to the console, we can use the built in print function.

print(changeMe)
// ==> 10

// are used for inline comments and we do not need to use ; to end expressions! Nice.

Most of the time, we may want to specify type when defining variables. This can be really helpful to catch bugs as your code base grows. To specify type, we add a : followed by the datatype. An error will be thrown if these variables are not assigned to the specified datatype later during code execution.

// explicit variable declaration and assignment
var firstName: String = "Avery"

Reassigning firstName to another data type later will throw an error.

Collections

Swift keeps it simple, with three major types of collections: Arrays, Sets, and Dictionaries.

  • Arrays are indexed collections of data, comma separated within []. They can be made immutable (into a tuple) simply be declaring them with the let keyword.
var mutableArray: [Int] = [1, 2, 3, 4]

let tuple: [String] = ["Can't", "Change", "Me"]

Notice we can explicitly set the type of both the Array and its contents after the colon.

  • Sets are unindexed collections. They look really similar to arrays, except if iterated over, the order will be random. These are good for storing data where order doesn't matter.
var myCats: Set<String> = ["Frank", "Hodor", "Buddy"]
  • Dictionaries are collections of key-value pairs. They also look pretty familiar to sets and arrays.
var me: [String: String] = ["Name": "Avery", "Age": "32", "City": "New Orleans"]

Unlike with javascript objects, we can use the data type of our choice for our keys!

Functions

We define functions in Swift with the func keyword. We must also make some decisions about what datatypes our arguments will be, as well as if our function should return a value.

Functions with no return value

func sayHello (name: String) {
  print("hello, \(name)")
}

sayHello(name: "Avery")

// ==> "Hello, Avery"

So a few things here:

  • We name our function sayHello and specify that it is expecting a variable with a type of String when it is invoked. Our print statement shows of Swift string interpolation as \(name) will be replaced by the function's argument when it is invoked hence the log of Hello, Avery, if you run this code in your compiler. ####Functions with return value
func sayHello (name: String) -> String {
  return "hello, \(name)"
}

print(sayHello(name: "Avery"))

Notice we add a -> (arrow) and specify that our return value will be of data type String

I hope you have enjoyed this brief intro into Swift. Clearly, I have just scratched the surface for what Swift is and can do. For more, I encourage you to check out the official docs here.

Top comments (0)