Home Swift
Post
Cancel

Swift

Overview

Remember that SwiftUI code from the previous article?

1
2
3
4
5
6
struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
        .foregroundColor(Color.blue)
    }
}

That’s actually swift code. Pretty advanced swift code, but it’s still swift.

  • View is a protocol
  • The brackets {} are closures
  • "Hello, world!" is a string
  • .blue is a variable

But for now, let’s start simpler and first learn the basics.

Creating variables

Simple syntax like python.

1
2
let string: String = "Hello"
let string = "Hello" /// this also works

Defining functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
func function1() {
  print("Hi")
}

/// This has 1 parameter
func function2(string: String) {
  print(string)
}

/// This has 1 parameter with an external name
func function3(stringToOutput string: String) {
  print(string)
}

/// This has 1 parameter with an external name
func function4(stringToOutput string: String) {
  print(string)
}

Calling functions

1
2
3
function1() /// Result: Hi
function2(string: "Hi") /// Result: Hi
function3(stringToOutput: "Hi") /// Result: Hi
This post is licensed under CC BY 4.0 by the author.

Challenge #1

Basics