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

时间:2017-07-21 12:38:21

标签: ios json uitableview swift3

我有这样的JSON响应:

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

现在我有了一个UILabel,我将日期和时间的文本设置为下一行。 UILabel文本将如下所示:

周日周二周日周日晚上

09:00-12:00

16:00-21:00

周五

17:00 - 22:00

为此,我使用的代码:

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"
}

但我无法将Fri打印到下一行,但它与Sun,Mon,Tue,Wed,Thu一起显示在同一行 任何想法如何实现

2 个答案:

答案 0 :(得分:1)

请尝试这样:

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

答案 1 :(得分:1)

试试这个:

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