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!

1 Like

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

2 Likes

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?

Hi, I tried the bullseye project after upgrading to xcode 11.3.1, and it does not run. Get Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffee523cff0) before even getting to contentView. I backed up, and everything works up to adding the alert.

@pggarland I just tried the final project of this video with Xcode 11.3.1 and it ran OK for me:

Screen Shot 2020-01-27 at 1.29.49 PM

Thanks…not sure what is happening, but I’ll figure it out.

Same syntax error as jdbeltran

Reference to member 'default' cannot be resolved without a contextual type

Hi, I am new to swift UI. Would you mind explaining the below points,

  • Why do we need a boolean put an alert?
    -What is the main function of @State? What it is? I did not understand why we use it at first place.
  • When I ran the project I got this error [Editor placeholder in source file]
    on “”“.alert(isPresented: $alertIsVisible) “””" marking the $ symbol. could you explain this please.

Later I cut copy pasted my program and ran…agn It did work. It must be a bug. 11.3.1v

Why do we need a boolean put an alert?

As Ray mentioned, an alert can either be displayed on the screen, or not. A boolean is a good choice to track this behavior by either being true (displayed to the end user) or false (not displayed).

What is the main function of @State?

The @State property wrapper keeps track of that variable. If that variable changes for any reason, then the view is regenerated. Using UIKit, we’d have to manually update our views, but SwiftUI does it for us when we use @State.

When I ran the project I got this error

You’ll need to paste your code and provide more details.

$ symbol. could you explain this please

The $ represents a binding. When you pass that variable, you are allowing another view to alter the state of the variable. In this case, the binding is being passed to the alert. When the user taps the button to dismiss the alert, the alertIsVisible is set to false, causing your view to rebuild itself to remove the alert from it.

I hope this helps!

Oh. I got it now. Thank you . :smiley:

1 Like

Hi, I am using Xcode12.2, I am having an issue with the pop-up.
import SwiftUI

struct ContentView: View {

@State var alertIsVisible: Bool = false

var body: some View {
    VStack {
        VStack {
            Text("Welcome to my first App!")
                .underline(true, color: Color.blue)
                .foregroundColor(Color.green)
                .fontWeight(.semibold)
        }
        Button(action: {
            print("Button Pressed")
            self.alertIsVisible = true
        })  {
            
           Text ("Hit Me!")
           
        }
        .alert(isPresented: $alertIsVisible) { () ->
            Alert in
            return Alert(title: Text("Hello There!"),
                         message: Text("This is my first pop-up."),
                         dismissButton: .default(label: Text("Awesome")))
        }
    }
}

}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I am not sure what I did wrong as I had to type in the Alert(title… part of the text, there was no autocompletion. What should I do?

Please disregard my last comment. I am using Xcode 12.2 on an M1 Mac, I keep getting the issue saying “Extraneous argument ‘label’ in call” on the line that says dismissButton: .default(label: Text(“Awesome”)))