简体   繁体   中英

How to print the JSON array data into the UILabel in Swift3 at new lines

I have got a JSON response like this:

[
    {
        "days": [
            "Sun",
            "Mon",
            "Tue",
            "Wed",
            "Thu"
        ],
        "times": [
            "09:00-12:00",
            "16:00-21:00"
        ]
    },
    {
        "days": [
            "Fri"
        ],
        "times": [
            "17:00-22:00"
        ]
    }
]

Now I have got a UILabel in which I am setting the text of days and times into next lines. The UILabel text will be looking like:

Sun Mon Tue Wed Thu

09:00-12:00

16:00-21:00

Fri

17:00 - 22:00

For this i am using the code :

var allDays = ""
var allTimes = ""
var allDaysTimes = ""
for operating in shop.operatingDays! {
  print(operating.days ?? "")
  print(operating.times ?? "")
  for days in operating.days! {
    print(days)
    allDays.append(days)
   // cell.operationalDays.text = allDays
  }
  for times in operating.times! {
    print(times)
    allTimes.append(times + "\n") 
    allDaysTimes = allDays + "\n" + allTimes
  }
  cell.operationalDays.text = allDaysTimes + "\n"
}

But i am not able to print Fri to the next line but it is showing in the same line along with Sun,Mon,Tue,Wed,Thu Any idea how to acheive

Please try like this:

for operating in shop.operatingDays! {
    print(operating.days ?? "")
    print(operating.times ?? "")
    cell.operationalDays.text =  operating.days!.joined() + operating.times.joined() + "\n"
}

Try this:

var allDays = ""
var allTimes = ""
var allDaysTimes = ""
for operating in shop.operatingDays! {
  print(operating.days ?? "")
  print(operating.times ?? "")
  for days in operating.days! {
    print(days)
    allDays.append(days)
   // cell.operationalDays.text = allDays
  }

  for times in operating.times! {
    print(times)
    allTimes.append(times + "\n") 

  }
  allDaysTimes.append(allDays + "\n" + allTimes)
  allDays = "\n"
  allTimes = ""

}
cell.operationalDays.text = allDaysTimes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM