鍍金池/ 問答/iOS  網(wǎng)絡(luò)安全  HTML/ 在顯示日期與時(shí)間時(shí)秒數(shù)無法顯示在label上(代碼沒錯(cuò))

在顯示日期與時(shí)間時(shí)秒數(shù)無法顯示在label上(代碼沒錯(cuò))

在設(shè)置顯示日期與時(shí)間的時(shí)候,試過幾種方法。

現(xiàn)在的方法是根據(jù)以下鏈接修改而來,可是無法顯示在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
    }

錯(cuò)誤截圖:
圖片描述

嘗試搜索stackworkflow,沒有結(jié)果。都鏈接正確,沒有最高贊的黃色嘆號(hào)。

圖片描述

參考鏈接:
libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
Date and Time! (Swift in Xcode)

試過以下兩種方法:

這種可以正確顯示,可是沒有秒數(shù)。,缺一個(gè)秒數(shù)動(dòng)起來的方法,就沒采用。

 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)日\(chéng)(hours)時(shí)\(minutes)分\(seconds)秒"

參考鏈接:
How To Get The Time And Date In Xcode 8 (Swift 3.0)

下面這種方法貌似是上面兩種的集合,但一樣缺少動(dòng)起來的方法,也沒有采用。

圖片描述
參考鏈接:
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)

2017年1月28日 07:07