Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Measurement And Unitを試した話
Search
Atsushi Uchida (utwang)
July 31, 2016
Programming
0
550
Measurement And Unitを試した話
集まれSwift好き!Swift愛好会 vol9での発表資料です。
Atsushi Uchida (utwang)
July 31, 2016
Tweet
Share
More Decks by Atsushi Uchida (utwang)
See All by Atsushi Uchida (utwang)
Apple公式ガイドを さっくりと読んでまとめてみた UIScrollView編
utwang
0
220
Apple公式ガイドさっくりまとめ ファイルシステム編
utwang
0
34
頻出pryコマンド
utwang
0
120
設計ポイントの 比較で知る Backbone.js
utwang
9
6.9k
Other Decks in Programming
See All in Programming
PagerDuty を軸にした On-Call 構築と運用課題の解決 / PagerDuty Japan Community Meetup 4
horimislime
1
110
開発効率向上のためのリファクタリングの一歩目の選択肢 ~コード分割~ / JJUG CCC 2024 Fall
ryounasso
0
360
EventSourcingの理想と現実
wenas
6
2.1k
Importmapを使ったJavaScriptの 読み込みとブラウザアドオンの影響
swamp09
4
1.2k
GCCのプラグインを作る / I Made a GCC Plugin
shouth
1
150
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
23k
offers_20241022_imakiire.pdf
imakurusu
2
360
cXML という電子商取引の トランザクションを支える プロトコルと向きあっている話
phigasui
3
2.2k
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
1
290
Why Spring Matters to Jakarta EE - and Vice Versa
ivargrimstad
0
960
Kaigi on Rails 2024 - Rails APIモードのためのシンプルで効果的なCSRF対策 / kaigionrails-2024-csrf
corocn
5
3.3k
外部システム連携先が10を超えるシステムでのアーキテクチャ設計・実装事例
kiwasaki
1
220
Featured
See All Featured
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
StorybookのUI Testing Handbookを読んだ
zakiyama
26
5.2k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
The Art of Programming - Codeland 2020
erikaheidi
51
13k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
A better future with KSS
kneath
238
17k
Side Projects
sachag
452
42k
Code Reviewing Like a Champion
maltzj
519
39k
How to Think Like a Performance Engineer
csswizardry
19
1.1k
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
Transcript
Measurement And Unit Λࢼͨ͠ 2016/7/31 ू·ΕSwift͖ʂSwiftѪձ vol9
ࣗݾհ ాರ೭ (@utwang) • ϑϦʔϥϯεΤϯδχΞ כϲ࡚ࡏॅ • Swift, Rails, Javascript
• εϊʔϘʔυ͖ • ݸਓͰυϥΠϒͷׂΓצΞϓϦͩ͠·ͨ͠
׆ಈ ԣपลͰษڧձ/ΠϕϯτΛͬͯ·͢ • Railsͷ͘͘ձ/Swiftͷ͘͘ձ
• 8/21() Ϋϩʔϥʔͷӡ༻ʹ·ͭΘΔLTΠϕϯτ εϐʔΧʔɺઈࢍืूதͰ͢
ΞδΣϯμ • Ϟνϕʔγϣϯ • Measurementͱ • Կ͕Ͱ͖Δ͔ʁ • Measurement/Unitͷ͍ํ •
MeasurementΛߏ͢ΔΫϥε • ͬͯΈͯͷײ
Xcode8 beta3Ͱࢼͨ͠༰Ͱ͢ɻ ਖ਼ࣜϦϦʔε൛Ͱ༰͕ҟͳΔՄೳੑ͕͋Γ·͢ɻ
Ϟνϕʔγϣϯ • ݸਓͰ࡞ͬͨΞϓϦͷӳޠରԠ • ݴޠಉ͡Ͱ୯Ґ͕ҟͳΔ߹͕͋ Δ͕ɺશ෦ϩʔΧϥΠζରԠ͢Δͷ େม • ΞϝϦΧ: ӳޠ
/mile /gallon(US) /$ • ΠΪϦε: ӳޠ /mile /gallon(UK) /£ • Χφμɹ: ӳޠ /km /liter /CA$ • WWDC2016Ͱൃද͞Ε ͨ"Measurement And Unit"ΛௐΔ
Measurementͱ • iOS10͔Βར༻Ͱ͖ΔFoundationͷίϯϙʔωϯτ • Swift2.3ͰNSMeasurement • Swift3.0ͰMeasurement • ܭྔͷ֓೦Λදݱͨ͠ΫϥεͰྔͱ୯ҐΛऔΓѻ͏
Կ͕Ͱ͖Δ͔ʁ • ͋Δܭྔͷ୯ҐΛผͷ୯ҐมͰ͖Δ • 15 km <-> 9.32 mile •
30 kg <-> 13.6 lb (ϙϯυ) • localeʹ߹Θͤͯదͳ୯ҐΛϑΥʔϚοτ • ΧελϜͷ୯ҐɺܭྔΛ࡞
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
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
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)
MeasurementΛߏ͢ ΔΫϥε • Measurement(ܭྔ) • Unit(୯Ґ) • Dimension(ཧྔͷ࣍ݩ) • நΫϥε
• UnitXXX (UnitAngle, UnitLength, UnitVolume ...) • Dimensionͷ۩Ϋϥε • UnitConverter
MeasurementΛߏ͢ ΔΫϥε ৽͍͠ཧྔͷ࣍ݩΛ࡞Δ߹ • DimensionΛܧঝͨ͠UnitTypingSpeed Λ࡞ • ֤୯Ґͱͯ͠UnitTypingSpeedܕΛࣗ Λฦ͢ϓϩύςΟΛ༻ҙ͢Δ class
UnitTypingSpeed: Dimension { static let punchPerSecond = UnitTypingSpeed(symbol: "punch/s") static let charsPerMinute = UnitTypingSpeed(symbol: "chars/min", converter: UnitPunchToCharConverter())
ͬͯΈͯͷײ ͍ॴ • ͋Δ୯ҐͷܭྔΛผͷ୯ҐͰද͍ࣔͨ͠߹ • ҬͰ୯ҐΛΓସ͍͑ͨ߹ Θͳ͍͍ͯ͘(͑ͳ͍)ॴ • ୯ҐΛ୯ͳΔϥϕϧͱͯ͠ද͍ࣔͨ͠߹ •
֎෦ཁૉͰൺ͕มΘΔͳͲɺ୯७ͳมͰͳ͍߹ (୯ՁͳͲҝସมಈͷӨڹΛड͚Δ(20 $/lb => 2500 ¥/kg))
͍ॴ
͓·͚ ୯Ґͷॻ͖ํࠃࡍతʹܾΊΒΕ͓ͯΓɺͷޙʹ֯̍Ϛε ۭ͚ɺ୯ҐΛॻ͘ɻྫ͑ɺ1.50 kgͱॻ͘ɻ — ग़యݩ: Wikipedia