1-有兩個類,在A類調用B類的方式時,出現?Instance member '**' cannot be used on type '**'; did you mean to use a value of this type instead
錯誤
是因為用B類的類名,調用了 B 類的實例化方法,類似 OC中直接用類名調用了減號方法,所以需要在方法的 func 關鍵字之前加上關鍵字 static ,來指定類型方法。類還可以用關鍵字 class 來允許子類重寫 父類的方法實現。如果不加,需要先實例化 B 的類名,用實例化的對象調用方法,如下是獲得當前時間的類型方法:
//MARK: -獲得當前時間
class func nowTime() -> String {
let date = NSDate()
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "YYYYMMddHHmmss"
let nowTimeStr = timeFormatter.string(from: date as Date) as String
return nowTimeStr
}