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

あらゆる情報を
楽に正しく String にフォーマットする 〜令和2021年から脱却せよ〜 / Easily and correctly format any information into a String

あらゆる情報を
楽に正しく String にフォーマットする 〜令和2021年から脱却せよ〜 / Easily and correctly format any information into a String

あらゆる情報を
楽に正しく String にフォーマットする 〜令和2021年から脱却せよ〜 / Easily and correctly format any information into a String

iOSDC Japan 2021 
Day 2
2021/09/19 11:30〜 Track D レギュラートーク(40分)
https://fortee.jp/iosdc-japan-2021/proposal/fa01a205-29bd-413d-8ba8-257555d4c7d0

https://www.youtube.com/watch?v=otyiNlEAeRI

treastrain / Tanaka Ryoga

September 19, 2021
Tweet

More Decks by treastrain / Tanaka Ryoga

Other Decks in Programming

Transcript

  1. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    USFBTUSBJO5BOBLB3ZPHB
    ͋ΒΏΔ৘ใΛ

    ָʹਖ਼͘͠4USJOHʹϑΥʔϚοτ͢Δ
    ʙྩ࿨೥͔Β୤٫ͤΑʙ
    1
    iOSDC Japan 2021

    Day 2
    #iosdc #d

    View full-size slide

  2. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ͜ͷηογϣϯͰ෼͔Δ͜ͱ
    $POUFOUT
    w ͳͥ'PSNBUUFSΛ࢖ͬͨ΄͏͕Α͍ͷ͔
    w J04Ͱ௥Ճ͞Εͨ৽ػೳ
    w J04ʢɾNBD04ɾUW04ɾXBUDI04ʣͰ༻ҙ͞Ε͍ͯΔ

    'PSNBUUFSͷ͢΂ͯͷ࢖༻ྫΛ঺հ
    w ઈରʹͯ͠͸ͳΒͳ͍'PSNBUUFSͷ࢖͍ํ
    2

    View full-size slide

  3. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 3

    View full-size slide

  4. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 4

    View full-size slide

  5. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 5

    View full-size slide

  6. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 6

    View full-size slide

  7. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 7
    Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.

    View full-size slide

  8. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    4XJGU J04 XBUDI04 $PSF/'$
    044

    ɹɹJU`TNZNPUIFSUPOHVF
    ɹɹɹɹɹɹɹɹɹɹ!USFBTUSBJOɹɹIUUQTUSFUKQ



    %F/"$P -UE

    ɹɹJ04"QQ&OHJOFFSʢ"QSJM$VSSFOUʣ
    USFBTUSBJO5BOBLB3ZPHB
    "CPVUNF
    8

    View full-size slide

  9. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    +BQBO/'$3FBEFS
    $IFDLUIFCBMBODFPGZPVSFNPOFZDBSEPOZPVSJ1IPOF
    9
    J1IPOFͰిࢠϚωʔͷ

    ࢒ߴɾར༻ཤྺͷಡΈऔΓ
    J1IPOFͰಡΈऔͬͨཤྺΛ

    J$MPVEܦ༝ͰଞͷσόΠε͔Β֬ೝͰ͖Δ
    ೥݄ϦϦʔεɹμ΢ϯϩʔυ਺,
    !+BQBO/'$3FBEFS
    J04"QQ

    View full-size slide

  10. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    53&5+BQBO/'$3FBEFS
    .*5-JDFODF
    IUUQTHJUIVCDPNUSFBTUSBJO53&5+BQBO/'$3FBEFS
    +BQBO/'$3FBEFSͷίΞ෦෼Λ044Խͨ͠ϥΠϒϥϦ
    *OJUJBMDPNNJU೥݄೔ɹ4UBST🌟೥݄೔࣌఺
    10
    044-JCSBSZ

    View full-size slide

  11. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    4XJGU J04 XBUDI04 $PSF/'$
    044

    ɹɹJU`TNZNPUIFSUPOHVF
    ɹɹɹɹɹɹɹɹɹɹ!USFBTUSBJOɹɹIUUQTUSFUKQ



    %F/"$P -UE

    ɹɹJ04"QQ%FWFMPQFSʢ"QSJM$VSSFOUʣ
    USFBTUSBJO5BOBLB3ZPHB
    "CPVUNF
    11

    View full-size slide

  12. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ීஈऔΓѻ͏͞·͟·ͳσʔλ
    7BSJPVTEBUBUIBUXFVTFPOBEBJMZCBTJT
    12
    ೔෇
    ೔࣌
    ࣌ؒ
    ࣌ࠁ
    ௨՟
    ඦ෼཰ʢʣ
    σʔλ༰ྔ
    ਓͷ໊લ
    ࢦ਺දه
    Թ౓
    ଎౓
    ࣭ྔ
    ໘ੵ
    ֯౓
    ిѹ
    ѹྗ
    Ճ଎౓
    ిՙ
    ௕͞
    ిྲྀ
    ೱ౓
    ిؾ఍߅
    प೾਺ ΤωϧΪʔ
    ମੵ
    ిྗ
    র౓
    ೩අ
    ظؒ

    View full-size slide

  13. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 13
    ? ? ? ? ? ?
    Date
    String

    View full-size slide

  14. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 14
    NumberFormatter | Apple Developer Documentation

    View full-size slide

  15. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ͋ͳ͕ͨ஌͍ͬͯΔ˓˓˓'PSNBUUFS͸ʁ
    8IBUl'PSNBUUFSzEPZPVLOPX
    15

    View full-size slide

  16. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ͜ͷηογϣϯͰ෼͔Δ͜ͱ
    $POUFOUT
    w ͳͥ'PSNBUUFSΛ࢖ͬͨ΄͏͕Α͍ͷ͔
    w J04Ͱ௥Ճ͞Εͨ৽ػೳ
    w J04ʢɾNBD04ɾUW04ɾXBUDI04ʣͰ༻ҙ͞Ε͍ͯΔ

    'PSNBUUFSͷ͢΂ͯͷ࢖༻ྫΛ঺հ
    w ઈରʹͯ͠͸ͳΒͳ͍'PSNBUUFSͷ࢖͍ํ
    16

    View full-size slide

  17. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ͳͥ'PSNBUUFSΛ࢖ͬͨ΄͏͕Α͍ͷ͔
    8IZTIPVMEXFVTFl'PSNBUUFSz
    w ࣗ෼ͰॲཧΛ࣮૷͢Δඞཁ͕ͳ͘ͳΔ
    w ޻਺Λ୹ॖͰ͖Δ
    w ߟྀ࿙ΕΛ๷͛Δ
    w ϩʔΧϥΠζ·Ͱڧྗʹαϙʔτ͞Ε͍ͯΔ
    w 04ଆͰαϙʔτ͕Ξοϓσʔτ͞ΕΔ
    17

    View full-size slide

  18. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ಠ࣮ࣗ૷͕ෆཁͰ͋Δ
    /POFFEGPSQSPQSJFUBSZJNQMFNFOUBUJPO
    w ਺ࣈʢ*OU΍%PVCMFʣ͔Β4USJOH΁ͷϑΥʔϚοτ
    w ܻ͓͖ʹʮ ʯΛೖΕΔ
    18
    // 1000 ͷͱ͖͸ "1,000"


    // 1000000 ͷͱ͖͸ "1,000,000"


    // 3141.59 ͷͱ͖͸ "3,141.59"

    View full-size slide

  19. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ಠ࣮ࣗ૷͕ෆཁͰ͋Δ
    /POFFEGPSQSPQSJFUBSZJNQMFNFOUBUJPO
    w ਺ࣈʢ*OU΍%PVCMFʣ͔Β4USJOH΁ͷϑΥʔϚοτ
    w ܻ͓͖ʹʮ ʯΛೖΕΔ
    19
    let formatter = NumberFormatter()


    formatter.numberStyle = .decimal


    print(formatter.string(from: 1521)) // Optional("1,521")

    View full-size slide

  20. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ಠ࣮ࣗ૷͕ෆཁͰ͋Δ
    /POFFEGPSQSPQSJFUBSZJNQMFNFOUBUJPO
    w ਺ࣈͷϑΥʔϚολʔҎ֎΋

    ๛෋ʹ༻ҙ͞Ε͍ͯΔ
    20

    View full-size slide

  21. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ϩʔΧϥΠζͷαϙʔτ
    4VQQPSUGPSMPDBMJ[BUJPO
    w ͜ͷܭࢉػʹೖྗ͞Εͨ਺ࣈ͸͍͔ͭ͘ʁ
    w ਖ਼ղ͸

    ʮ͍ͪͯΜ͝ʹ͍ͪʯ
    21

    View full-size slide

  22. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ϩʔΧϥΠζͷαϙʔτ
    4VQQPSUGPSMPDBMJ[BUJPO
    w ͜ͷJ1IPOFͷઃఆ
    w ʮӳޠʯʮϑϥϯεʯʮ࿨ྐྵʯ
    22
    print(Locale.current) // en_FR (current)


    let formatter = NumberFormatter()


    formatter.numberStyle = .decimal


    print(formatter.string(from: 1.521)) // Optional("1,521")

    View full-size slide

  23. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ϩʔΧϥΠζͷαϙʔτ
    4VQQPSUGPSMPDBMJ[BUJPO
    w ͜ͷJ1IPOFͷઃఆ
    w ʮΞϥϏΞޠʯʮΤδϓτʯ
    23
    print(Locale.current) // ar_EG (current)


    let formatter = NumberFormatter()


    formatter.numberStyle = .decimal


    print(formatter.string(from: 1.521)) // Optional(“١٫٥٢١")

    View full-size slide

  24. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ͜ͷηογϣϯͰ෼͔Δ͜ͱ
    $POUFOUT
    w ͳͥ'PSNBUUFSΛ࢖ͬͨ΄͏͕Α͍ͷ͔
    w J04Ͱ௥Ճ͞Εͨ৽ػೳ
    w J04ʢɾNBD04ɾUW04ɾXBUDI04ʣͰ༻ҙ͞Ε͍ͯΔ

    'PSNBUUFSͷ͢΂ͯͷ࢖༻ྫΛ঺հ
    w ઈରʹͯ͠͸ͳΒͳ͍'PSNBUUFSͷ࢖͍ํ
    24

    View full-size slide

  25. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    'PSNBU4UZMF
    /PPWFSWJFXBWBJMBCMF
    25
    /// A type that can convert a given data type into a representation.


    @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)


    public protocol FormatStyle : Decodable, Encodable, Hashable

    View full-size slide

  26. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    'PSNBU4UZMF
    /PPWFSWJFXBWBJMBCMF
    26
    let formatter = NumberFormatter()


    formatter.numberStyle = .decimal


    print(formatter.string(from: 1521)) // Optional("1,521")
    print(1521.formatted()) // "1,521"

    View full-size slide

  27. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    'PSNBU4UZMF
    /PPWFSWJFXBWBJMBCMF
    27
    struct ContentView: View {


    var body: some View {


    // Formatted Number: 1,521


    Text("Formatted Number: \(1521.formatted())")


    }


    }

    View full-size slide

  28. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ͜ͷηογϣϯͰ෼͔Δ͜ͱ
    $POUFOUT
    w ͳͥ'PSNBUUFSΛ࢖ͬͨ΄͏͕Α͍ͷ͔
    w J04Ͱ௥Ճ͞Εͨ৽ػೳ
    w J04ʢɾNBD04ɾUW04ɾXBUDI04ʣͰ༻ҙ͞Ε͍ͯΔ

    'PSNBUUFSͷ͢΂ͯͷ࢖༻ྫΛ঺հ
    w ઈରʹͯ͠͸ͳΒͳ͍'PSNBUUFSͷ࢖͍ํ
    28

    View full-size slide

  29. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    'PSNBUBMMUIFUIJOHT

    l͢΂ͯΛϑΥʔϚοτ͢Δz
    29
    'PSNBUUFST.BLFEBUBIVNBOGSJFOEMZ"QQMF88%$

    View full-size slide

  30. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 30
    123

    View full-size slide

  31. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 31
    Formatters
    40
    1
    Numbers & Currency
    Integer
    ੔਺
    123
    1234

    View full-size slide

  32. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ੔਺
    *OUFHFS
    w ਺ࣈΛ੔਺ʹͨ͠4USJOHΛಘΔ
    w ࢛ࣺޒೖɺ੾Γࣺͯɾ੾Γ্͛ͳͲͷΦϓγϣϯ
    32
    let formatter = NumberFormatter()


    formatter.numberStyle = .none


    print(formatter.string(from: 1234.5678)) // Optional("1235")


    formatter.roundingMode = .down


    print(formatter.string(from: 1234.5678)) // Optional("1234")

    View full-size slide

  33. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 33
    1,000
    Formatters
    40
    2
    Decimal
    Numbers & Currency
    ਐ਺
    123

    View full-size slide

  34. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ਐ਺
    %FDJNBM
    w ਺ࣈΛਐ਺දهʹͨ͠4USJOHΛಘΔ
    w ਺ࣈʹؔ͢Δ͋ΒΏΔઃఆΦϓγϣϯ
    w Oܻ۠੾Γɾখ਺఺Ͱ࢖͏ه߸ͷࢦఆ
    w ઌ಄ʹεϖʔεΛೖΕΔ
    w ༗ޮ਺ࣈܻ਺
    w FUDʜ
    34

    View full-size slide

  35. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ਐ਺
    %FDJNBM
    35
    let formatter = NumberFormatter()


    formatter.numberStyle = .decimal


    print(formatter.string(from: 1234.5678)) // Optional("1,234.568")


    print(formatter.groupingSize) // 3


    print(formatter.groupingSeparator) // Optional(",")


    print(formatter.decimalSeparator) // Optional(".")


    formatter.formatWidth = 5


    print(formatter.string(from: 1)) // Optional(" 1")


    print(formatter.string(from: 12)) // Optional(" 12")


    print(formatter.string(from: 123)) // Optional(" 123")


    print(formatter.string(from: 1234)) // Optional("1,234")


    formatter.maximumSignificantDigits = 5


    print(formatter.string(from: 1234.5678)) // Optional("1,234.6")

    View full-size slide

  36. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 36
    50%
    Formatters
    40
    3
    Percent
    ύʔηϯτʢඦ෼཰ʣ Numbers & Currency
    123

    View full-size slide

  37. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ύʔηϯτʢඦ෼཰ʣ
    1FSDFOU
    37
    let formatter = NumberFormatter()


    formatter.numberStyle = .percent


    print(formatter.string(from: 0.1)) // Optional("10%")


    print(formatter.string(from: 0.05)) // Optional("5%")


    print(formatter.string(from: -0.28)) // Optional("-28%")


    formatter.locale = Locale(identifier: "ar_EG")


    print(formatter.string(from: 0.01)) // Optional("١٪")


    print(formatter.percentSymbol) // Optional("٪")


    formatter.locale = Locale(identifier: "ja_JP")


    formatter.percentSymbol = "ύʔηϯτ"


    print(formatter.string(from: 0.03)) // Optional("3ύʔηϯτ")


    // available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)


    print(0.02.formatted(.percent)) // 2%

    View full-size slide

  38. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 38
    1.62E2
    Formatters
    40
    4
    Scientific
    Պֶʢࢦ਺ʣදه Numbers & Currency
    123

    View full-size slide

  39. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    Պֶʢࢦ਺ʣදه
    4DJFOUJ
    fi
    D
    39
    let formatter = NumberFormatter()


    formatter.numberStyle = .scientific


    print(formatter.string(from: 162)) // Optional("1.62E2")


    formatter.exponentSymbol = "e"


    print(formatter.string(from: 31.4)) // Optional("3.14e1")

    View full-size slide

  40. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 40
    two thousand

    twenty-one
    Formatters
    40
    5
    Spell Out
    ௲Γ Numbers & Currency
    123

    View full-size slide

  41. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ௲Γ
    4QFMM0VU
    41
    let formatter = NumberFormatter()


    formatter.numberStyle = .spellOut


    formatter.locale = Locale(identifier: "ja_JP")


    print(formatter.string(from: 162)) // Optional("ඦ࿡ेೋ")


    print(formatter.string(from: 31.4)) // Optional("ࡾेҰɾ࢛")


    print(formatter.string(from: -256)) // Optional("ϚΠφεೋඦޒे࿡")


    formatter.locale = Locale(identifier: "en_US")


    print(formatter.string(from: 162)) // Optional("one hundred sixty-two")


    print(formatter.string(from: 31.4)) // Optional("thirty-one point four")


    print(formatter.string(from: -256)) // Optional("minus two hundred fifty-six")

    View full-size slide

  42. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 42
    1st
    Formatters
    40
    6
    Ordinal
    ং਺ Numbers & Currency
    123

    View full-size slide

  43. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ং਺
    0SEJOBM
    43
    let formatter = NumberFormatter()


    formatter.numberStyle = .ordinal


    formatter.locale = Locale(identifier: "en_US")


    print(formatter.string(from: 1)) // Optional("1st")


    print(formatter.string(from: 2)) // Optional("2nd")


    print(formatter.string(from: 3)) // Optional("3rd")


    print(formatter.string(from: 4)) // Optional("4th")


    formatter.locale = Locale(identifier: "ja_JP")


    print(formatter.string(from: 1)) // Optional("ୈ1")


    print(formatter.string(from: 2)) // Optional("ୈ2")


    print(formatter.string(from: 3)) // Optional("ୈ3")


    print(formatter.string(from: 4)) // Optional("ୈ4")

    View full-size slide

  44. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 44
    ¥1,538
    Formatters
    40
    7
    Currency
    ௨՟ Numbers & Currency

    View full-size slide

  45. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ௨՟
    $VSSFODZ
    45
    let formatter = NumberFormatter()


    formatter.numberStyle = .currency


    formatter.locale = Locale(identifier: "ja_JP")


    print(formatter.string(from: 100)) // Optional("¥100")


    formatter.locale = Locale(identifier: "en_US")


    print(formatter.string(from: 100)) // Optional("$100.00")


    formatter.locale = Locale(identifier: "zh_HK")


    print(formatter.string(from: 100)) // Optional("HK$100.00")


    formatter.maximumFractionDigits = 0


    print(formatter.string(from: 100)) // Optional("HK$100")


    // available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)


    print(120.formatted(.currency(code: "JPY"))) // ¥120

    View full-size slide

  46. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 46
    Formatters
    40
    8
    Currency Plural
    ௨՟ʢ୯Ґෳ਺ܗରԠʣ Numbers & Currency
    226.78

    US dollars

    View full-size slide

  47. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ௨՟ʢ୯Ґෳ਺ܗରԠʣ
    $VSSFODZ1MVSBM
    47
    let formatter = NumberFormatter()


    formatter.numberStyle = .currencyPlural


    formatter.locale = Locale(identifier: "ja_JP")


    print(formatter.string(from: 100)) // Optional("100 ԁ")


    formatter.locale = Locale(identifier: "en_JP")


    print(formatter.string(from: 100)) // Optional("100 Japanese yen")


    formatter.locale = Locale(identifier: "en_US")


    print(formatter.string(from: 100)) // Optional("100.00 US dollars")


    formatter.locale = Locale(identifier: "zh_HK")


    print(formatter.string(from: 100)) // Optional("100.00 ߓݩ")


    formatter.locale = Locale(identifier: "en_HK")


    print(formatter.string(from: 100)) // Optional("100.00 Hong Kong dollars")

    View full-size slide

  48. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 48
    JPY 1,538
    Formatters
    40
    9
    Currency

    ISO Code
    ௨՟ίʔυ Numbers & Currency

    View full-size slide

  49. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ௨՟ίʔυ
    $VSSFODZ*40$PEF
    49
    let formatter = NumberFormatter()


    formatter.numberStyle = .currencyISOCode


    formatter.locale = Locale(identifier: "ja_JP")


    print(formatter.string(from: 100)) // Optional("JPY 100")


    formatter.locale = Locale(identifier: "en_US")


    print(formatter.string(from: 100)) // Optional("USD 100.00")


    formatter.locale = Locale(identifier: "en_GB")


    print(formatter.string(from: 100)) // Optional("GBP 100.00")


    formatter.locale = Locale(identifier: "zh_HK")


    print(formatter.string(from: 100)) // Optional("HKD 100.00")

    View full-size slide

  50. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 50
    (
    ¥1,538
    )
    Formatters
    40
    10
    Currency
    Accounting
    ձܭ Numbers & Currency

    View full-size slide

  51. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ձܭ
    $VSSFODZ"DDPVOUJOH
    51
    let formatter = NumberFormatter()


    formatter.numberStyle = .currencyAccounting


    print(formatter.string(from: 1537)) // Optional("¥1,537")


    print(formatter.string(from: -1537)) // Optional("(¥1,537)")


    print(formatter.negativePrefix) // Optional("(¥")


    print(formatter.negativeSuffix) // Optional(")")


    formatter.negativePrefix = "△"


    formatter.negativeSuffix = ""


    print(formatter.string(from: -1200)) // Optional("△1,200")

    View full-size slide

  52. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 52
    Formatters
    40
    11
    Person’s Name
    Names
    ਓͷ໊લ
    AI
    ࢁా



    View full-size slide

  53. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ਓͷ໊લ
    1FSTPO`T/BNF
    53
    let formatter = PersonNameComponentsFormatter()


    var nameComponents = PersonNameComponents()


    nameComponents.familyName = "Tanaka"


    nameComponents.givenName = "Ryoga"


    nameComponents.nickname = "treastrain"


    formatter.locale = Locale(identifier: "en_US")


    print(formatter.string(from: nameComponents)) // Ryoga Tanaka


    formatter.locale = Locale(identifier: "ja_JP")


    print(formatter.string(from: nameComponents)) // Tanaka Ryoga


    formatter.style = .abbreviated


    print(formatter.string(from: nameComponents)) // TR


    // available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)


    print(nameComponents.formatted()) // Tanaka Ryoga


    print(nameComponents.formatted(.name(style: .short))) // treastrain


    print(nameComponents.formatted(.name(style: .medium))) // Tanaka Ryoga


    print(nameComponents.formatted(.name(style: .long))) // Tanaka Ryoga


    print(nameComponents.formatted(.name(style: .abbreviated))) // TR

    View full-size slide

  54. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ਓͷ໊લ
    1FSTPO`T/BNF
    54
    let formatter = PersonNameComponentsFormatter()


    var nameComponents = PersonNameComponents()


    nameComponents.familyName = "ᖂᖒ"


    nameComponents.givenName = "ᒇҰ"


    nameComponents.nameSuffix = "ઌੜ"


    nameComponents.nickname = "͍͑ͪΌΜ"


    formatter.style = .abbreviated


    print(formatter.string(from: nameComponents)) // ᖂᖒ


    formatter.style = .short


    print(formatter.string(from: nameComponents)) // ͍͑ͪΌΜ


    formatter.style = .medium


    print(formatter.string(from: nameComponents)) // ᖂᖒᒇҰ


    formatter.style = .long


    print(formatter.string(from: nameComponents)) // ᖂᖒᒇҰઌੜ


    // available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)


    print(nameComponents.formatted()) // ᖂᖒᒇҰ

    View full-size slide

  55. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ਓͷ໊લ
    1FSTPO`T/BNF
    55
    let formatter = PersonNameComponentsFormatter()


    var components = formatter.personNameComponents(from: "וম͞Μ ଠ࿠")


    print(components?.familyName) // Optional("וম͞Μ")


    print(components?.middleName) // nil


    print(components?.givenName) // Optional("ଠ࿠")


    components = formatter.personNameComponents(from: "John Appleseed")


    print(components?.familyName) // Optional("Appleseed")


    print(components?.middleName) // nil


    print(components?.givenName) // Optional("John")


    // iOSDC Japan 2020 - Day 0 - Track B


    // ͋ͳͨͷ஌Βͳ͍࿈བྷઌͷੈք by ೔޲ڧ / coffeegyunyu


    // Λͥͻࢀর͍ͩ͘͞

    View full-size slide

  56. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 56
    2021/09/19
    11
    :
    30
    :
    00
    Formatters
    40
    12
    Dates
    Dates & Times
    ೔࣌

    View full-size slide

  57. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ೔࣌
    %BUFT
    57
    let date = Date(timeIntervalSince1970: 1632018600)


    let formatter = DateFormatter()


    formatter.dateStyle = .medium


    formatter.timeStyle = .short


    formatter.locale = Locale(identifier: "ja_JP")


    print(formatter.string(from: date)) // 2021/09/19 11:30


    formatter.locale = Locale(identifier: "en_US")


    print(formatter.string(from: date)) // Sep 19, 2021 at 11:30 AM


    formatter.locale = Locale(identifier: "zh_HK")


    print(formatter.string(from: date)) // 2021೥9݄19೔ ্ޕ11:30


    // available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)


    print(date.formatted(date: .complete, time: .shortened)) // 2021೥9݄19೔ ೔༵೔ 11:30

    View full-size slide

  58. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ೔࣌
    %BUFT
    58
    let date = Date(timeIntervalSince1970: 1632018600)


    let formatter = DateFormatter()


    formatter.locale = Locale(identifier: "ja_JP")


    formatter.calendar = Calendar(identifier: .japanese)


    formatter.dateFormat = "GyMMM"


    print(formatter.string(from: date)) // ྩ࿨39݄


    formatter.locale = Locale(identifier: "en_US")


    formatter.calendar = Calendar(identifier: .gregorian)


    formatter.dateFormat = "GyMMM"


    print(formatter.string(from: date)) // AD2021Sep

    View full-size slide

  59. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 59
    ˞࣮ࡍͷݪҼʹ͍ͭͯ͸ෆ໌Ͱ͢ɻ

    ˞ݱࡏ͸मਖ਼͞Ε͍ͯ·͢ɻ

    View full-size slide

  60. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ೔࣌
    %BUFT
    60
    let date = Date(timeIntervalSince1970: 1632018600)


    let formatter = DateFormatter()


    formatter.locale = Locale(identifier: "ja_JP")


    formatter.calendar = Calendar(identifier: .japanese)


    formatter.setLocalizedDateFormatFromTemplate("GyMMM")


    print(formatter.string(from: date)) // ྩ࿨3೥9݄


    formatter.locale = Locale(identifier: "en_US")


    formatter.calendar = Calendar(identifier: .gregorian)


    formatter.setLocalizedDateFormatFromTemplate("GyMMM")


    print(formatter.string(from: date)) // Sep 2021 AD

    View full-size slide

  61. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 61
    2 days,

    1 hr, 50 min
    Formatters
    40
    13
    Quantities of
    Time
    Dates & Times
    ࣌ؒ

    View full-size slide

  62. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ࣌ؒ
    2VBOUJUJFTPG5JNF
    62
    // ͸΍Ϳ͞44߸ ৽വؗ๺ే~౦ژ ॴཁ࣌ؒ


    let components = DateComponents(hour: 3, minute: 58)


    let formatter = DateComponentsFormatter()


    formatter.calendar?.locale = Locale(identifier: "ja_JP")


    formatter.unitsStyle = .positional


    print(formatter.string(from: components)) // Optional("3:58")


    formatter.unitsStyle = .abbreviated


    print(formatter.string(from: components)) // Optional("3࣌ؒ58෼")


    formatter.unitsStyle = .spellOut


    print(formatter.string(from: components)) // Optional("ࡾ࣌ؒ ޒेീ෼")


    formatter.calendar?.locale = Locale(identifier: "en_US")


    formatter.unitsStyle = .positional


    print(formatter.string(from: components)) // Optional("3:58")


    formatter.unitsStyle = .abbreviated


    print(formatter.string(from: components)) // Optional("3h 58m")


    formatter.unitsStyle = .spellOut


    print(formatter.string(from: components)) // Optional("three hours, fifty-eight minutes")

    View full-size slide

  63. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 63
    2 days ago
    Formatters
    40
    14
    Relative

    Date or Time
    Dates & Times
    ૬ରతͳ೔࣌

    View full-size slide

  64. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ૬ରతͳ೔࣌
    3FMBUJWF%BUFPS5JNF
    64
    let formatter = RelativeDateTimeFormatter()


    formatter.dateTimeStyle = .numeric


    print(formatter.localizedString(from: DateComponents(month: -1))) // 1 ͔݄લ


    print(formatter.localizedString(from: DateComponents(month: 0))) // 0 ͔݄ޙ


    print(formatter.localizedString(from: DateComponents(month: 1))) // 1 ͔݄ޙ


    formatter.dateTimeStyle = .named


    print(formatter.localizedString(from: DateComponents(month: -1))) // ઌ݄


    print(formatter.localizedString(from: DateComponents(month: 0))) // ࠓ݄


    print(formatter.localizedString(from: DateComponents(month: 1))) // དྷ݄

    View full-size slide

  65. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ૬ରతͳ೔࣌
    3FMBUJWF%BUFPS5JNF
    65
    let formatter = RelativeDateTimeFormatter()


    formatter.dateTimeStyle = .named


    print(formatter.localizedString(from: DateComponents(day: -3))) // 3 ೔લ


    print(formatter.localizedString(from: DateComponents(day: -2))) // Ұࡢ೔


    print(formatter.localizedString(from: DateComponents(day: -1))) // ࡢ೔


    print(formatter.localizedString(from: DateComponents(day: 0))) // ࠓ೔


    print(formatter.localizedString(from: DateComponents(day: 1))) // ໌೔


    print(formatter.localizedString(from: DateComponents(day: 2))) // ໌ޙ೔


    print(formatter.localizedString(from: DateComponents(day: 3))) // 3 ೔ޙ

    View full-size slide

  66. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 66
    Fri, Sep 17,

    2021 17
    :
    00



    Sun, Sep 19,

    2021 18
    :
    50
    Formatters
    40
    15
    Time Intervals
    Dates & Times
    ִ࣌ؒؒ

    View full-size slide

  67. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ִ࣌ؒؒ
    5JNF*OUFSWBMT
    67
    let startDate = Date(timeIntervalSince1970: 1631865600)


    let endDate = Date(timeIntervalSince1970: 1632045000)


    let formatter = DateIntervalFormatter()


    formatter.locale = Locale(identifier: "en_US")


    print(formatter.string(from: startDate, to: endDate)) // 9/17/21, 5:00 PM – 9/19/21, 6:50 PM


    formatter.locale = Locale(identifier: "zh_HK")


    print(formatter.string(from: startDate, to: endDate)) // 17/9/2021 Լޕ5:00ࢸ19/9/2021 Լޕ6:50


    formatter.locale = Locale(identifier: "ja_JP")


    print(formatter.string(from: startDate, to: endDate)) // 2021/09/17 17:00ʙ2021/09/19 18:50


    formatter.dateTemplate = "EEE, MMM d, ''yyyy"


    print(formatter.string(from: startDate, to: endDate)) // 2021೥9݄17೔(ۚ)ʙ19೔(೔)

    View full-size slide

  68. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 68
    2021
    -
    09
    -
    19

    T11
    :
    30
    :
    00
    +
    09
    :
    00
    Formatters
    40
    16
    ISO 8601 Format
    Dates & Times
    *40ܗࣜͷ೔࣌

    View full-size slide

  69. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    *40ܗࣜͷ೔࣌
    *40'PSNBU
    69
    let date = Date(timeIntervalSince1970: 1632018600)


    let formatter = ISO8601DateFormatter()


    print(formatter.string(from: date)) // 2021-09-19T02:30:00Z


    formatter.timeZone = TimeZone(identifier: "Asia/Tokyo")


    print(formatter.string(from: date)) // 2021-09-19T11:30:00+09:00


    // available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)


    print(date.formatted(.iso8601)) // 2021-09-19T02:30:00Z

    View full-size slide

  70. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 70
    31.25 GB
    Formatters
    40
    17
    Data Sizes
    Data Sizes
    σʔλαΠζ

    View full-size slide

  71. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    σʔλαΠζ
    %BUB4J[FT
    71
    // Xcode 12.5.1 ͷαΠζ: 29,509,386,405όΠτ


    let byteCount: Int64 = 29509386405


    let formatter = ByteCountFormatter()


    formatter.countStyle = .file


    print(formatter.string(fromByteCount: byteCount)) // 29.51 GB


    formatter.countStyle = .memory


    print(formatter.string(fromByteCount: byteCount)) // 27.48 GB


    formatter.countStyle = .file


    formatter.allowedUnits = .useMB


    print(formatter.string(fromByteCount: byteCount)) // 29,509.4 MB


    // available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)


    print(byteCount.formatted(.byteCount(style: .file))) // 29.51 GB

    View full-size slide

  72. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 72
    631.9 km
    Formatters
    40
    18
    Length
    Physical Dimension
    ௕͞

    View full-size slide

  73. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ௕͞
    -FOHUI
    73
    let formatter = MeasurementFormatter()


    formatter.locale = Locale(identifier: "ja_JP")


    var length = Measurement(value: 900, unit: .meters)


    print(formatter.string(from: length)) // 0.9 km


    length = length + Measurement(value: 631, unit: .kilometers)


    print(formatter.string(from: length)) // 631.9 km


    formatter.locale = Locale(identifier: "en_US")


    print(formatter.string(from: length)) // 392.644 mi

    View full-size slide

  74. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 74
    15.11 km²
    Formatters
    40
    19
    Area
    Physical Dimension
    ໘ੵ

    View full-size slide

  75. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ໘ੵ
    "SFB
    75
    let formatter = MeasurementFormatter()


    formatter.locale = Locale(identifier: "ja_JP")


    var area = Measurement(value: 15.11, unit: .squareKilometers)


    print(formatter.string(from: area)) // 15.11 km²


    area.convert(to: .ares)


    formatter.unitOptions = .providedUnit


    print(formatter.string(from: area)) // 151,100 a

    View full-size slide

  76. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    l௕͞zΛ࢖ͬͯ໘ੵ͸ٻΊΒΕΔ͔ʁ
    /P
    76
    let length1 = Measurement(value: 10, unit: .meters)


    let length2 = Measurement(value: 20, unit: .meters)


    let length3 = length1 + length2 // 30 m


    // Binary operator '*' cannot be applied to two 'Measurement' operands


    // let length4 = length1 * length2


    let length5 = 5 * length1 // 50 m

    View full-size slide

  77. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 77
    525 ml
    Formatters
    40
    20
    Volume
    Physical Dimension
    ମੵ

    View full-size slide

  78. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ମੵ
    7PMVNF
    78
    let formatter = MeasurementFormatter()


    formatter.locale = Locale(identifier: "ja_JP")


    let volume1 = Measurement(value: 525, unit: .milliliters)


    print(formatter.string(from: volume1)) // 0.525 L


    formatter.unitOptions = .providedUnit


    var volume2 = Measurement(value: 1, unit: .gallons)


    print(formatter.string(from: volume2)) // 1 gal


    volume2.convert(to: .imperialGallons)


    print(formatter.string(from: volume2)) // 0.833 gal Imp.

    View full-size slide

  79. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 79
    1.047 rad
    Formatters
    40
    21
    Angle
    Physical Dimension
    ֯౓

    View full-size slide

  80. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ֯౓
    "OHMF
    80
    let formatter = MeasurementFormatter()


    formatter.locale = Locale(identifier: "ja_JP")


    var angle = Measurement(value: 60, unit: .degrees)


    print(formatter.string(from: angle)) // 60౓


    angle.convert(to: .radians)


    print(formatter.string(from: angle)) // 1.047 rad

    View full-size slide

  81. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 81
    0.187 kg
    Formatters
    40
    22
    Mass
    Mass, Weight & Force
    ࣭ྔ

    View full-size slide

  82. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 82
    1,013.25 hPa
    Formatters
    40
    23
    Pressure
    Mass, Weight & Force
    ѹྗ

    View full-size slide

  83. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ࣭ྔɾѹྗ
    .BTT1SFTTVSF
    83
    let formatter = MeasurementFormatter()


    formatter.locale = Locale(identifier: "ja_JP")


    let mass = Measurement(value: 187, unit: .grams)


    print(formatter.string(from: mass)) // 0.187 kg


    let pressure = Measurement(value: 1013.25, unit: .hectopascals)


    print(formatter.string(from: pressure)) // 1,013.25 hPa

    View full-size slide

  84. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 84
    9.81 m/s²
    Formatters
    40
    24
    Acceleration
    Time & Motion
    Ճ଎౓

    View full-size slide

  85. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    Ճ଎౓
    "DDFMFSBUJPO
    85
    let formatter = MeasurementFormatter()


    formatter.locale = Locale(identifier: "ja_JP")


    var acceleration = Measurement(value: 1, unit: .gravity) // 1 G


    print(formatter.string(from: acceleration))


    acceleration.convert(to: .metersPerSecondSquared)


    print(formatter.string(from: acceleration)) // 9.81 m/s²

    View full-size slide

  86. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 86
    3.967 hr
    Formatters
    40
    25
    Duration
    Time & Motion
    ܧଓ࣌ؒ

    View full-size slide

  87. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 87
    50 Hz
    Formatters
    40
    26
    Frequency
    Time & Motion
    प೾਺

    View full-size slide

  88. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ܧଓ࣌ؒɾप೾਺
    %VSBUJPO'SFRVFODZ
    88
    let formatter = MeasurementFormatter()


    formatter.locale = Locale(identifier: "ja_JP")


    var duration = Measurement(value: 1, unit: .hours)


    duration = duration + Measurement(value: 30, unit: .minutes)


    print(formatter.string(from: duration)) // 5,400ඵ


    duration.convert(to: .hours)


    formatter.unitOptions = .providedUnit


    print(formatter.string(from: duration)) // 1.5࣌ؒ


    let frequency = Measurement(value: 50, unit: .hertz)


    print(formatter.string(from: frequency)) // 50 Hz

    View full-size slide

  89. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 89
    320 km/h
    Formatters
    40
    27
    Speed
    Time & Motion
    ଎͞

    View full-size slide

  90. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ଎͞
    4QFFE
    90
    let formatter = MeasurementFormatter()


    formatter.locale = Locale(identifier: "ja_JP")


    let speed = Measurement(value: 320, unit: .kilometersPerHour)


    print(formatter.string(from: speed)) // 320 km/h


    formatter.locale = Locale(identifier: "en_US")


    print(formatter.string(from: speed)) // 198.839 mph

    View full-size slide

  91. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 91
    9.8 J
    Formatters
    40
    28
    Energy
    Energy, Heat, & Light
    ΤωϧΪʔ

    View full-size slide

  92. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 92
    61 W
    Formatters
    40
    29
    Power
    Energy, Heat, & Light
    ిྗ

    View full-size slide

  93. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ΤωϧΪʔɾిྗ
    &OFSHZ1PXFS
    93
    let formatter = MeasurementFormatter()


    formatter.locale = Locale(identifier: "ja_JP")


    var energy = Measurement(value: 418400, unit: .joules)


    print(formatter.string(from: energy)) // 100 kcal


    energy.convert(to: .kilowattHours)


    formatter.unitOptions = .providedUnit


    print(formatter.string(from: energy)) // 0.116 kWh


    var power = Measurement(value: 61, unit: .watts)


    print(formatter.string(from: power)) // 61 W


    power.convert(to: .horsepower)


    print(formatter.string(from: power)) // 0.082അྗ

    View full-size slide

  94. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 94
    273.15 K
    Formatters
    40
    30
    Temperature
    Energy, Heat, & Light
    Թ౓

    View full-size slide

  95. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 95
    500 lx
    Formatters
    40
    Illuminance
    31
    Energy, Heat, & Light
    র౓

    View full-size slide

  96. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    Թ౓ɾর౓
    5FNQFSBUVSF*MMVNJOBODF
    96
    let formatter = MeasurementFormatter()


    formatter.locale = Locale(identifier: "ja_JP")


    var temperature = Measurement(value: 273.15, unit: .kelvin)


    print(formatter.string(from: temperature)) // 0°C


    temperature.convert(to: .fahrenheit)


    formatter.unitOptions = .providedUnit


    print(formatter.string(from: temperature)) // 32°F


    let illuminance = Measurement(value: 500, unit: .lux)


    print(formatter.string(from: illuminance)) // 500 lx

    View full-size slide

  97. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 97
    2,815 mAh
    Formatters
    40
    32
    Electric Charge
    Electricity
    ిՙ

    View full-size slide

  98. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 98
    500 mA
    Formatters
    40
    33
    Electric Current
    Electricity
    ిྲྀ

    View full-size slide

  99. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 99
    141.421 V
    Formatters
    40
    34
    Electric

    Potential Difference
    Electricity
    ిҐࠩʢిѹʣ

    View full-size slide

  100. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 100
    2.4 kΩ
    Formatters
    40
    35
    Electric Resistance
    Electricity
    ిؾ఍߅

    View full-size slide

  101. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ిՙɾిྲྀɾిҐࠩʢిѹʣɾిؾ఍߅
    &MFDUSJD$IBSHF$VSSFOU1PUFOUJBM%J
    ff
    FSFODF3FTJTUBODF
    101
    let formatter = MeasurementFormatter()


    formatter.locale = Locale(identifier: "ja_JP")


    let q = Measurement(value: 2815, unit: .milliampereHours)


    print(formatter.string(from: q)) // 2,815 mAh


    let i = Measurement(value: 0.5, unit: .milliamperes)


    print(formatter.string(from: i)) // 500 mA


    let e = Measurement(value: 141.421, unit: .volts)


    print(formatter.string(from: e)) // 141.421 V


    let r = Measurement(value: 2.4, unit: .kiloohms)


    print(formatter.string(from: r)) // 2,400 Ω

    View full-size slide

  102. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 102
    0.154 g/L
    Formatters
    40
    36
    Concentration of
    Mass
    Concentration &
    Dispersion
    ࣭ྔೱ౓

    View full-size slide

  103. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 103
    410.5 ppm
    Formatters
    40
    37
    Dispersion
    Concentration &
    Dispersion
    ෼ࢄ

    View full-size slide

  104. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ࣭ྔೱ౓ɾ෼ࢄ
    $PODFOUSBUJPOPG.BTT%JTQFSTJPO
    104
    let formatter = MeasurementFormatter()


    formatter.locale = Locale(identifier: "ja_JP")


    let concentrationOfMass = Measurement(value: 0.154,
    unit: .gramsPerLiter)


    print(formatter.string(from: concentrationOfMass)) // 0.154 g/L


    let dispersion = Measurement(value: 410.5, unit: .partsPerMillion)


    print(formatter.string(from: dispersion)) // 410.5 ppm

    View full-size slide

  105. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 105
    3.115 L/100km
    Formatters
    40
    38
    Fuel Efficiency
    Fuel Efficiency
    ೩අ

    View full-size slide

  106. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ೩අ
    'VFM&
    ff i
    DJFODZ
    106
    let formatter = MeasurementFormatter()


    formatter.locale = Locale(identifier: "ja_JP")


    let fuelEfficiency = Measurement(value: 3.115,
    unit: .litersPer100Kilometers)


    print(formatter.string(from: fuelEfficiency)) // 3.115 L/100km

    View full-size slide

  107. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 107
    29.104 GiB
    Formatters
    40
    39
    Amount of
    Information
    Data Storage
    ৘ใྔ

    View full-size slide

  108. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ৘ใྔ
    "NPVOUPG*OGPSNBUJPO
    108
    let formatter = MeasurementFormatter()


    formatter.locale = Locale(identifier: "ja_JP")


    var amount = Measurement(value: 29.51, unit: .gigabytes)


    print(formatter.string(from: amount)) // 29.51 GB


    amount.convert(to: .gigabits)


    print(formatter.string(from: amount)) // 236.08 Gb


    let byteCountFormatter = ByteCountFormatter()


    print(byteCountFormatter.string(from: amount)) // 29.51 GB

    View full-size slide

  109. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 109
    iPhone,

    iPad,

    and iPod touch
    Formatters
    40
    40
    Lists
    Lists
    Ϧετ

    View full-size slide

  110. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    Ϧετ
    -JTUT
    110
    let formatter = ListFormatter()


    var items = ["ΓΜ͝", "όφφ", "ΦϨϯδ"]


    formatter.locale = Locale(identifier: "ja_JP")


    print(formatter.string(from: items)) // Optional("ΓΜ͝ɺόφφɺΦϨϯδ")


    items = ["Apple", "Banana", "Orange"]


    formatter.locale = Locale(identifier: "en_US")


    print(formatter.string(from: items)) // Optional("Apple, Banana, and Orange")

    View full-size slide

  111. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    Ϧετ
    -JTUT
    111
    let formatter = ListFormatter()


    var items = ["೔ຊޠ", "ӳޠ", "εϖΠϯޠ"]


    formatter.locale = Locale(identifier: "ja_JP")


    print(formatter.string(from: items)) // Optional("೔ຊޠɺӳޠɺεϖΠϯޠ")


    formatter.locale = Locale(identifier: "es_ES")


    items = ["Japonés", "Inglés", "Español"]


    print(formatter.string(from: items)) // Optional("Japonés, Inglés y Español")


    items = ["Japonés", "Español", "Inglés"]


    print(formatter.string(from: items)) // Optional("Japonés, Español e Inglés")

    View full-size slide

  112. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    Ϧετ
    -JTUT
    112
    // ஋ஈҰཡ


    let items = [120, 250, 370, 490, 610, 730, 860, 980, 1100]


    let itemFormatter = NumberFormatter()


    itemFormatter.numberStyle = .currency


    let formatter = ListFormatter()


    formatter.itemFormatter = itemFormatter


    print(formatter.string(from: items))


    // Optional("¥120ɺ¥250ɺ¥370ɺ¥490ɺ¥610ɺ¥730ɺ¥860ɺ¥980ɺ¥1,100")

    View full-size slide

  113. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ·ͱΊ
    $POUFOUT
    w ͳͥ'PSNBUUFSΛ࢖ͬͨ΄͏͕Α͍ͷ͔
    w ࣗ෼ͰॲཧΛ࣮૷͢Δඞཁ͕ͳ͘ͳΔ
    w ޻਺Λ୹ॖͰ͖Δɾߟྀ࿙ΕΛ๷͛Δ
    w ϩʔΧϥΠζڧྗͳαϙʔτ
    w J04Ͱ௥Ճ͞Εͨ৽ػೳ
    w GPSNBUUFE
    ɺ'PSNBU4UZMFͰ୹͘ॻ͘͜ͱ͕Ͱ͖Δ
    w ඪ४Ͱ༻ҙ͞Ε͍ͯΔ'PSNBUUFSͷ͢΂ͯͷ࢖༻ྫ
    w 'PSNBUUFSͷଘࡏ͚ͩͰ΋஌ͬͯ΄͍͠
    113

    View full-size slide

  114. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ࢀߟจݙ
    3FGFSFODFT"DLOPXMFEHFNFOUT
    w 'PSNBUUFSTσʔλΛώϡʔϚϯϑϨϯυϦʔʹɹ"QQMF88%$

    IUUQTEFWFMPQFSBQQMFDPNWJEFPTQMBZXXED
    w ͋ͳͨͷ஌Βͳ͍࿈བྷઌͷੈքCZ!DP
    ff
    FFHZVOZVɹJ04%$+BQBO

    IUUQTGPSUFFKQJPTEDKBQBOQSPQPTBMBDGBDFFEBBDDCECGB
    w ʲ4XJGUʳ೔࣌ɺ਺ɺ௨՟ɺσʔλαΠζɺϦετɺਓͷ໊લɺ୯Ґ෇͖ͷ਺͔Β4USJOH΁ͷϑΥʔϚο
    τ͸ࣗ෼Ͱ࣮૷͠ͳ͍ͰCZ!USFBTUSBJOɹ2JJUBJ04"EWFOU$BMFOEBS

    IUUQTRJJUBDPNUSFBTUSBJOJUFNTFFDFGGBBE
    w 4USFBNMJOFZPVSMPDBMJ[FETUSJOHTɹ"QQMF88%$

    IUUQTEFWFMPQFSBQQMFDPNWJEFPTQMBZXXED
    114

    View full-size slide

  115. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved. 115
    J1IPOFͷྐྵ๏Λʮ࿨ྐྵʯʹ͢Δͱ࠶ݱͰ͖·͢

    View full-size slide

  116. Copyright © 2021 treastrain / Tanaka RyogaɹAll rights reserved.
    ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂɹɹɹɹɹɹɹɹɹIUUQTUSFUKQɹ⏩
    ͋ΒΏΔ৘ใΛ

    ָʹਖ਼͘͠4USJOHʹϑΥʔϚοτ͢Δ
    ʙྩ࿨೥͔Β୤٫ͤΑʙ
    116
    iOSDC Japan 2021

    Day 2
    #iosdc #d

    View full-size slide