0

Questions : How do I use DateFormatter(Type: CLong) in tableview?

Similar questions I looked for

How to change JSON string to date format in tableview

I looked up questions like these on stackoverflow. However, there was no way to use Type: CLong.

Clong is a type I'm new to using, so I don't know what type to use Optional.

connection json file

struct BoardList: Codable {
    var b_date: CLong?
}

TableViewController

var boards: [BoardList]?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
      if let boards = self.boards {
         let model = boards[indexPath.row]
            
         let dateFormatter = DateFormatter()
         dateFormatter.dateFormat = "yyyy/MM/dd HH:mm:ss"

         cell.txtDate.text = model.b_date // ERROR [Cannot assign value of type 'CLong?' (aka 'Optional<Int>') to type 'String?']
    
     }
     return cell
 }

++ I must take the CLong type. Because the server value is specified in that format.

value assigned to Postman value assigned to Postman

je2
  • 35
  • 1
  • 6
  • Why are you using `C*` types in parsed types? – user28434'mstep Oct 16 '20 at 09:11
  • I just modified the question. Thank you - user28434 – je2 Oct 16 '20 at 09:11
  • 1
    question still stands. In `Swift` `CLong` is just an alias for `Int`. And if server uses some type it doesn't mean at all the client should use the same type. – user28434'mstep Oct 16 '20 at 09:13
  • 1
    You need to know what the value in `b_date` represents; presumably seconds since some reference point. Once you know this you can use the Int value to create a `Date` – Paulw11 Oct 16 '20 at 09:15
  • At first, I received that as a String and then received the error as an Int, and I was confused with the error. It works well even if I receive it as an Int. I think I should get the year/month/day here. Thank you for your answer. I thought I should only get it with clong. Sorry -user28434, Paulw11 – je2 Oct 16 '20 at 09:21

1 Answers1

1

The server sends JSON. There is no type CLong in JSON, it's an Int.

You can decode the date without any date formatter just be declaring b_date as Date. The default date decoding strategy is secondsSince1970 which the integer represents.

let jsonString = """
{"b_date" : 1602813427}
"""

struct BoardList: Codable {
    let b_date: Date
}

let data = Data(jsonString.utf8)

do {
    let result = try JSONDecoder().decode(BoardList.self, from: data)
    print(result)
} catch {
    print(error)
}
 

You can also specify the convertFromSnakeCase strategy and declare the struct member bDate to get rid of the ugly snake case name.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • Technically, there's no `Int` in `JSON` either. Only `Number` which is like `Double` in `Swift`. That's also the reason why `[U]Int64` may behave weird when parsed from `JSON`. – user28434'mstep Oct 16 '20 at 09:20
  • @user28434 Yes, but the `JSONDecoder` treats integer values as `Int` by default. – vadian Oct 16 '20 at 09:21
  • Thank you for your answer, vadian, but I can't specify a value because it only converts and loads multiple b_date values ​​from the server. What should I do in this case? I have a lot of b_date values ex) 1602813434,1602813474 – je2 Oct 16 '20 at 09:30
  • `1602813434,1602813474` is invalid JSON unless the entire expression is wrapped in double quotes or in square brackets. – vadian Oct 16 '20 at 09:33
  • Sorry. I don't understand. I am doing a process that converts the value ```b_date``` that I put in in real time for practice on the server. The time will continue to change, but if you save it in a variable, I think that the newly registered values ​​cannot be time converted. - vadian – je2 Oct 16 '20 at 09:37
  • `"b_date": 1602813434,1602813474` in a JSON string is not valid. Valid JSON is `"b_date": "1602813434,1602813474"` or `"b_date": [1602813434,1602813474]` – vadian Oct 16 '20 at 09:41
  • Oh I understand. Thank you. The ```ex)1602813434,1602813474``` that I mentioned shows some of the values ​​stored in the server. In the server, ```"b_date": 1602834691``` is stored like this In xcode I have ```struct BoardList: Codable { var b_date: Int? }``` It is loaded in the form. Servers have a lot of value. As I said before, I think that if I store the values ​​added in real time in an array, I cannot change all the added values ​​to the current time. The question is complicated, but I would really appreciate if you could answer it. – je2 Oct 16 '20 at 09:50
  • I should have summarized it well, but I'm sorry for the complexity. – je2 Oct 16 '20 at 09:50