Module2: Challenge 9

I decided to give Challenge 9 a try. I think I have most everything correct except for a few things. Right now it is throwing errors on the optional about consecutive declarations on a line must be separated by a semicolon.
Any Advice?

//
//  ContentView.swift
//  Module2Challenge9
//
//  Created by Eric Beecroft on 10/25/21.
//

import SwiftUI

struct ContentView: View {
    @State var myStrings? = [String]()
    var body: some View {
        VStack{
            HStack{
                Button("Set nil"){
                    if(myStrings != nil){
                        myStrings = nil
                    }
                    else{
                        Text("Add data first before clicking set nil button!")
                    }
                }
                Button("Add data"){
                    myStrings.append("Do I work, here?")
                    
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
@State var myStrings? = [String]()

should be:

@State var myStrings: [String]? = [String]()

or

@State var myStrings: [String]? = []

This challenge is deliberately tricky if you are not familiar with Optionals. The intent is to reinforce how to deal with Optionals. You will encounter Optionals in a lot of json code so learning how to deal wth them it pretty important.

The first sentence in the Challenge is:

  • Declare a State property for an array of Strings but make it optional and don’t set any initial value.

With that in mind your @State declaration should be:

@State var myString: [String]?

How you set up the user interface in terms of VStacks and HStacks is really up to you. At the very least you should have two Buttons and then an if statement.

This challenge does feel like it is pretty difficult. I changed it to use the [String]? optional for myString variable. Now I am getting a couple of errors. ‘nil’ cannot be assigned to type ‘[String]’, Cannot use mutating member on immutable value: ‘self’ is immutable, and Value of optional type ‘[String]?’ must be unwrapped to refer to member ‘append’ of wrapped base type ‘[String]’

@roosterboy I changed the variable myString to take [String]? as a parameter.

//
//  ContentView.swift
//  Module2Challenge9
//
//  Created by Eric Beecroft on 10/25/21.
//

import SwiftUI

struct ContentView: View {
    @State var myStrings: [String]?
    var body: some View {
        VStack{
            HStack{
                Button("Set nil"){
                    if(myStrings != nil){
                        myStrings = nil
                    }
                    else{
                        Text("Add data first before clicking set nil button!")
                    }
                }
                Button("Add data"){
                    myStrings.append("Do I work, here?")
                    
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

This is because myStrings has been declared as Optional.

In your Button action closure you need to initialise the array to an empty array and then append the data to it.
ie:

myStrings = []

then

myStrings!.append("Do I work, here?")

NOTE: that myStrings needs to be force unwrapped using the ! because it is Optional and you know that it is safe to do so since you have initialised it.

Also you cannot do this:

                Button("Set nil"){
                    if(myStrings != nil){
                        myStrings = nil
                    }
                    else{
                        Text("Add data first before clicking set nil button!")
                    }
                }

You can’t have an else statement inside a button action closure that attempts to display a Text() view.

Consider this solution using your naming conventions:

struct ContentView: View {
    @State var myStrings: [String]?
    var body: some View {
        VStack{
            HStack{
                Button("Set nil"){
                    if myStrings != nil {
                        myStrings = nil
                    }
                }

                Button("Add data") {
                    myStrings = []
                    myStrings!.append("Do I work, here?")
                    myStrings!.append("Nope!")
                }

            }

            if myStrings != nil {
                List(myStrings!, id: \.self) { item in
                    Text(item)
                }
            } else {
                Text("Add data first before clicking set nil button!")
            }

        }
    }
}

I fixed the statement to now use an array for the add function, but now I am running into 2 errors. Comparing non-optional value of type ‘[String]’ to ‘nil’ always returns true and ‘nil’ cannot be assigned to type ‘[String]’.

//
//  ContentView.swift
//  Module2Challenge9
//
//  Created by Eric Beecroft on 10/25/21.
//

import SwiftUI

struct ContentView: View {
    @State var myStrings: [String]?
    var body: some View {
        VStack{
            HStack{
                Button("Set nil"){
                    if(myStrings != nil){
                        myStrings = nil
                    }
                }
                Button("Add data"){
                    myStrings = []
                    myStrings!.append("Do I work, here?")
                    myStrings!.append("Nope!")
                }
            }
            
            if myStrings != nil{
                List(myStrings!, id: \.self){ item in
                    Text(item)
                }
            }
            else{
                Text("Add data first before clicking set nil button!")
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Using your code I do not see that problem. Try cleaning your build folder Shift + Command + K

Also it is not necessary to check that the myStrings array is not nil before setting it to nil. Just set it to nil.

                Button("Set nil"){
                    myStrings = nil
                }

I didn’t know there was such a command, so I tried it and I received the warning message if statement always returns true. I removed the if statement entirely and everything looks like it is working so far.

Here is the current code view.

//
//  ContentView.swift
//  Module2Challenge9
//
//  Created by Eric Beecroft on 10/25/21.
//

import SwiftUI

struct ContentView: View {
    @State var myStrings: [String]?
    var body: some View {
        VStack{
            HStack{
                Button("Set nil"){
                    myStrings = nil
                }
                Button("Add data"){
                    myStrings = []
                    myStrings!.append("Do I work, here?")
                    myStrings!.append("Nope!")
                }
            }
            
            if myStrings != nil{
                List(myStrings!, id: \.self){ item in
                    Text(item)
                }
            }
            else{
                Text("Query: Information currently empty.\n\n Please click the Add data button!")
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}