参见英文答案 >
“CGFloat is not convertible to Int” when trying to calculate an expression1个
我四处搜寻,被这个谜语搞得一团糟.
我四处搜寻,被这个谜语搞得一团糟.
在Swift,Xcode 6.2中,这些行有效:
let day_seconds = 86400 let one_day_from_now = NSDate(timeIntervalSinceNow:86400)
let day_seconds = 86400 let one_day_from_now = NSDate(timeIntervalSinceNow:day_seconds)
控制台输出:
“Playground execution Failed: /var/folders/4n/88gryr0j2pn318sw_g_mgkgh0000gn/T/lldb/10688/playground625.swift:24:30: error: extra argument ‘timeIntervalSinceNow’ in call
let one_day_from_now = NSDate(timeIntervalSinceNow:day_seconds)”
这里发生了什么?为什么NSDate棘手?
这是因为timeIntervalSinceNow期望NSTimeInterval是Double.
原文链接:https://www.f2er.com/swift/318682.html如果你这样做:
let day_seconds = 86400
day_second是Int类型,它不是方法所期望的.
但是,当您键入数字本身时:
let one_day_from_now = NSDate(timeIntervalSinceNow:86400)
编译器隐含你传递Double,因为它是方法所期望的,这是好的.
解决方案可以使用
NSTimeInterval(day_seconds)或Double(day_seconds)是相同的或当你声明常量时确保它是double,例如:
let day_seconds = 86400.0
要么
let day_seconds: Double = 86400
要么
let day_seconds: NSTimeInterval = 86400