Your First iOS and SwiftUI App · Variables | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4919757-your-first-ios-and-swiftui-app/lessons/19

Having problems with this one. I have copied the code exactly as Ray has written it but it keeps giving me error codes. Please help!

Hi @gladios13! Looking at your screenshot, I’m betting the issue is that you have a newline between “The slider’s value is” and “(roundedValue).”

Swift is finnicky about where you put newlines. The reason why it may look like a newline on my screen is that Xcode wraps lines that go beyond the length of the screen (but it’s not really a newline).

Try deleting that and it should take care of it for you. And don’t forget you can always compare your work to the finished project that you can download on the video.

I hope that helps!

In the example you create a variable to store the rounded value and then add it to the popup like you showed us below.

.alert(isPresented: $alertIsVisible) { () -> Alert in
var roundedValue: Int = Int(self.sliderValue.rounded())
return Alert(title: Text(“Knock Knock Answer!”),
message: Text (“The Slider Value is (roundedValue).”),
dismissButton: .default(Text(“Awesome!”)))
}

Is it bad practice to not store the variable and just add it straight to the message in alert? Example below:

.alert(isPresented: $alertIsVisible) { () -> Alert in
return Alert(title: Text(“Knock Knock Answer!”),
//CHANGE MADE HERE
message: Text (“The Slider Value is ( Int(self.sliderValue.rounded())).”),
dismissButton: .default(Text(“Awesome!”)))
}

@jccolm10 It is always good practice to store the actual value in a variable after all just in case you may need to use it more than once so you do not end up repeating yourself in any way at all. Please let us know if you have any other questions or issues about the whole thing when you get a chance. Thank you!

I get a strange error message although I checked the code more than once and definitely have everything Ray has… Any ideas?

Edit: Sorry, despite having it checked three times, I made a mistake… you should not use self. here…
I am very sorry, my bad.