-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitExtensions.swift
46 lines (37 loc) · 1.25 KB
/
UnitExtensions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// UnitExtensions.swift
// RunForFun
//
// Created by Константин Малков on 23.08.2020.
// Copyright © 2020 Malkov Kostia. All rights reserved.
//
//класс с конвертацией полученных данных в нужный формат
//
import Foundation
class UnitConverterPace: UnitConverter {
private let coefficient: Double
init(coefficient: Double) {
self.coefficient = coefficient
}
override func baseUnitValue(fromValue value: Double) -> Double {
return reciprocal(value * coefficient)
}
override func value(fromBaseUnitValue baseUnitValue: Double) -> Double {
return reciprocal(baseUnitValue * coefficient)
}
private func reciprocal(_ value: Double) -> Double {
guard value != 0 else { return 0 }
return 1.0 / value
}
}
extension UnitSpeed {
class var secondsPerMeter: UnitSpeed {
return UnitSpeed(symbol: "сек/м", converter: UnitConverterPace(coefficient: 1))
}
class var minutesPerKilometer: UnitSpeed {
return UnitSpeed(symbol: "мин/км", converter: UnitConverterPace(coefficient: 60.0 / 1000.0))
}
class var minutesPerMile: UnitSpeed {
return UnitSpeed(symbol: "мин/мил", converter: UnitConverterPace(coefficient: 60.0 / 1609.34))
}
}