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. Measurement And Unit Λࢼͨ͠࿩ 2016/7/31 ू·ΕSwift޷͖ʂSwiftѪ޷ձ vol9

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

    • εϊʔϘʔυ޷͖ • ݸਓͰυϥΠϒ୅ͷׂΓצΞϓϦͩ͠·ͨ͠
  3. ׆ಈ ԣ඿पลͰษڧձ/ΠϕϯτΛ΍ͬͯ·͢ • Railsͷ΋͘΋͘ձ/Swiftͷ΋͘΋͘ձ

  4. • 8/21(೔) Ϋϩʔϥʔͷӡ༻ʹ·ͭΘΔLTΠϕϯτ εϐʔΧʔɺઈࢍืूதͰ͢

  5. ΞδΣϯμ • Ϟνϕʔγϣϯ • Measurementͱ͸ • Կ͕Ͱ͖Δ͔ʁ • Measurement/Unitͷ࢖͍ํ •

    MeasurementΛߏ੒͢ΔΫϥε • ࢖ͬͯΈͯͷײ૝
  6. Xcode8 beta3Ͱࢼͨ͠಺༰Ͱ͢ɻ ਖ਼ࣜϦϦʔε൛Ͱ͸಺༰͕ҟͳΔՄೳੑ͕͋Γ·͢ɻ

  7. Ϟνϕʔγϣϯ • ݸਓͰ࡞ͬͨΞϓϦͷӳޠରԠ • ݴޠ͸ಉ͡Ͱ΋୯Ґ͕ҟͳΔ৔߹͕͋ Δ͕ɺશ෦ϩʔΧϥΠζରԠ͢Δͷ͸ େม • ΞϝϦΧ: ӳޠ

    /mile /gallon(US) /$ • ΠΪϦε: ӳޠ /mile /gallon(UK) /£ • Χφμɹ: ӳޠ /km /liter /CA$ • WWDC2016Ͱൃද͞Ε ͨ"Measurement And Unit"Λௐ΂Δ
  8. Measurementͱ͸ • iOS10͔Βར༻Ͱ͖ΔFoundationͷίϯϙʔωϯτ • Swift2.3Ͱ͸NSMeasurement • Swift3.0Ͱ͸Measurement • ܭྔͷ֓೦Λදݱͨ͠ΫϥεͰྔͱ୯ҐΛऔΓѻ͏

  9. Կ͕Ͱ͖Δ͔ʁ • ͋Δܭྔͷ୯ҐΛผͷ୯Ґ΁ม׵Ͱ͖Δ • 15 km <-> 9.32 mile •

    30 kg <-> 13.6 lb (ϙϯυ) • localeʹ߹Θͤͯద੾ͳ୯ҐΛϑΥʔϚοτ • ΧελϜͷ୯ҐɺܭྔΛ࡞੒
  10. 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
  11. 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
  12. 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)
  13. MeasurementΛߏ੒͢ ΔΫϥε • Measurement(ܭྔ) • Unit(୯Ґ) • Dimension(෺ཧྔͷ࣍ݩ) • ந৅Ϋϥε

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

    UnitTypingSpeed: Dimension { static let punchPerSecond = UnitTypingSpeed(symbol: "punch/s") static let charsPerMinute = UnitTypingSpeed(symbol: "chars/min", converter: UnitPunchToCharConverter())
  15. ࢖ͬͯΈͯͷײ૝ ࢖͍ॴ • ͋Δ୯ҐͷܭྔΛผͷ୯ҐͰද͍ࣔͨ͠৔߹ • ஍ҬͰ୯ҐΛ੾Γସ͍͑ͨ৔߹ ࢖Θͳͯ͘΋͍͍(࢖͑ͳ͍)ॴ • ୯ҐΛ୯ͳΔϥϕϧͱͯ͠ද͍ࣔͨ͠৔߹ •

    ֎෦ཁૉͰൺ཰͕มΘΔͳͲɺ୯७ͳม׵Ͱ͸ͳ͍৔߹ (୯ՁͳͲ͸ҝସมಈͷӨڹΛड͚Δ(20 $/lb => 2500 ¥/kg))
  16. ࢖͍ॴ

  17. ͓·͚ ୯Ґͷॻ͖ํ͸ࠃࡍతʹܾΊΒΕ͓ͯΓɺ਺஋ͷޙʹ൒֯̍Ϛε ۭ͚ɺ୯ҐΛॻ͘ɻྫ͑͹ɺ1.50 kgͱॻ͘ɻ — ग़యݩ: Wikipedia