【发布时间】:2018-07-10 11:04:32
【问题描述】:
我收到“swift 中的 void 函数中出现意外的非 void 返回值”的错误。以及如何调用这个函数。我想要返回字符串并希望将其作为参数传递。请看我下面的代码
static func truetime() -> String? {
let client = TrueTimeClient.sharedInstance
//client.start()
client.fetchIfNeeded { result in
switch result {
case let .success(referenceTime):
let now = referenceTime.now()
//print("Time is " + "\(now)")
let dateFormatter : DateFormatter = DateFormatter()
dateFormatter.dateFormat = "\(KeyValues.datetimeformat)"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+05:00")
let dateString = dateFormatter.string(from: now)
print("Time is " + "\(dateString)")
return dateString
case let .failure(error):
print("Error! \(error)")
}
}
}
【问题讨论】:
-
client.fetchIfNeeded { result in … }是一个具有 void 返回类型的闭包(类似于内联函数)。return dateString正在尝试返回不允许的String。
标签: ios swift switch-statement