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

3      

What I belive is happening is in your commented out line you're passing in a Date() value and the Text view wants a String. You should be able to use your dateFormatter to convert that Date to a string.

Try changing your commented out Text(item.time) to this;

Text(dateFormatter.string(from: item.time))

Hopefully this helps.

Cheers, Dan O'Leary

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.