CS193p Notes - Lecture 1: Course logistics and introduction to SwiftUI
Tags: ios, swift
Published: 2020-07-26 16:28:27

You can develop apps to iOS, watchOS, etc. but inside them you have templates that are just starting points for certain type of apps.

Language: `Swift`

User Interface: `SwiftUI` (`Storyboard` is the same as UIKit)

- SwiftUI is a new thing and most of the old tutorials talk about UIKit

`ContentView.swift`

- By pressing `resume` you can see what the code looks like in a device. This updates automatically when the code is changed.
- `ContectView_Previews` is not something that's changed often and can be "hide" by adding a lot of line breaks. It just shows the View in the preview and can be deleted without getting any errors.
- `SwiftUI` is imported every time there is something happening in the UI and `foundation` is imported when we do something else because it contains things like arrays, dictionaries, etc.

- `struct` in Swift is more powerful than in some other languages because instead of just storing variables it can contain functions and behaviors.
- `struct ContectView: View` means that `ContectView` behaves/functions like `View`
- `ContentView` is the whole app background area of the device.
- `var` (variable) inside `struct` is called property.
- `var body: some View` where `some View` is type.
    - `some` keyword means that it can be any type as long as it behaves like `View`
- `View` returns one view but `ViewContainer` returns multiple.
- One line function doesn't need `return` keyword.
- `some View` could be `Text` in this example
    - `some View` is better format because then compiler makes the decision and often the return type changes which would also require changing this if not using `some View`

- It's normal to use labels when giving parameters to functions
    - `return RoudedRectangle(cornerRadius: 10.0)`

```swift
struct ContentView: View {
  var body: some View {
    return ZStack(content: {
      RoundedRectangle(cornerRadius: 10.0).stroke().foregroundColor(Color.orange)
      Text("ghost")
    })
  }
}
```

- Some people put `.something` (called View Function) to new line to stand out from other code more easily

```swift
struct ContentView: View {
  var body: some View {
    return ForEach(0..<4, content: { index in  
      ZStack(content: {
        RoundedRectangle(cornerRadius: 10.0).fill(Color.white)
        RoundedRectangle(cornerRadius: 10.0).stroke(lineWidth: 3)
        Text("ghost")
      })
    })
        .padding()
        .foregroundColor(Color.orange)
        .font(Font.largeTitle)
  }
}
```

- `0..<4` is same as `[0,1,2,3]`
- Two `RoundedRectangle` because the other is border and other is the background.
- `ForEach` is not Layout View the same way `ZStack` and that is why it should be inside a Layout View.

- If the last argument of a function is curly brace function it can be written `abc(first: 1) {stuff}` after the function call.
    - If no other arguments `()` can be removed too.


[@RealLankinen](https://twitter.com/reallankinen)

- [**Notes 1**](https://dev.to/lankinen/cs193p-notes-lecture-1-course-logistics-and-introduction-to-swiftui-44ap)
- [Notes 2](https://dev.to/lankinen/cs193p-notes-lecture-2-mvvm-and-the-swift-type-system-406f)
- [Notes 3](https://dev.to/lankinen/cs193p-notes-lecture-3-reactive-ui-protocols-layout-5g8a)
- [Notes 4](https://dev.to/lankinen/cs193p-notes-lecture-4-grid-enum-optionals-4noj)
- [Notes 5](https://dev.to/lankinen/cs193p-notes-lecture-5-viewbuilder-shape-viewmodifier-fgh)
- [Notes 6](https://dev.to/lankinen/cs193p-notes-lecture-6-animation-1a8f)
- [Notes 7](https://dev.to/lankinen/cs193p-notes-lecture-7-multithreading-emojiart-2ak3)
- [Notes 8](https://dev.to/lankinen/cs193p-notes-lecture-8-gestures-json-1f3m)
- [Notes 9](https://dev.to/lankinen/cs193p-notes-lecture-9-data-flow-26e)
- [Notes 10](https://dev.to/lankinen/cs193p-notes-lecture-10-navigation-textfield-3ei9)
- [Notes 11](https://dev.to/lankinen/cs193p-notes-lecture-11-picker-2hgh)
- [Notes 12](https://dev.to/lankinen/cs193p-notes-lecture-12-core-data-eci)
- [Notes 13](https://dev.to/lankinen/cs193p-notes-lecture-13-persistence-2jeh)
- [Notes 14](https://dev.to/lankinen/cs193p-notes-lecture-14-uikit-integration-1p7m)

[Originally published here](https://www.notion.so/lankinen/CS193p-2020-iOS-development-course-from-Stanford-81c094238e9d471bbf059ea8d83b3e15)