Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Measurements and Units

Ken Tominaga
September 01, 2018

Measurements and Units

Ken Tominaga

September 01, 2018
Tweet

More Decks by Ken Tominaga

Other Decks in Programming

Transcript

  1. Measurementの定義 /// A `Measurement` is a model type that holds

    a `Double` value associated with a `Unit`. /// Measurements support a large set of operators, including `+`, `-`, `*`, `/`, and a full set of comparison operators. public struct Measurement<UnitType> where UnitType : Unit { /// The unit component of the `Measurement`. public let unit: UnitType /// The value component of the `Measurement`. public var value: Double /// Create a `Measurement` given a specified value and unit. public init(value: Double, unit: UnitType) } (一部略)
  2. Measurementの使い方 8 km Value Measurement Unit let _8km: Measurement<UnitLength> =

    Measurement(value: 8, unit: UnitLength.kilometers) // 8.0 km
  3. Measurementの特徴 <単位変換> let _8km: Measurement<UnitLength> = Measurement(value: 8, unit: UnitLength.kilometers)

    // 8.0 km let _5mi = _8km.converted(to: UnitLength.miles) // 5.0 mi 8 km 5 mile (正確には 4.97098189319845 mi)
  4. Measurementの特徴 <加減乗除> let _4km = Measurement<UnitLength>(value: 4, unit: .kilometers) let

    _1km = Measurement<UnitLength>(value: 1, unit: .kilometers) _4km + _1km // 5.0 km _4km - _1km // 3.0 km _4km * 2 // 8.0 km _4km / 2 // 2.0 km let _3mi = Measurement<UnitLength>(value: 3, unit: .miles) _4km + _3mi // 8828.02 m
  5. UnitとDimensionについて open class Unit { open var symbol: String {

    get } public init(symbol: String) } open class Dimension : Unit { @NSCopying open var converter: UnitConverter { get } public init(symbol: String, converter: UnitConverter) open class func baseUnit() -> Self } (一部略)
  6. Unitの定義 open class Unit { open var symbol: String {

    get } public init(symbol: String) } (一部略)
  7. Dimensionの定義 open class Dimension : Unit { @NSCopying open var

    converter: UnitConverter { get } public init(symbol: String, converter: UnitConverter) open class func baseUnit() -> Self } (一部略)
  8. Dimensionの特徴 • 単位の種類を表す(長さ、重さ、速さなど) • 異なる単位で表すことができる UnitLength: m, km, mi, ft,

    yd, etc. • 基本単位を必ず持つ UnitLength: m(メートル) • 同一次元内で単位変換できる km ⇄ m, mi ⇄ ft
  9. Reference • Measurements and Units - WWDC 2016 - Videos

    - Apple Developer • Units and Measurement | Apple Developer Documentation • Measurement - Foundation | Apple Developer Documentation • Unit - Foundation | Apple Developer Documentation • Dimension - Foundation | Apple Developer Documentation