【问题标题】:How to print the JSON array data into the UILabel in Swift3 at new lines如何在新行中将 JSON 数组数据打印到 Swift3 中的 UILabel 中
【发布时间】:2017-07-21 12:38:21
【问题描述】:

我收到了这样的 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 一起显示在同一行 知道如何实现

【问题讨论】:

  • let v = arrayJSON.flatMap( {(subDict) -> String in let days = subDict["days"]?.joined(separator: " "); let times = subDict["times"]?.joined(separator: "\n"); return days! + "\n" + times! }).joined(separator: "\n"); cell.operationalDays.text = v;?
  • @ChelseaShawra 你只有一个 UI 标签?

标签: ios json uitableview swift3


【解决方案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

【讨论】:

    【解决方案2】:

    请尝试这样:

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

    【讨论】:

    • 你遇到了什么问题?
    猜你喜欢
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 2022-09-27
    • 2022-01-10
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 2011-10-30
    相关资源
    最近更新 更多