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

Measurement And Unitを試した話

Measurement And Unitを試した話

集まれSwift好き!Swift愛好会 vol9での発表資料です。

Atsushi Uchida (utwang)

July 31, 2016
Tweet

More Decks by Atsushi Uchida (utwang)

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ ಺ాರ೭ (@utwang) • ϑϦʔϥϯεΤϯδχΞ כϲ࡚ࡏॅ • Swift, Rails, Javascript

    • εϊʔϘʔυ޷͖ • ݸਓͰυϥΠϒ୅ͷׂΓצΞϓϦͩ͠·ͨ͠
  2. Ϟνϕʔγϣϯ • ݸਓͰ࡞ͬͨΞϓϦͷӳޠରԠ • ݴޠ͸ಉ͡Ͱ΋୯Ґ͕ҟͳΔ৔߹͕͋ Δ͕ɺશ෦ϩʔΧϥΠζରԠ͢Δͷ͸ େม • ΞϝϦΧ: ӳޠ

    /mile /gallon(US) /$ • ΠΪϦε: ӳޠ /mile /gallon(UK) /£ • Χφμɹ: ӳޠ /km /liter /CA$ • WWDC2016Ͱൃද͞Ε ͨ"Measurement And Unit"Λௐ΂Δ
  3. Կ͕Ͱ͖Δ͔ʁ • ͋Δܭྔͷ୯ҐΛผͷ୯Ґ΁ม׵Ͱ͖Δ • 15 km <-> 9.32 mile •

    30 kg <-> 13.6 lb (ϙϯυ) • localeʹ߹Θͤͯద੾ͳ୯ҐΛϑΥʔϚοτ • ΧελϜͷ୯ҐɺܭྔΛ࡞੒
  4. Measurementͷ࢖͍ํ(1) ྔͱ୯ҐΛ1ͭͷΠϯελϯεͱͯ͠؅ཧɾίϯόʔτ • ج४ͱͳΔ୯Ґ(baseUnit)Λݩʹม׵ • ௕͞ͷ৔߹͸meter͕baseUnitͱͳΔ let distanceInJapan = Measurement(value:

    80, unit: UnitLength.kilometers) // => 80.0 km let distanceInUS = distanceInJapan.converted(to: UnitLength.miles) // => 49.7098189319845 mi
  5. Measurementͷ࢖͍ํ(2) ΧελϜͷ୯ҐΛ࡞੒Ͱ͖Δ • ೩අͷϓϦηοτ͸ L/100km, mile/gal(US), mile/gal(UK) • ೔ຊͰ͸km/LͰද͢ͷ͕Ұൠతɻج४ͷL/100km͔Βͷൺ཰Λ ࢦఆͯ͠৽ͨʹ୯ҐΛ࡞Δɻ

    let kmPerLiter = UnitFuelEfficiency(symbol: "km/l", converter: UnitConverterLinear(coefficient: 0.25)) //UnitFuelEfficiencyͷextensionͱͯ͠΋ఆٛՄೳ let fuelEfficiencyInJp = Measurement(value: 10, unit: kmPerLiter) // => 10 km/L
  6. Measurementͷ࢖͍ํ(3) localeʹ߹Θͤͨ୯ҐΛมߋ • Locale৘ใΛ΋ͭMeasurementFormatter͕༻ҙ͞Ε͍ͯΔ • Regionʹదͨ͠୯ҐΛදࣔ͢Δ let mFormatter = MeasurementFormatter()

    mFormatter.unitStyle = .short let distance = Measurement(value: 400, unit: UnitLength.kilometers) mFormatter.string(from: distance) // => 248.549 mi (in US) // => 400 km (in Canada)
  7. MeasurementΛߏ੒͢ ΔΫϥε • Measurement(ܭྔ) • Unit(୯Ґ) • Dimension(෺ཧྔͷ࣍ݩ) • ந৅Ϋϥε

    • UnitXXX (UnitAngle, UnitLength, UnitVolume ...) • Dimensionͷ۩৅Ϋϥε • UnitConverter
  8. MeasurementΛߏ੒͢ ΔΫϥε ৽͍͠෺ཧྔͷ࣍ݩΛ࡞Δ৔߹ • DimensionΛܧঝͨ͠UnitTypingSpeed Λ࡞੒ • ֤୯Ґͱͯ͠UnitTypingSpeedܕΛࣗ਎ Λฦ͢ϓϩύςΟΛ༻ҙ͢Δ class

    UnitTypingSpeed: Dimension { static let punchPerSecond = UnitTypingSpeed(symbol: "punch/s") static let charsPerMinute = UnitTypingSpeed(symbol: "chars/min", converter: UnitPunchToCharConverter())