在设置显示日期与时间的时候,试过几种方法。
现在的方法是根据以下链接修改而来,可是无法显示在label上。
import UIKit
class TableViewController: UITableViewController{
@IBOutlet var TimeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
Timer.scheduledTimer(timeInterval:0.1,target:self,selector:Selector(("updateTime")),userInfo:nil,repeats:true)
func didReceiveMemoryWarning() {
}
func updateTime(){
TimeLabel.text = DateFormatter.localizedString(from: Date(), dateStyle: DateFormatter.Style.full, timeStyle: DateFormatter.Style.full)
}
func setupNavBar(){
navigationController?.navigationBar.prefersLargeTitles = true
}
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0
}
错误截图:
尝试搜索stackworkflow,没有结果。都链接正确,没有最高赞的黄色叹号。
参考链接:
libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
Date and Time! (Swift in Xcode)
试过以下两种方法:
这种可以正确显示,可是没有秒数。,缺一个秒数动起来的方法,就没采用。
let date = Date()
let calendar = Calendar.current
let year = calendar.component(.year, from: date)
let month = calendar.component(.month, from: date)
let day = calendar.component(.day, from: date)
let hours = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let seconds = calendar.component(.second, from: date)
TimeLabel.text = "\(year)年\(month)月\(day)日\(hours)时\(minutes)分\(seconds)秒"
参考链接:
How To Get The Time And Date In Xcode 8 (Swift 3.0)
下面这种方法貌似是上面两种的集合,但一样缺少动起来的方法,也没有采用。
参考链接:
Fetching Date and Time (2 methods) (Swift 3 + Xcode 8.2.1)
试一下改成这样: Timer.scheduledTimer(timeInterval:0.1,target:self,selector:#selector(updateTime),userInfo:nil,repeats:true)