100 Days of SwiftUI - Day One is Done!
Tags: swift, ios
Published: 2021-01-03 21:44:22

---
title: 100 Days of SwiftUI - Day One is Done!
published: true
description: 
tags: swift, iOS
cover_image: https://res.cloudinary.com/parttimedeveloper/image/upload/v1609366611/131963506_1122588218212390_1491360064685225119_n.jpg_g2gz2f.jpg
canonical_url: https://parttimedeveloper.dev/blog/2021-01-02-2outof100/
series: 100 Days of SwiftUI
---

This is day 1 out of 100 for 100 days of SwiftUI and day 2 for 100 days of code! I started the 100 days of SwiftUI with a friend so that we would be each others accountability buddy. These are my notes from the course.

You can find my projects and playgrounds for the 100 Days on my [100 Days of SwiftUI GitHub Repo](https://github.com/maeganwilson/100DaysOfSwiftui).

Today's topics were:

- Variables and Constants
- Simple Data Types
- Strings
- Intergers

# Variables

Declare variables with `var`
Variables only need to be declared once and the value can be changed just by using the name

```swift
var str = "Hello, playground" // -> Hello, playground
str = "Goodbye" // -> Goodbye
```

# Strings and integers

Swift is a type safe language, which means that each variable has a specific data type. `str` is a String meaning it holds letters

A number for an age can be stored as an `Int` type. If using large numbers in Swift, you can use an `_` as thousands separators and it will not mess up the number.

``` swift
var age = 38 // this is an integer aka a type of Int
var population = 8_000_000 // _ can be used as a thousands separator
```

#  Multi-line strings

Strings can be declared with double quotes, but by doing that then line breaks cannot be included. For multi-line strings, use triple double quotes, `"""`.

``` swift
var str1 = """
This goes
over multiple
lines
"""
```

![Multi-line is multi-line](https://raw.githubusercontent.com/maeganwilson/100DaysOfSwiftui/master/images/001_multiline.png)

If formatting across multiple lines, but it is not to be outputted that way, then use a `\`.

``` swift
var str2 = """
This goes \
over multiple \
lines
"""
```

![Multi-line is not multi-line](https://raw.githubusercontent.com/maeganwilson/100DaysOfSwiftui/master/images/001_multiline_2.png)

# Doubles and Booleans

`Double`s hold decimal numbers like `3.14` or `1.23456781029`.

If declaring a variable with a fractional number (decimal number), then Swift will declare the variable with a `Double` type.

``` swift
var pi = 3.14 // type = Double
```

`Boolean`s hold either `true` or `false`.

``` swift
var awesome = true // type = Boolean
```

# String interpolation

String interpolation = the ability to put variables into strings

To do this: `\(variable_name)`

``` swift
var score = 85
var str = "Your score was \(score)" // Your score was 85
```

# Constants

Used to set a value that never changes and is declared with `let`

``` swift
let taylor = "swift" // swift
```

Trying to change the value of a constant will cause an error.

![Error for trying to change value](https://github.com/maeganwilson/100DaysOfSwiftui/blob/master/images/001_constants_1.png)

If a variable is declared and the value is not changed, Xcode will produce a warning.

# Type annotations

Swift can infer a type based on what the value assigned to the constant/variable is (type inference).

Sometimes this works, but sometimes being specific would be better or help document what you are expecting the type to be. This is done by `var name: type_name` for a variable or `let name: type_name = value` for a constant.

``` swift
let album: String = "Reputation"
let year: Int = 1989
let height: Double = 1.78
let taylorRocks: Bool = true
```

---

That's it for today! Thanks for reading!