# Your First iOS and SwiftUI App · SwiftUI State | raywenderlich.com

**URL:** <https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920>\
**Category:** 🎓 Courses & Articles\
**Created:** [September 5, 2019, 6:21am UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920 "2019-09-05T06:21:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rwenderlich](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/rwenderlich/32/108604_2.png) [@rwenderlich](https://forums.kodeco.com/u/rwenderlich)\
**Post date:** [September 5, 2019, 6:21am UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/1 "2019-09-05T06:21:36Z")

</div>

* * *
This is a companion discussion topic for the original entry at [https://www.raywenderlich.com/4919757-your-first-ios-and-swiftui-app/lessons/7](https://www.raywenderlich.com/4919757-your-first-ios-and-swiftui-app/lessons/7)

---

<div class="post-metadata">

**Author:** ![jdbeltran](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jdbeltran/32/81494_2.png) [@jdbeltran](https://forums.kodeco.com/u/jdbeltran)\
**Post date:** [October 9, 2019, 8:58am UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/2 "2019-10-09T08:58:20Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![lgroves317](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lgroves317/32/102075_2.png) [@lgroves317](https://forums.kodeco.com/u/lgroves317)\
**Post date:** [October 14, 2019, 2:32am UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/3 "2019-10-14T02:32:17Z")

</div>

I had the same issue. Changing:

.color(Color.green)

to

.foregroundColor(.green)

fixed it.

---

<div class="post-metadata">

**Author:** ![711copymachine](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/711copymachine/32/86017_2.png) [@711copymachine](https://forums.kodeco.com/u/711copymachine)\
**Post date:** [October 20, 2019, 1:13pm UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/4 "2019-10-20T13:13:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rwenderlich](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/rwenderlich/32/108604_2.png) [@rwenderlich](https://forums.kodeco.com/u/rwenderlich)\
**Post date:** [October 30, 2019, 3:30pm UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/5 "2019-10-30T15:30:58Z")

</div>

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](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!

---

<div class="post-metadata">

**Author:** ![imharshvardhan](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/imharshvardhan/32/102382_2.png) [@imharshvardhan](https://forums.kodeco.com/u/imharshvardhan)\
**Post date:** [December 12, 2019, 3:16pm UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/7 "2019-12-12T15:16:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![qassmord](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/qassmord/32/81494_2.png) [@qassmord](https://forums.kodeco.com/u/qassmord)\
**Post date:** [December 12, 2019, 6:37pm UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/8 "2019-12-12T18:37:26Z")

</div>

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 😑

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

---

<div class="post-metadata">

**Author:** ![dianehoff](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/dianehoff/32/81494_2.png) [@dianehoff](https://forums.kodeco.com/u/dianehoff)\
**Post date:** [December 17, 2019, 7:53pm UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/9 "2019-12-17T19:53:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![karthika\_s\_das](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/karthika_s_das/32/81494_2.png) [@karthika\_s\_das](https://forums.kodeco.com/u/karthika_s_das)\
**Post date:** [December 22, 2019, 12:11pm UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/10 "2019-12-22T12:11:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jgarr16](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jgarr16/32/81494_2.png) [@jgarr16](https://forums.kodeco.com/u/jgarr16)\
**Post date:** [December 24, 2019, 3:51am UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/11 "2019-12-24T03:51:39Z")

</div>

@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_

---

<div class="post-metadata">

**Author:** ![blakninja](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/blakninja/32/102075_2.png) [@blakninja](https://forums.kodeco.com/u/blakninja)\
**Post date:** [December 29, 2019, 10:44pm UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/12 "2019-12-29T22:44:58Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pggarland](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/pggarland/32/81494_2.png) [@pggarland](https://forums.kodeco.com/u/pggarland)\
**Post date:** [January 27, 2020, 3:12am UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/13 "2020-01-27T03:12:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rwenderlich](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/rwenderlich/32/108604_2.png) [@rwenderlich](https://forums.kodeco.com/u/rwenderlich)\
**Post date:** [January 27, 2020, 6:31pm UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/14 "2020-01-27T18:31:33Z")

</div>

@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](https://files.chunter.kodeco.com/original/3X/b/1/b1a1c01543467f6868780a4ea50b3b373912a1d1.png)

---

<div class="post-metadata">

**Author:** ![pggarland](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/pggarland/32/81494_2.png) [@pggarland](https://forums.kodeco.com/u/pggarland)\
**Post date:** [January 28, 2020, 12:06am UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/15 "2020-01-28T00:06:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![13en](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/13en/32/81494_2.png) [@13en](https://forums.kodeco.com/u/13en)\
**Post date:** [February 9, 2020, 12:37am UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/16 "2020-02-09T00:37:46Z")

</div>

Same syntax error as jdbeltran

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

```

---

<div class="post-metadata">

**Author:** ![jbollu20](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jbollu20/32/81494_2.png) [@jbollu20](https://forums.kodeco.com/u/jbollu20)\
**Post date:** [March 16, 2020, 6:13am UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/18 "2020-03-16T06:13:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bdmoakley](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bdmoakley/32/108772_2.png) [@bdmoakley](https://forums.kodeco.com/u/bdmoakley)\
**Post date:** [March 16, 2020, 5:21pm UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/19 "2020-03-16T17:21:09Z")

</div>

_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!

---

<div class="post-metadata">

**Author:** ![jbollu20](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jbollu20/32/81494_2.png) [@jbollu20](https://forums.kodeco.com/u/jbollu20)\
**Post date:** [March 16, 2020, 5:41pm UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/20 "2020-03-16T17:41:19Z")

</div>

Oh. I got it now. Thank you . 😃

---

<div class="post-metadata">

**Author:** ![idontn01](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/idontn01/32/81494_2.png) [@idontn01](https://forums.kodeco.com/u/idontn01)\
**Post date:** [December 17, 2020, 4:50am UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/21 "2020-12-17T04:50:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![idontn01](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/idontn01/32/81494_2.png) [@idontn01](https://forums.kodeco.com/u/idontn01)\
**Post date:** [December 17, 2020, 4:58am UTC](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920/22 "2020-12-17T04:58:00Z")

</div>

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”)))

[Next page](https://forums.kodeco.com/t/your-first-ios-and-swiftui-app-swiftui-state-raywenderlich-com/80920.md?page=2)
