简体   繁体   English

如何在Swift3的新行中将JSON数组数据打印到UILabel中

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

I have got a JSON response like this: 我有这样的JSON响应:

[
    {
        "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. 现在,我有了一个UILabel,其中将日期和时间的文本设置为下一行。 The UILabel text will be looking like: UILabel文本将如下所示:

Sun Mon Tue Wed Thu 周日周二周三

09:00-12:00 09:00-12:00

16:00-21:00 16:00-21:00

Fri 周五

17:00 - 22:00 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 但是我无法将Fri打印到下一行,但是它与Sun,Mon,Tue,Wed,Thu一起显示在同一行中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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