550

# Measurement And Unitを試した話

July 31, 2016

## Transcript

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

• εϊʔϘʔυ޷͖ • ݸਓͰυϥΠϒ୅ͷׂΓצΞϓϦͩ͠·ͨ͠

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

MeasurementΛߏ੒͢ΔΫϥε • ࢖ͬͯΈͯͷײ૝

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

/mile /gallon(US) /\$ • ΠΪϦε: ӳޠ /mile /gallon(UK) /£ • Χφμɹ: ӳޠ /km /liter /CA\$ • WWDC2016Ͱൃද͞Ε ͨ"Measurement And Unit"Λௐ΂Δ

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))