Hi there,
I'm still new on learning how to code, so I'm seeking some guidance on this problem. I'm trying to make a page that shows a list of employees, I get an error reading
'Instance member 'data' cannot be used on type 'Employee'; did you mean to use a value of this type instead?'
Below is the code.
// AllEmployees.swift
import SwiftUI
struct AllEmployees: View {
let names = data.firstName
// Error shows on the next line ↓
@State private var data = Employee.data
@State private var searchText = ""
var body: some View {
NavigationStack {
List {
ForEach(data.firstName, id: \.self) { name in
NavigationLink {
Text(data.firstName)
} label: {
Text(name)
}
}
}
.searchable(text: $searchText)
.navigationTitle("All Employees")
}
}
var searchResults: [String] {
if searchText.isEmpty {
return names
} else {
return names.filter { $0.contains(searchText) }
}
}
}
struct AllEmployees_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
AllEmployees()
}
}
}
Any help is appreciated :)
Thanks for the complement.
But your code is very confusing
- because of Data which is essentially employee.
- why do you need the extension ?
What is data supposed to be ?
- is it a single Employee data ?
- if so, what is ForEach(data.firstname) supposed to mean ? You loop on a single name ?
- You declare
let names = data.firstName
What is names supposed to be ?
I had to make a lot of changes to get it compile. Hope that is your code intent.
struct Employee: Identifiable, Codable, Hashable { // <<-- Need Hashable
var id: UUID = UUID()
var firstName: String
var lastName: String
var title: String
var email: String
init(firstName: String, lastName: String, title: String, email: String) {
self.firstName = firstName
self.lastName = lastName
self.title = title
self.email = email
}
}
// Why do you need this extension ? It works without
extension Employee {
struct Data {
var firstName: String = ""
var lastName: String = ""
var title: String = ""
var email: String = ""
}
var data: Data {
Data(firstName: firstName, lastName: lastName, title: title, email: email)
}
mutating func update(from data: Data) {
firstName = data.firstName
lastName = data.lastName
title = data.title
email = data.email
}
init(data: Data) {
id = UUID()
firstName = data.firstName
lastName = data.lastName
title = data.title
email = data.email
}
}
extension Employee {
static let sampleData: [Employee] =
[
Employee(firstName: "John", lastName: "Doe", title: "Chief Executive Officer", email: "johndoe@example.com"),
Employee(firstName: "Jane", lastName: "Doe", title: "Creative Assistant", email: "janedoe@example.com"),
]
}
struct AllEmployees: View {
var names : [String] {
Employee.sampleData.map() {$0.firstName }
}
var searchResults: [String] {
if searchText.isEmpty {
return names
} else {
return names.filter { $0.lowercased().contains(searchText.lowercased()) } // <<-- convert to lowercase recommended
}
}
// No more Error on the next line ↓
@State private var data = Employee.sampleData // <<-- Changed
@State private var searchText = ""
var body: some View {
NavigationStack {
List {
ForEach(searchResults, id: \.self) { name in
NavigationLink {
Text(name)
} label: {
Text(name)
}
}
}
.searchable(text: $searchText)
.navigationTitle("All Employees")
}
}
}