How to run DateFormatter() in Swift View?

  Kiến thức lập trình

I’m very new to Swift. My question is, I’m putting DateFormatter()in the ContentView and it’s not working and I get Type ‘()’ cannot conform to ‘View’. The DateComponents() and Calendar objects work fine though. What should I do? Here’s the code snippet:

import SwiftUI

struct ContentView: View {
    
    @State var birthdate = Date()
    var gregorianComponents = DateComponents()            // This line works fine  
    var formatter = DateFormatter()                       // This line is the problem!
    let chineseCalendar = Calendar(identifier: .chinese)  // This line works fine

    var body: some View {
        
        DatePicker("Birthdate:", selection: $birthdate, displayedComponents: [.date])
        dateFormatter.calendar = Calendar(identifier: .chinese).   //Type '()' cannot conform to 'View'
        let dateInChinese = dateFormatter.string(from: birthdate)

Original post I’m trying to build along: How to convert Gregorian dates to Chinese (not Sexagenary Cycle) in Swift

New contributor

Nikki J. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT