Szekspir
Szekspir

Reputation: 119

Cannot assign value of type [String] to type String? with text label

I have problem with assign [String] value to Label. (or - better - to UITableView). I get error 'Cannot assign value of type [String] to type String?'

I tried with as? Strig and as! String, but this also not working well. The code:

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

}
}

Any help would be great ;)

Upvotes: 0

Views: 1014

Answers (1)

vadian
vadian

Reputation: 285039

  • auctionDates is an array (a list of strings)
  • text expects a single string (not a list)

There are many solutions, one of them is to flatten the list

self.myLabel.text = auctionDates.joinWithSeparator(", ")

Upvotes: 3

Related Questions