SwiftUI State | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/17493408-your-first-ios-and-swiftui-app-an-app-from-scratch/lessons/12

Hi!

This is the first theme at this course, that I don’t understand at all. Could you give some links to read more about SwiftUI State.

Sorry if things weren’t quite clear here! Here are a few other resources that may be helpful:

https://www.raywenderlich.com/books/swiftui-by-tutorials/v2.0/chapters/9-state-data-flow

https://www.raywenderlich.com/3967613-swift-ui-working-with-state

Also, we are working on a brand new book that is coming out later this year called the SwiftUI Apprrentice, which will cover all aspects of SwiftUI from a complete beginner perspective, so be sure to stay tuned for that!

self.alertIsVisibile = true

is self just a general practice or preference here ? Coming from Android and we don’t use this in similar cases. Seems to work the same without self so wondering why and/or when we should use it.

@tylerturnbull Good observation! To help make this easier to learn, at the beginning of the course, I show the “long way of doing things” by typing self.xxx. Later, in the video on Type Inference, I show how you can omit this (which is indeed the best practice).

Note for those who may have run into this like I did…

In the video, When you start adding the method that presents the popup window, he says type “alert” when you actually need to type “.alert” and then continue with the rest of the content from the video.

Normally, I would have caught this but at the time I was more listening to his instruction and looking at my screen. Oh well.

If you neglect to add that ever important . before alert, it WILL NOT throw any error messages, but the preview window will time out and when you try to run the simulator, you will get an error starting like this: [Error message: Thread 1: EXC_BAD_ACCESS ]

It’s not obvious, but fixing the missing . resolves all of this.

This was really helpful! I was wondering why preview section was not working, Thanks!

1 Like

It appears for the latest version of swiftui that the format of the alert presented is incorrect. There is no drop down for alerts in the format provided in the video.
Sd be
.alert(isPresented: $alertShown) { () → Alert in
Alert(title: Text(“Alert Title”), message: Text(“Alert Message”), dismissButton: .default(Text(“Ok”)))
}

Captura de Pantalla 2021-10-11 a la(s) 18.32.30

Hi! @rwenderlich could you tell us the new syntax for the .alert method Xcode suggest in using since the one used in the video pops a warning that the method will be deprecated.

@rwenderlich yes cannot figure out the syntax little help here

1 Like

SwiftUI no longer has a dedicated Alert type. Instead, you now create a View for an alert’s actions, and another for its message. There are many ways to get the necessary data to those views. Here’s a way to do it, keeping all the code in one place. (You can directly replace Ray’s alert with this.)

.alert(
  "Hello there!",
  isPresented: $alertIsVisible,
  presenting: {
    let roundedValue = Int(sliderValue.rounded())
    return (
      roundedValue,
      game.points(sliderValue: roundedValue)
    )
  } () as (roundedValue: Int, points: Int)
) { data in
  Button("Awesome!") {
    game.startNewRound(points: data.points)
  }
} message: { data in
  Text("The slider's value is \(data.roundedValue).\n" + "You scored \(data.points) points this round.")
}

@curropalacios

1 Like