将[String]值赋给Label时遇到问题。 (或 - 更好 - 到UITableView)。我收到错误'无法将[String]类型的值赋给String类型?'
我试过吗? Strig和as!字符串,但这也不太好用。代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myLabel: UILabel!
let cal = NSCalendar.currentCalendar()
let fmt = NSDateFormatter()
var auctionDates = [String]()
let textCellIdentifier = "TextCell"
override func viewDidLoad() {
super.viewDidLoad()
fmt.dateFormat = "(EEE)"
fmt.locale = NSLocale(localeIdentifier: "pl_PL")
var date = cal.startOfDayForDate(NSDate())
while auctionDates.count < 7 {
let weekDay = cal.component(.Weekday, fromDate: date)
if weekDay != 0 {
auctionDates.append(fmt.stringFromDate(date))
}
date = cal.dateByAddingUnit(.Day, value: 1, toDate: date, options: NSCalendarOptions(rawValue: 0))!
}
print(auctionDates)
self.myLabel.text = auctionDates
}
}
任何帮助都会很棒;)
答案 0 :(得分:3)
auctionDates
是数组(字符串列表)text
需要单个字符串(不是列表)有很多解决方案,其中一个是扁平化列表
self.myLabel.text = auctionDates.joinWithSeparator(", ")