SJ logo

SwiftUI: Getting Started - Weather App - Watch OS app Part 1

10.05.20202 Min Read — In Swift

What is Swift UI

Last year at WWDC ’19, Apple surprised everyone with a brand-new framework called SwiftUI, which allows developers to write apps for Apple platforms using a new declarative Swift syntax, right in Xcode. SwiftUI is built on top of Swift and allows developers to quickly write apps.

You can use the same SwiftUI skills for making an iOS app as you would for making an app on watchOS, tvOS or macOS.

ezgif com-video-to-gif (2)

MVVM Design Pattern

MVVM stands for Model View ViewModel.

Untitled_Artwork 9

Model

Identify objects

If you define the object type for data sending out or returning to app use need to define with type. Let say:

  • you call a GET api and you get data return from the server-side. For this point, we should declare with type Decodable

    Decodable A type that can decode itself from an external representation.

    struct Weather: Decodable {
        var temp: Double?
        var humidity: Double?
    }
  • You call a POST api and you will send data to server to create one more country. For this point, we should declare with type Codable

    Codable A type that can convert itself into and out of an external representation.

    struct Country: Codable {
        let name: String
        let flag: String
    }

    What is the difference between let and var in swift?

    Both var and let are references BUT:

    • let defines a constant variable.
    • var defines an ordinary variable.

View

The view represents the user interface.

SwiftUI provides Generic StructureView Layout so you can use stacks and lists to lay out the views of your user interface.

Stacks

  • HStack
  • VStack
  • ZStack

Lists and Scroll Views

  • List
  • ForEach
  • ScrollView
  • DynamicViewContent
  • etc

Spacers and Dividers

View Model

View models represent the data and the structure behind the view. Handling view's behavior at View Model.

© 2020 by Silicon Jungles. All rights reserved.
Last build: 15.05.2020