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

Add a shift including time(date) and name

Forums > SwiftUI

Need talented people to help!! I am designing to add a shift to this ContentView page, everything is fine, but when I am trying to use the timepicker to export a precise time. It's not working It says 'Initializer 'init(_:)' requires that 'Date' conform to 'StringProtocol''**~~


import SwiftUI

struct ExpenseItem: Identifiable, Codable {
    let id = UUID()
    let name: String
    let client: String
    let time: Date

}
class Classes: ObservableObject {
    @Published var items: [ExpenseItem] {
        didSet {
            let encoder = JSONEncoder()
            if let encoded = try? encoder.encode(items) {
                UserDefaults.standard.set(encoded, forKey: "Items")
            }
        }
    }
    init() {
        if let items = UserDefaults.standard.data(forKey: "Items") {
            let decoder = JSONDecoder()
            if let decoded = try? decoder.decode([ExpenseItem].self, from: items) {
                self.items = decoded
                return
            }
        }

        self.items = []

    }
}
struct ContentView: View {
    @ObservedObject var classes = Classes()
    @State private var showingAddView = false

    var dateFormatter: DateFormatter {
        let formatter = DateFormatter()
        formatter.timeStyle = .short
        return formatter
    }

    var body: some View {
        NavigationView{
            List//ScrollView(.horizontal)
                {
                ForEach(classes.items) { item in
                    HStack {
                        VStack(alignment: .leading) {
                            Text(item.name)
                                .font(.headline)

                            *****//Text(item.time)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*****

                            Text(item.client)

                        }
                        .cornerRadius(10)
                                .overlay(
                        RoundedRectangle(cornerRadius: 10)
                                        .stroke(Color(.sRGB, red: 150/255, green: 150/255, blue: 150/255, opacity: 0.1), lineWidth: 1))

                                .padding([.top, .horizontal])

                    }
                }
            .onDelete(perform: removeItems)
            }
        .navigationBarTitle("iphone")
        .navigationBarItems(trailing:
            Button(action:{
                self.showingAddView = true
            }) {
                Image(systemName: "plus")
            }
            )
                .sheet(isPresented: $showingAddView) {
                    AddView(classes: self.classes)
            }
        }
    }
    func removeItems(at offsets: IndexSet) {
        classes.items.remove(atOffsets: offsets)
    }
}

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

2      

item.time is a Date but Text() takes a String. So convert your Date to a String first.

Something like this: How to format text inside text views

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.