Created
January 19, 2021 08:44
-
-
Save rogerluan/336cb2f2d164e18c171a661b44297195 to your computer and use it in GitHub Desktop.
Quick snippet to get all the Alpha-2 ISO country code as well as they localized display name, using native Swift lib (no imported external enums/manual entries).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var countries: [String:String] = [:] | |
for code in NSLocale.isoCountryCodes { | |
let id: String = Locale.identifier(fromComponents: [ | |
NSLocale.Key.countryCode.rawValue : code | |
]) | |
guard let name = (Locale.current as NSLocale).displayName(forKey: .identifier, value: id) else { continue } | |
countries[code] = name | |
} | |
for (index, countryCode) in countries.keys.sorted(by: { countries[$0]! < countries[$1]! }).enumerated() { | |
print("\(index + 1): \(countryCode) \(countries[countryCode]!)") | |
} | |
// This will produce the following output: | |
1: AF Afghanistan | |
2: AL Albania | |
3: DZ Algeria | |
4: AS American Samoa | |
5: AD Andorra | |
6: AO Angola | |
7: AI Anguilla | |
8: AQ Antarctica | |
9: AG Antigua & Barbuda | |
10: AR Argentina | |
11: AM Armenia | |
12: AW Aruba | |
13: AC Ascension Island | |
14: AU Australia | |
15: AT Austria | |
16: AZ Azerbaijan | |
17: BS Bahamas | |
18: BH Bahrain | |
19: BD Bangladesh | |
20: BB Barbados | |
21: BY Belarus | |
22: BE Belgium | |
23: BZ Belize | |
24: BJ Benin | |
25: BM Bermuda | |
26: BT Bhutan | |
27: BO Bolivia | |
28: BA Bosnia & Herzegovina | |
29: BW Botswana | |
30: BV Bouvet Island | |
31: BR Brazil | |
32: IO British Indian Ocean Territory | |
33: VG British Virgin Islands | |
34: BN Brunei | |
35: BG Bulgaria | |
36: BF Burkina Faso | |
37: BI Burundi | |
38: KH Cambodia | |
39: CM Cameroon | |
40: CA Canada | |
41: IC Canary Islands | |
42: CV Cape Verde | |
43: BQ Caribbean Netherlands | |
44: KY Cayman Islands | |
45: CF Central African Republic | |
46: EA Ceuta & Melilla | |
47: TD Chad | |
48: CL Chile | |
49: CN China mainland | |
50: CX Christmas Island | |
51: CP Clipperton Island | |
52: CC Cocos [Keeling] Islands | |
53: CO Colombia | |
54: KM Comoros | |
55: CG Congo - Brazzaville | |
56: CD Congo - Kinshasa | |
57: CK Cook Islands | |
58: CR Costa Rica | |
59: HR Croatia | |
60: CU Cuba | |
61: CW Curaçao | |
62: CY Cyprus | |
63: CZ Czechia | |
64: CI Côte d’Ivoire | |
65: DK Denmark | |
66: DG Diego Garcia | |
67: DJ Djibouti | |
68: DM Dominica | |
69: DO Dominican Republic | |
70: EC Ecuador | |
71: EG Egypt | |
72: SV El Salvador | |
73: GQ Equatorial Guinea | |
74: ER Eritrea | |
75: EE Estonia | |
76: SZ Eswatini | |
77: ET Ethiopia | |
78: FK Falkland Islands | |
79: FO Faroe Islands | |
80: FJ Fiji | |
81: FI Finland | |
82: FR France | |
83: GF French Guiana | |
84: PF French Polynesia | |
85: TF French Southern Territories | |
86: GA Gabon | |
87: GM Gambia | |
88: GE Georgia | |
89: DE Germany | |
90: GH Ghana | |
91: GI Gibraltar | |
92: GR Greece | |
93: GL Greenland | |
94: GD Grenada | |
95: GP Guadeloupe | |
96: GU Guam | |
97: GT Guatemala | |
98: GG Guernsey | |
99: GN Guinea | |
100: GW Guinea-Bissau | |
101: GY Guyana | |
102: HT Haiti | |
103: HM Heard & McDonald Islands | |
104: HN Honduras | |
105: HK Hong Kong | |
106: HU Hungary | |
107: IS Iceland | |
108: IN India | |
109: ID Indonesia | |
110: IR Iran | |
111: IQ Iraq | |
112: IE Ireland | |
113: IM Isle of Man | |
114: IL Israel | |
115: IT Italy | |
116: JM Jamaica | |
117: JP Japan | |
118: JE Jersey | |
119: JO Jordan | |
120: KZ Kazakhstan | |
121: KE Kenya | |
122: KI Kiribati | |
123: XK Kosovo | |
124: KW Kuwait | |
125: KG Kyrgyzstan | |
126: LA Laos | |
127: LV Latvia | |
128: LB Lebanon | |
129: LS Lesotho | |
130: LR Liberia | |
131: LY Libya | |
132: LI Liechtenstein | |
133: LT Lithuania | |
134: LU Luxembourg | |
135: MO Macao | |
136: MG Madagascar | |
137: MW Malawi | |
138: MY Malaysia | |
139: MV Maldives | |
140: ML Mali | |
141: MT Malta | |
142: MH Marshall Islands | |
143: MQ Martinique | |
144: MR Mauritania | |
145: MU Mauritius | |
146: YT Mayotte | |
147: MX Mexico | |
148: FM Micronesia | |
149: MD Moldova | |
150: MC Monaco | |
151: MN Mongolia | |
152: ME Montenegro | |
153: MS Montserrat | |
154: MA Morocco | |
155: MZ Mozambique | |
156: MM Myanmar [Burma] | |
157: NA Namibia | |
158: NR Nauru | |
159: NP Nepal | |
160: NL Netherlands | |
161: NC New Caledonia | |
162: NZ New Zealand | |
163: NI Nicaragua | |
164: NE Niger | |
165: NG Nigeria | |
166: NU Niue | |
167: NF Norfolk Island | |
168: KP North Korea | |
169: MK North Macedonia | |
170: MP Northern Mariana Islands | |
171: NO Norway | |
172: OM Oman | |
173: PK Pakistan | |
174: PW Palau | |
175: PS Palestinian Territories | |
176: PA Panama | |
177: PG Papua New Guinea | |
178: PY Paraguay | |
179: PE Peru | |
180: PH Philippines | |
181: PN Pitcairn Islands | |
182: PL Poland | |
183: PT Portugal | |
184: PR Puerto Rico | |
185: QA Qatar | |
186: RO Romania | |
187: RU Russia | |
188: RW Rwanda | |
189: RE Réunion | |
190: WS Samoa | |
191: SM San Marino | |
192: SA Saudi Arabia | |
193: SN Senegal | |
194: RS Serbia | |
195: SC Seychelles | |
196: SL Sierra Leone | |
197: SG Singapore | |
198: SX Sint Maarten | |
199: SK Slovakia | |
200: SI Slovenia | |
201: GS So. Georgia & So. Sandwich Isl. | |
202: SB Solomon Islands | |
203: SO Somalia | |
204: ZA South Africa | |
205: KR South Korea | |
206: SS South Sudan | |
207: ES Spain | |
208: LK Sri Lanka | |
209: BL St. Barthélemy | |
210: SH St. Helena | |
211: KN St. Kitts & Nevis | |
212: LC St. Lucia | |
213: MF St. Martin | |
214: PM St. Pierre & Miquelon | |
215: VC St. Vincent & Grenadines | |
216: SD Sudan | |
217: SR Suriname | |
218: SJ Svalbard & Jan Mayen | |
219: SE Sweden | |
220: CH Switzerland | |
221: SY Syria | |
222: ST São Tomé & Príncipe | |
223: TW Taiwan | |
224: TJ Tajikistan | |
225: TZ Tanzania | |
226: TH Thailand | |
227: TL Timor-Leste | |
228: TG Togo | |
229: TK Tokelau | |
230: TO Tonga | |
231: TT Trinidad & Tobago | |
232: TA Tristan da Cunha | |
233: TN Tunisia | |
234: TR Turkey | |
235: TM Turkmenistan | |
236: TC Turks & Caicos Islands | |
237: TV Tuvalu | |
238: UM U.S. Outlying Islands | |
239: VI U.S. Virgin Islands | |
240: UG Uganda | |
241: UA Ukraine | |
242: AE United Arab Emirates | |
243: GB United Kingdom | |
244: US United States | |
245: UY Uruguay | |
246: UZ Uzbekistan | |
247: VU Vanuatu | |
248: VA Vatican City | |
249: VE Venezuela | |
250: VN Vietnam | |
251: WF Wallis & Futuna | |
252: EH Western Sahara | |
253: YE Yemen | |
254: ZM Zambia | |
255: ZW Zimbabwe | |
256: AX Åland Islands |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment