$30 off During Our Annual Pro Sale. View Details »

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. あなたの知らない
    MeasurementsとUnitsの世界
    2018/09/01 iOSDC LT
    @ken0nek

    View Slide

  2. Measurements & Units ??

    View Slide

  3. Measurements & Unitsとは
    iOS10からFoundation.frameworkに追加された、
    数量と単位を扱う仕組み
    ● 数量の単位変換
    ● 数量の表示形式
    8 km 5 mile
    Value
    Measurement
    Unit

    View Slide

  4. https://www.apple.com/ios/health/

    View Slide

  5. Measurement

    View Slide

  6. 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 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)
    }
    (一部略)

    View Slide

  7. Measurementの使い方
    8 km
    Value
    Measurement
    Unit
    let _8km: Measurement
    = Measurement(value: 8, unit: UnitLength.kilometers)
    // 8.0 km

    View Slide

  8. Measurementの特徴 <単位変換>
    let _8km: Measurement
    = 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)

    View Slide

  9. Measurementの特徴 <加減乗除>
    let _4km = Measurement(value: 4, unit: .kilometers)
    let _1km = Measurement(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(value: 3, unit: .miles)
    _4km + _3mi // 8828.02 m

    View Slide

  10. Unit & Dimension

    View Slide

  11. Unit

    View Slide

  12. 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
    }
    (一部略)

    View Slide

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

    View Slide

  14. Unitの特徴
    記号 “km”
    次元 長さ
    基本単位との変換 1 km = 1000 m
    例: UnitLength.kilometers

    View Slide

  15. Dimension

    View Slide

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

    View Slide

  17. Dimensionの特徴
    ● 単位の種類を表す(長さ、重さ、速さなど)
    ● 異なる単位で表すことができる
    UnitLength: m, km, mi, ft, yd, etc.
    ● 基本単位を必ず持つ
    UnitLength: m(メートル)
    ● 同一次元内で単位変換できる
    km ⇄ m, mi ⇄ ft

    View Slide

  18. Dimensionについて <定義の豊富さ>
    UnitLength UnitArea UnitAngle
    UnitDuration UnitMass UnitSpeed
    UnitPressure UnitTemprature UnitAcceleration
    国際単位系に準拠した単位が
    170個以上

    View Slide

  19. まとめ
    ● Measurements&UnitsはiOS10から導入された
    数量と単位を扱う仕組み
    ● 単位変換やローカライズが簡単に実現できる
    発展編...
    ● UnitConverter
    ● MeasurementFormatter
    ● カスタム単位の実装方法
    ● カスタム次元の実装方法

    View Slide

  20. 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

    View Slide

  21. Thank you

    View Slide