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

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

Answered by Claude31 in 748995022

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")
            
        }
    }
    
}

Where does the error occur ? Here I guess ?

    @State private var data = Employee.data

You should show Employee definition, but probably you need to change to something like:

    @State private var data = Employee().data

Empoyee() is an instance of Employee, not the type itself.

I tried Employee().data however, It still came up with an error.

Here is the definition of Employee:


import Foundation

struct Employee: Identifiable, Codable {
    let id: UUID
    var firstName: String
    var lastName: String
    var title: String
    var email: String
   
    
    init(id: UUID = UUID(), firstName: String, lastName: String, title: String, email: String) {
        self.id = id
        self.firstName = firstName
        self.lastName = lastName
        self.title = title
        self.email = email
        
    }
}

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"),
        ]
    }

Accepted Answer

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")
            
        }
    }
    
}
Instance member cannot be used on type; did you mean to use a value of this type instead?
 
 
Q