一、相关类
Dimension 是units的分类。比如长度,时间,速度。
长度:km,m,ft,mi,etc
有一个默认的基本单位:let meter = UnitLength.baseUnit
可以转换单位 km <=>ft, m <=> mi
// Measurement
public struct Measurement<UnitType : Unit> : Comparable, Equatable {
public let unit: UnitType
public var value: Double
public init(value: Double, unit: UnitType)
}
// Calculations With Measurements
let distanceTraveled = Measurement(value: 5, unit: UnitLength.feet)
let distanceToGo = Measurement(value: 6, unit: UnitLength.feet)
let totalDistance = distanceTraveled + distanceToGo
// Unit
public class Unit : NSObject, NSCopying {
public let symbol : String
public init(symbol: String)
}
// Dimension
public class Dimension : Unit, NSCoding {
public var converter : UnitConverter { get }
public init(symbol: String, converter: UnitConverter)
public class var baseUnit : Dimension
}
public class UnitLength : Dimension {/*
Base unit - meters */
public class var kilometers: UnitLength { get }
public class var meters: UnitLength { get }
public class var feet: UnitLength { get }
public class var miles: UnitLength { get }
...}
二、自定义单位
单位转换: baseUnit <=>unit
-
UnitConverter
-
baseUnitValue(fromValue value:)
-
value(fromBaseUnitValue baseUnitValue:)
-
UnitConverterLinear
-
baseUnitValue = value * coefficient + constant
-
value = (baseUnitValue - constant)/coefficient
// “Jammin’ in the Streetz” Game - Custom Units
let jamz = UnitDuration(symbol: “jamz", converter: UnitConverterLinear(coefficient: 30))
let hopz = UnitLength(symbol: “hopz", converter: UnitConverterLinear(coefficient: 0.75))
let glidez = UnitLength(symbol: “glidez", converter: UnitConverterLinear(coefficient: 1.5))
// “Jammin’ in the Streetz” Game - Custom Dimension
238_MeasurementsAndUnits_Final.key
public class UnitDanceMove : Dimension {
static let wackyArmMovements = UnitDanceMove(symbol: "💪",converter: UnitConverterLinear(coefficient: 1))
static let robot = UnitDanceMove(symbol: "🤖",converter: UnitConverterLinear(coefficient: 4))
static let cabbagePatch = UnitDanceMove(symbol: "👼", converter: UnitConverterLinear(coefficient: 3))
static let jazzHands = UnitDanceMove(symbol: "👐",converter: UnitConverterLinear(coefficient: 2))
}
// “Jammin’ in the Streetz” Game - Jam Session
public struct JamSession {
public var stepsTaken : Measurement<UnitLength>
public var jamTime : Measurement<UnitDuration>
public var danceMoves : Measurement<UnitDanceMove>
public var danceRate : Measurement<UnitSpeed> {
let stepsInMeters = stepsTaken.converted(to: .meters)
let jamTimeInSeconds = jamTime.converted(to: .seconds)
return Measurement(value: (stepsInMeters.value / jamTimeInSeconds.value),
}
}
三、格式化
unit options.png// MeasurementFormatter
public class MeasurementFormatter : Formatter, NSSecureCoding {
public var unitOptions: MeasurementFormatter.UnitOptions
public var unitStyle: Formatter.UnitStyle
@NSCopying public var locale: Locale!
@NSCopying public var numberFormatter: NumberFormatter!
public func string(from measurement: Measurement<Unit>) -> String
public func string(from unit: Unit) -> String
}
// 1.系统实现的单位,结果为系统符号
let formatter = MeasurementFormatter()
let distance = Measurement(value: 5, unit: UnitLength.kilometers)
let result = formatter.string(from: distance)
// 2.自定义系统实现的单位,结果为系统符号
let hopz = UnitLength(symbol: “hopz", converter: UnitConverterLinear(coefficient: 0.75))
let hopzDistance = Measurement(value: 1000, unit: hope)
let resultingHopz = formatter.string(from: hopzDistance)
// 3.自定义系统实现的单位,设置unitOptions,结果为自定义符号
formatter.unitOptions = [.providedUnit]
let hopzDistance = Measurement(value: 1000, unit: hope)
let resultingHopz = formatter.string(from: hopzDistance)
// 4.完全自定义单位,结果为自定义符号
let robotDance = Measurement(value: 30, unit: UnitDanceMove.robot)
let resultingRobotDances = formatter.string(from: robotDance)
网友评论