假设我想显示一个字符串,具体取决于它的日期。 例如,如果它是2016-12-24我想要字符串"圣诞快乐"要显示或如果它是2017-01-01我想要文本"新年快乐"。
这是我到目前为止所得到的,但我不知道如何添加字符串并将其连接到标签。
let now = NSDate()
print(now)
now.timeIntervalSinceReferenceDate
// March 10, 1876 was 3,938,698,800 seconds before the third millennium (January 1, 2001 midnight UTC)
let firstLandPhoneCallDate = NSDate(timeIntervalSinceReferenceDate: -3_938_698_800.0)
// April 3, 1973 was 875,646,000 seconds before the third millennium
let firstCellPhoneCallDate = NSDate(timeIntervalSinceReferenceDate: -875_646_000.0)
// January 9, 2007, 18:00 UTC was 190,058,400 seconds after the third millennium
let iPhoneAnnouncementDate = NSDate(timeIntervalSinceReferenceDate: 190_058_400.0)
// January 27, 2010, 18:00 UTC was 286,308,000 seconds after the third millennium
let iPadAnnouncementDate = NSDate(timeIntervalSinceReferenceDate: 286_308_000.0)
答案 0 :(得分:1)
我建议你有一个包含日期和事件名称的字典或数据库。在这个例子中,我将使用简单的dict,如:
let eventsCalendar: Dictionary<String, String> = [
"0101": "Happpy New Year!",
//... other dates and events
]
在这个例子中,我使用字符串,但日期可以是NSDate。 然后将今天的NSDate变成字符串,如:
func todaysDateAsString() -> String {
let date = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMdd"
let str = dateFormatter.stringFromDate(date)
return str
}
最后,将字典中的相关内容分配到您的标签Outlet(您可以将标签拖放到ViewController代码窗口并创建一个插座名称(不是动作,而是插座)。调用它,标签然后使用方法那个标签 - label.text
func todaysEventString(dateString: String) -> String {
return eventsCalendar[dateString]!
}
label.text = todaysEventString(todaysDateAsString())
今天仅使用月份和日期。如果您有多年不同的事件,则在数据库中包含一年,并在dateformatter.dateFormat中使用year。 在你的情况下:
func DateAsString() -> String {
let date = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let str = dateFormatter.stringFromDate(date)
return str
}
数据库以yyyy-MM-dd格式保存密钥:
"1992-01-22": "Somebody's Birthday",
答案 1 :(得分:1)
在这里,这非常有效:
//
// ViewController.swift
// temp
//
// Created by thinkswift on 12.01.16.
// Copyright © 2016. thinkswift. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myLabel: UILabel!
let eventsCalendar: Dictionary<String, String> = [
"1992-01-22": "Somebody's Birthday",
"2016-01-12": "Johns Birthday"
//... other dates and events
]
func DateAsString() -> String {
let date = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let str = dateFormatter.stringFromDate(date)
return str
}
func todaysEventString(dateString: String) -> String {
return eventsCalendar[dateString]!
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myLabel.text = todaysEventString(DateAsString())
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 2 :(得分:0)
导入UIKit
类ViewController:UIViewController {
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
let eventsCalendar: Dictionary<String, String> = [
"1992-01-22": "Somebody's Birthday",
//... other dates and events
]
func DateAsString() -> String {
let date = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let str = dateFormatter.stringFromDate(date)
return str
}
func todaysEventString(dateString: String) -> String {
return eventsCalendar[dateString]!
}
label.text = todaysEventString(todaysDateAsString())
}
答案 3 :(得分:0)
Swift 3.0更新 - 适用于Xcode 8.02
let date = NSDate()
let formatter = DateFormatter()
formatter.dateFormat = "M/dd/yyyy" // Set the way the date should be displayed
dateLabel.text = formatter.string(from: date as Date)