UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: XCode is telling me that a value has no member *name*, but it does.

Forums > SwiftUI

I have created a struct to store data that needs to be read and written to. This is how I've set it up.

struct countdowns: Codable {
    var newEvents: [newEvent]
}

struct newEvent: Codable {
    var date = dateComponent()
    var time = dateComponent()
    var name: String
}

This is the code that is writing to it.

struct NewDateView: View {
    var body: some View {
        VStack {
            Spacer() .frame(minHeight: 6, maxHeight: 6)
            HStack {
                Text("Add New Event")
                    .headerStyle()
            }
            ExtendedDivider()
                .padding(.top, -1.0)
                .frame(height: 0.7)
            TextField("New event.", text: countdowns.newEvents.name)
            Spacer()
        }
        .frame(minWidth: 600, maxWidth: .infinity, minHeight: 500, maxHeight: .infinity)
    }
}

The TextField is returning two errors:

Instance member 'newEvents' cannot be used on type 'countdowns'; did you mean to use a value of this type instead?

Value of type '[newEvent]' has no member 'name'

The one thats particularly stumping me is the second error, as as far as I'm aware, I haven't made any typos or anything that would stop name from being recognised. I'm relatively new to Swift, but I know this should be an easy fix but I can't seem to figure it out. Any help would be greatly appreciated.

Cheers

2      

A few pointers. When using a type you start with a Capital letter eg struct NewEvent and struct Countdowns.

The TextField requires a Binding properties. which is why it can find in scope, you can not just change a property in an array you would need to apend it maybe with a Button or some other method.

I have change you code to show you (note that had to remove some code to make it work for me)

class Countdowns: ObservableObject {
    @Published var newEvents = [NewEvent]()
}

struct NewEvent: Codable {
    var id = UUID()
    var date = Date.now
    var name: String
}
struct ContentView: View {
    @StateObject private var countdowns = Countdowns()
    @State private var name = ""

    var body: some View {
        VStack {
            Spacer()
                .frame(minHeight: 6, maxHeight: 6)

            HStack {
                Text("Add New Event")
            }
            Divider()
                .padding(.top, -1.0)
                .frame(height: 0.7)

            TextField("New event.", text: $name)
                .textFieldStyle(.roundedBorder)
                .padding(.horizontal)

            Button("Add Event") {
                let newEvent = NewEvent(name: name)
                countdowns.newEvents.append(newEvent)
            }
            .buttonStyle(.bordered)

            Spacer()
        }
    }
}

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.