根据一周的日期显示不同的消息

时间:2017-12-21 13:04:09

标签: ios swift date time

对于我正在研究的项目,我想创建一个应用程序,根据星期几(例如星期一,星期一,星期三等)显示不同的文本,但是我不知道该怎么做。任何帮助将非常感谢,因为我是新编码。 (因为以下代码似乎没有按预期运行。

class ViewController: UIViewController {

    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        let date = Date()
        let calendar = Calendar.current

        let hour = calendar.component(.hour, from: date)
        let minutes = calendar.component(.minute, from: date)
        let seconds = calendar.component(.second, from: date)

        let day = calendar.component(.day, from: date)
        let month = calendar.component(.month, from: date)
        let year = calendar.component(.year, from: date)

        if day = 21 {
            label.text = "not comming today"
        }
        else if day = 22 {
            label.text = "not comming today"
        }
        else if day = 23 {
            label.text = "is comming today"
        }
    }    

}

1 个答案:

答案 0 :(得分:1)

您需要weekday组件

let weekday = calendar.component(.weekday, from: date)
switch weekday {
  case 1: print("It's Sunday")
  case 2: print("It's Monday")
  ...
  case 7: print("It's Saturday")
  default: break
}