Group Group Group Group Group Group Group Group Group

Your First iOS and SwiftUI App · SwiftUI State | 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/7

I’m getting a syntax error with the .default method “Reference to member ‘default’ cannot be resolved without contextual type.” I’ve gone over line by line and barring the different strings which shouldn’t affect it everything is the same.

.alert(isPresented: $alertIsVisible) { () ->
Alert in
return Alert(title: Text(“Popped up!”), message: Text(“What’s next?”), dismissButton: .default(Text(“All done”)))
}

I’m coming over with a small Java/C++ background running Xcode 11.1 any help would be great thanks!

I had the same issue. Changing:

.color(Color.green)

to

.foregroundColor(.green)

fixed it.

I’m new to Swift and never saw .alert kind of function. Without “self” or any other class/object before it.

Is it explained anywhere?

Also would like to ask why some functions like print doesn’t have that dot before their name. But this requires them?

Hi all! A few responses to the comments here:

@jdbeltran I tried your code and I think the problem is you might be using smart quotes (“ and ”). But in Swift you should always use normal quotes (").

@lgroves317 Yeah, in previous versions of Xcode it used .color rather than .foregroundColor. However the video shows using .foregroundColor so I think we’re OK.

@711copymachine Good question! This can be a bit confusing when you’re first starting, but here’s what’s going on:

  • Whenever you see a dot before a method (like .alert), that means you’re calling a method on an object.
  • Swift allows you to put newlines between the object you’re calling a method on, and the name of the method, to make things easier to read.
  • So in this case, the object you’re calling the .alert method on is the Button object itself.
  • As for print, that’s an example of a function. A function is like a method, except it’s not attached to any object. We talk about that in Part 1 video 6: https://www.raywenderlich.com/4919757-your-first-ios-and-swiftui-app/lessons/6

If you have any more questions on this let me know as I know it can be confusing when you’re first getting started!

We are setting boolean true on the button click when alert dismiss does isPresented property reverted the state ( true to false)?

I had the same issue as @jdbeltran and got the same error. I tried his code (replacing the smart quotes) and it worked fine. Then I went back and tried my code and it worked. Not a fan of the inconsistency. Maybe my xcode is bugged or something :expressionless:

(Also, thanks for putting out such good content and responding to users. Big fan of that consistency. )

I have the same question as imharshvardhan. How does the alertIsVisible variable get set back to false?
Thanks!

Hello,

I am getting error in loading preview. When i first clicked on resume some pop up came and i clicked on deny even without reading it properly. I think there is a relation for this error with that pop up, some kind of permission? Please see the error below –

Directory/ContentView.2.preview-thunk.dylib, 2): no suitable image found. Did find Directory/ContentView.2.preview-thunk.dylib: open() failed with errno=1)}

Thanks,
Karthika

@dianehoff When the user presses the alert pop-up’s dismiss button it does two things:

  1. makes the pop-up disappear, and
  2. changes alertIsVisible from True to False

Reference: diagram on Page 113 of iOS Apprentice

1 Like

Hi, I have a basic question. In the past, with view controllers, we would put the creation of a UIAlertController and present it in the closure of the button press. Here, the button object (or class) has a method .alert(). This is different to UIKit and strange in a way. Can you explain the runtime logic? Can we call .alert() on Text instead of Button?