(This probably needs a better headline ...)
I would like to have a set of accessories that I can use in code to quickly express the length of time. For example:
42.seconds
3.14.minutes
0.5.hours
13.days
This post shows that you cannot just do it with a simple new protocol, expand and force, IntegerTypeand FloatingPointTypeto accept it. So I thought that I would just go on a more redundant route and just continue IntegerTypedirectly (and then repeat the code for FloatingPointType).
extension IntegerType {
var seconds:NSTimeInterval {
return NSTimeInterval(self)
}
}
The generated error is confusing:
Playground execution failed: /var/folders/2k/6y8rslzn1m95gjpg534j7v8jzr03tz/T/./lldb/9325/playground131.swift:31:10: error: cannot invoke initializer for type 'NSTimeInterval' with an argument list of type '(Self)'
return NSTimeInterval(self)
^
/var/folders/2k/6y8rslzn1m95gjpg534j7v8jzr03tz/T/./lldb/9325/playground131.swift:31:10: note: overloads for 'NSTimeInterval' exist with these partially matching parameter lists: (Double), (UInt8), (Int8), (UInt16), (Int16), (UInt32), (Int32), (UInt64), (Int64), (UInt), (Int), (Float), (Float80), (String), (CGFloat), (NSNumber)
return NSTimeInterval(self)
, , , NSTimeInterval() (Self), , NSTimeInterval. ?
: , Swift . / Apple Swift
Update/
, :
42.seconds --> 42
3.14.minutes --> 188.4
0.5.hours --> 1800
13.days --> 1123200
, , NSTimeInterval ( Double), :
42.seconds is NSTimeInterval --> true
3.14.minutes is NSTimeInterval --> true
0.5.hours is NSTimeInterval --> true
13.days is NSTimeInterval --> true
, , Double Int :
extension Int {
var seconds:NSTimeInterval {
return NSTimeInterval(self)
}
var minutes:NSTimeInterval {
return NSTimeInterval(self * 60)
}
var hours:NSTimeInterval {
return NSTimeInterval(self * 3600)
}
var days:NSTimeInterval {
return NSTimeInterval(self * 3600 * 24)
}
}
extension Double {
var seconds:NSTimeInterval {
return NSTimeInterval(self)
}
var minutes:NSTimeInterval {
return NSTimeInterval(self * 60)
}
var hours:NSTimeInterval {
return NSTimeInterval(self * 3600)
}
var days:NSTimeInterval {
return NSTimeInterval(self * 3600 * 24)
}
}
:
let foo:Uint = 4242
foo.minutes --> 254520
foo.minutes is NSTimeInterval --> true
, Int, UInt. UInt, UInt16, Int16 ..
Int IntegerType, , , , . FloatingPointType, Double. . , IntegerType, . IntegerType , , , , NSTimeInterval() ?