0

I would like to receive the date value in the api value as utc. I looked up the stackoverflow. There was a similar case, but we couldn't solve it because it was different from me.

The server (POSTMAN(db) stores the value "b_date": 1602813891.

link >> Dateformatter issue in swift

mycode

var ViewItem: BoardView?

func DetailViewList() {

     DispatchQueue.main.async {
     self.txtUserName.text = String(ViewItem.userId ?? "")
     self.txtCount.text = String(ViewItem.b_count ?? 0)

     }
}

func utcToLocal(utcDate: String, dateFormat: String) -> String {

     let dfFormat = DateFormatter()

     dfFormat.dateFormat = dateFormat
     dfFormat.timeZone = TimeZone(abbreviation: "UTC")
        
     let dtUtcDate = dfFormat.date(from: utcDate)
     dfFormat.timeZone = TimeZone.current
     dfFormat.dateFormat = dateFormat

     txtDate.text = Int(ViewItem?.b_date) // ERROR [Cannot invoke initializer for type 'Int' with an argument list of type '(Int?)'] , Overloads for 'Int' exist with these partially matching parameter lists: (CGFloat), (Double), (Float), (Float80), (Int64), (Word), (__shared NSNumber)
        
     return dfFormat.string(from: dtUtcDate!)
    }

jsonData

struct BoardView: Codable {
    var b_date: Int?
    var b_count: Int?
    var userId: String?
}
je2
  • 35
  • 1
  • 6
  • What is the difference to your [previous question](https://stackoverflow.com/questions/64386099/cannot-assign-value-of-type-clong-aka-optionalint-to-type-string-sw)? I told you to declare `b_date` as `Date`. What's wrong with it? – vadian Oct 20 '20 at 10:57
  • The previous question was how to ```get json``` ```long```. The answer to the question was not ```long```, but an int. Badian, thank you for trying to help me, but it hasn't been solved. – je2 Oct 20 '20 at 11:07
  • As I said in the previous question, I don't think saving the added values in an array in real time can change all the added values to the current time. I posted it because I didn't think it was the same question. I'll delete it if it's a problem. – je2 Oct 20 '20 at 11:07
  • 1
    The ERROR is clear: You have to create a `String` rather than an `Int` to assign the value to a label. And I still have no idea what *saving the added values in an array in real time* means. – vadian Oct 20 '20 at 11:12
  • Thank you. The app I'm making is a bulletin board connected to the server. I explained that if you post a new article on the web, the time on the article is added in real time. – je2 Oct 20 '20 at 11:29
  • 1
    The error message tells you that you can't initialise an Int with an optional Int. Why is b_date an optional? If your json always has this value then simply make it non-optional. If it is optional then you need to unwrap it before using it. It also isn't clear exactly what you are trying to do with the string input to your function. Convert the json value to a `Date` straight away and then just format that date as required. – Paulw11 Oct 20 '20 at 12:01

0 Answers0