Skip to content

Commit 6126bbd

Browse files
committed
Examples 01-09
1 parent 62f2b45 commit 6126bbd

File tree

10 files changed

+400
-202
lines changed

10 files changed

+400
-202
lines changed

contents/[01]the-print-function/index.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,35 @@ description: ""
44
order: 1
55
---
66

7-
The print function displays text or values on the screen. In the simplest form it means we can display messages on the command line. Create a new file named `main.swift` and place the following Swift code inside the file:
7+
The print function displays text or values on the screen.
88

9-
```swift
10-
// FILE: main.swift
9+
We can display messages on the command line using print, such as the famous "Hello, World!" text. To try it out, create a new file named main.swift and place the following Swift code inside:
1110

11+
```swift
1212
print("Hello, World!")
13-
14-
/**
15-
Run Swift files using the command line:
16-
17-
$ swift main.swift
18-
> Hello, World!
19-
*/
2013
```
2114

22-
Run the program via the `swift main.swift` command, it will display the `Hello, World!` message.
15+
The Swift toolchain is the complete set of tools — including the compiler, standard library, debugger, and package manager — that enables building, running, and testing Swift programs across platforms.
16+
17+
The `swift` command is included in the Swift toolchain. By running your program with the `swift` command, the compiler executes the code and displays the classic "Hello, World!" message:
2318

24-
---
2519

2620
```sh
27-
# run the swift file
2821
swift main.swift
2922
# Hello, world!
23+
```
24+
25+
The Swift compiler is part of the Swift toolchain, and the `swiftc` command is used to compile Swift source files into executable binaries. You can start the compilation process by running the following command:
3026

31-
# build the program
27+
```sh
3228
swiftc -o app main.swift
33-
# run the program
29+
```
30+
31+
The `swiftc` command generates an `app` binary, which you can then execute directly from the command line like this:
32+
33+
```sh
3434
./app
3535
# Hello, world!
36-
```
36+
```
37+
Now that we can build and run basic Swift programs, it’s time to dive deeper into the language and explore its core features.
38+

contents/[02]constants-and-variables/index.md

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,56 @@ description: ""
44
order: 2
55
---
66

7-
foo
7+
A **constant** is a named container in memory that holds a value which cannot be changed after it is set.
8+
9+
Constants are used to store information that remains the same throughout the program's execution. Because their values are fixed, constants are also referred to as immutable variables.
10+
11+
You can define a constant by using the `let` keyword. Then you choose the name you wish, finally assign the value of the constant after an equal `=` sign (assignment operator):
812

913
```swift
10-
// a constant
11-
let name: String = "John"
12-
print(name) // => John
13-
14-
name = "Bob" // => ERROR
15-
// Cannot assign to value:
16-
// 'name' is a 'let' constant
17-
18-
// a variable
19-
var age: Int = 42
20-
age = 69
21-
print(age) // => 69
14+
/// firstName is a constant, it can never change
15+
let firstName = "Kitti"
2216
```
2317

24-
bar
18+
If we try to assign another value to an existing constant, we get a compile-time error:
19+
20+
```swift
21+
let firstName = "Kitti"
22+
23+
/// Cannot assign to value: 'firstName' is a 'let' constant
24+
firstName = "Tibor"
25+
```
26+
27+
A **variable** is a named container in memory that stores a value which can change during program execution.
28+
29+
The value of a variable can change. It is used to store and update information as needed. We also refer to variables as mutable variables.
30+
31+
You can define a variable using the `var` keyword:
32+
33+
```swift
34+
/// We assign the value "Takács" to the lastName variable
35+
var lastName = "Takács"
36+
```
37+
38+
Since `lastName` is a variable, it's possible to change its value:
39+
40+
```swift
41+
var lastName = "Takács"
42+
43+
/// we change the value of the lastName variable to "Bödecs"
44+
lastName = "Bödecs"
45+
```
46+
47+
Here's the complete example including a constant and a variable:
48+
49+
```swift
50+
let firstName = "Kitti"
51+
var lastName = "Takács"
52+
53+
lastName = "Bödecs"
2554

26-
```sh
27-
swift main.swift
55+
/// print out the full name
56+
print(firstName, lastName)
2857
```
2958

30-
baz
59+
In the previous example, we declared only string constants and variables. Now, let's explore other common data types available in the Swift programming language.

contents/[03]standard-types/index.md

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,48 @@ order: 3
55
---
66

77

8-
foo
8+
A `String` type is a sequence of characters used to represent text in programming. It can include letters, numbers, symbols, spaces — and even emojis.
99

1010
```swift
11-
let b: Bool = false
11+
let firstName: String = "Kitti"
12+
var lastName = "Bödecs"
1213

13-
let i: Int = 6
14+
print(firstName, lastName)
15+
```
1416

15-
let f: Float = 4.20
17+
A `Bool` (Boolean) data type represents two possible values: `true` or `false`. It's used to handle logical decisions in programming.
1618

17-
let d: Double = 6.9
19+
```swift
20+
let isDeveloper: Bool = true
21+
print(isDeveloper)
1822

19-
let s: String = "Lorem ipsum"
23+
var isItMonday = false
24+
print(isItMonday)
2025
```
2126

22-
bar
27+
An `Int` type (short for integer) stores whole numbers, either positive or negative. It is also possible to perform mathematical operations by using Int types:
28+
29+
```swift
30+
let x: Int = 12
31+
let y = 3
2332

24-
```sh
25-
swift main.swift
33+
print(x, y)
2634
```
2735

28-
baz
36+
A `Double` data type is used to store numbers with decimal points, allowing for more precision than an `Int`. It is commonly used for calculations requiring fractional values.
37+
38+
```swift
39+
let pi: Double = 3.14
40+
print(pi)
41+
42+
let mph = 0.621371
43+
print(mph)
44+
```
45+
46+
Swift is smart enough to automatically figure out the type of a variable or constant without explicitly writing it. This is called _type inference_.
47+
48+
Type inference works with `Bool`, `Int`, `Double` and `String` types.
49+
50+
Next, let's explore string operations in Swift.
51+
52+

contents/[04]string-operations/index.md

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,75 @@ description: ""
44
order: 4
55
---
66

7+
The plus sign (addition operator) works on `String` types to join multiple strings together. This is often called as string concatenation:
78

8-
foo
9+
```swift
10+
let firstName = "Kitti"
11+
let lastName = "Bödecs"
12+
13+
let fulName = firstName + " " + lastName
14+
print(fulName)
15+
```
16+
17+
Use the `\()` syntax to insert values into a String. Interpolation works with any data type, such as `Int` or `Double`.
18+
19+
```swift
20+
var x = 12
21+
var y = 6
22+
let pi = 3.14
23+
let name = "Kitti"
24+
25+
print("Hello, my name is \(name).")
26+
print("The coordinates are x: \(x) and y: \(y)")
27+
print("Pi is always \(pi).")
28+
```
29+
30+
Use triple quotes `"""` to define a string that spans multiple lines:
931

1032
```swift
11-
let name = "John"
12-
let age = 42
13-
let end = "years old."
33+
let lyrics = """
34+
You belong
35+
with me.
36+
"""
1437

15-
// String interpolation
16-
print("\(name) is \(age) \(end).")
17-
// => John is 42 years old.
38+
print(lyrics)
1839
```
1940

20-
bar
41+
When using multiline string literals, the closing triple quote `"""` must align with the start of the content. Whitespace is preserved exactly as written when using multiline strings.
42+
43+
You can check if a string is empty by using the `isEmpty` property. The `count` property returns the number of characters. It is also possible to turn a string into a lowercase or uppercase string:
44+
2145

22-
```sh
23-
swift main.swift
46+
```swift
47+
let helloWorld = "Hello, World!"
48+
49+
print(helloWorld.isEmpty)
50+
print(helloWorld.count)
51+
print(helloWorld.uppercased())
52+
print(helloWorld.lowercased())
2453
```
2554

26-
baz
55+
To include double quotes (or other special characters) inside a string, use the backslash `\` to escape them:
56+
57+
```swift
58+
let quote = "He said: \"Hello!\"."
59+
60+
print(quote)
61+
```
62+
63+
If you prefer to avoid escape characters, you can use raw string literals. Raw strings are defined using the `#` symbol before and after the quotes, allowing you to include special characters like double quotes without needing to escape them:
64+
65+
```swift
66+
let quote = #"He said: "Hello!"."#
67+
print(quote)
68+
```
69+
70+
You can also use string interpolation with raw strings. To do this, add a `#` after the backslash, like `\#(value)`, inside the raw string literal:
71+
72+
```swift
73+
let name = "Kitti"
74+
print(#"Hello, my name is \#(name)."#)
75+
```
76+
77+
You may use multiple `#` delimiter characters; if you do, you must use the corresponding number of characters when interpolating.
78+

0 commit comments

Comments
 (0)