Trying to extend IntegerType (and FloatingPointType); Why not all Int types can be converted to NSTimeInterval

(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() ?

+4
3

Uint, UInt16, Int16 ..

. . , . ( "?" " ".)

, . ( , Float80, ). .

import Foundation

// Declare the protocol
protocol TimeIntervalConvertible {
    func toTimeInterval() -> NSTimeInterval
}

// Add all the helpers you wanted
extension TimeIntervalConvertible {
    var seconds:NSTimeInterval {
        return NSTimeInterval(self.toTimeInterval())
    }

    var minutes:NSTimeInterval {
        return NSTimeInterval(self.toTimeInterval() * 60)
    }

    var hours:NSTimeInterval {
        return NSTimeInterval(self.toTimeInterval() * 3600)
    }

    var days:NSTimeInterval {
        return NSTimeInterval(self.toTimeInterval() * 3600 * 24)
    }
}

// Provide the implementations. FloatingPointType doesn't have an equivalent to
// toIntMax(). There no toDouble() or toFloatMax(). Converting a Float to
// a Double injects data noise in a way that converting Int8 to IntMax does not.
extension Double {
    func toTimeInterval() -> NSTimeInterval { return NSTimeInterval(self) }
}

extension Float {
    func toTimeInterval() -> NSTimeInterval { return NSTimeInterval(self) }
}

extension IntegerType {
    func toTimeInterval() -> NSTimeInterval { return NSTimeInterval(self.toIntMax()) }
}

// And then we tell it that all the int types can get his implementation
extension Int: TimeIntervalConvertible {}
extension Int8: TimeIntervalConvertible {}
extension Int16: TimeIntervalConvertible {}
extension Int32: TimeIntervalConvertible {}
extension Int64: TimeIntervalConvertible {}
extension UInt: TimeIntervalConvertible {}
extension UInt8: TimeIntervalConvertible {}
extension UInt16: TimeIntervalConvertible {}
extension UInt32: TimeIntervalConvertible {}
extension UInt64: TimeIntervalConvertible {}

, Swift. stdlib. :

extension Double {
    public init(_ v: UInt8)
    public init(_ v: Int8)
    public init(_ v: UInt16)
    public init(_ v: Int16)
    public init(_ v: UInt32)
    public init(_ v: Int32)
    public init(_ v: UInt64)
    public init(_ v: Int64)
    public init(_ v: UInt)
    public init(_ v: Int)
}

" "? . Swift.

"?"

. - . , . .

, "Swift ", . Swift . . , . , , Int64 Int8 UInt8 Int8 . Int64 Double . 64- Double. , , . Float Double . 1/10, Float, 1/10, Double. Float Double, ? , , , .

, .days . 24 . 23 25 DST. . . "", . , , NSDate, NSTimeInterval. .

, . :

1.seconds

:

1 * Second

, , Double. . , "time * frequency == cycles" "cycle/time == frequency", Double. , NSTimeInterval ; Double. , , NSTimeInterval, Double ( ).

, , :

let Second: NSTimeInterval = 1
let Seconds = Second
let Minute = 60 * Seconds
let Minutes = Minute
let Hour = 60 * Minutes
let Hours = Hour

let x = 100*Seconds

. .

+4

NSTimeInterval - Double. , , , :

struct MyTimeInterval {
    var totalSecs: NSTimeInterval

    var totalHours: NSTimeInterval {
        get {
            return self.totalSecs / 3600.0
        }
        set {
            self.totalSecs = newValue * 3600.0
        }
    }

    init(_ secs: NSTimeInterval) {
        self.totalSecs = secs
    }

    func totalHourString() -> String {
        return String(format: "%.2f hours", arguments: [self.totalHours])
    }
}

var t = MyTimeInterval(5400)
print(t.totalHourString())
+1
extension Int {
    var seconds: Int {
            return self
        }
    var minutes: Int {
        get {
            return self * 60
        }
    }
    var hours: Int {
        get {
            return minutes * 60
        }
    }
    var timeString: String {
        get {
            let seconds = abs(self) % 60
            let minutes = ((abs(self) - seconds) / 60) % 60
            let hours = ((abs(self) - seconds) / 3660)
            let sign = self < 0 ? "-" :" "
            let str = String(format: "\(sign)%2d:%02d:%02d", arguments: [Int(hours),Int(minutes),Int(seconds)])
            return str
        }
    }
}

6.minutes == 360.seconds                    // true

let t1 = 1.hours + 22.minutes + 12.seconds
let t2 = 12.hours - 15.minutes
let time = t1 - t2

print("t1    =",t1.timeString,t1)             // t1    =   1:22:12 4932
print("t2    =",t2.timeString,t2)             // t2    =  11:45:00 42300
print("t1-t2 =",time.timeString,time)         // t1-t2 = -10:22:48 -37368

(12.hours / 30.minutes) == 24                 // true
0

Source: https://habr.com/ru/post/1615148/


All Articles