swift 3个国家/地区列出了电话代码?

时间:2017-06-29 08:57:39

标签: ios swift

我是swift的新手,我正在尝试获取电话代码示例+1或+92的国家/地区列表,我正在尝试打印我们的:

        let networkInfo = CTTelephonyNetworkInfo()
        let carrier = networkInfo.subscriberCellularProvider
        let phoneCode = carrier?.mobileNetworkCode

 print(phoneCode)

        let countryCode = NSLocale.isoCountryCodes
        for country in countryCode {
            print(country)


        }

phoneCode输出 03 这是错误的应该 +961 country的结果是ios名称2,示例ZA,YT等如何获取完整的国家/地区名称?我应该使用自定义JSON!

6 个答案:

答案 0 :(得分:6)

  

尝试使用[country:code]字典获取国家/地区名称和国家/地区电话代码。

class CountryPhoneCodeAndName: NSObject {
   var countryWithCode = [CountryNameWithCode]()

    var countryDictionary = ["AF":"93", "AL":"355", "DZ":"213","AS":"1", "AD":"376", "AO":"244", "AI":"1","AG":"1","AR":"54","AM":"374","AW":"297","AU":"61","AT":"43","AZ":"994","BS":"1","BH":"973","BD":"880","BB":"1","BY":"375","BE":"32","BZ":"501","BJ":"229","BM":"1","BT":"975","BA":"387","BW":"267","BR":"55","IO":"246","BG":"359","BF":"226","BI":"257","KH":"855","CM":"237","CA":"1","CV":"238","KY":"345","CF":"236","TD":"235","CL":"56","CN":"86","CX":"61","CO":"57","KM":"269","CG":"242","CK":"682","CR":"506","HR":"385","CU":"53","CY":"537","CZ":"420","DK":"45","DJ":"253","DM":"1","DO":"1","EC":"593","EG":"20","SV":"503","GQ":"240","ER":"291","EE":"372","ET":"251","FO":"298","FJ":"679","FI":"358","FR":"33","GF":"594","PF":"689","GA":"241","GM":"220","GE":"995","DE":"49","GH":"233","GI":"350","GR":"30","GL":"299","GD":"1","GP":"590","GU":"1","GT":"502","GN":"224","GW":"245","GY":"595","HT":"509","HN":"504","HU":"36","IS":"354","IN":"91","ID":"62","IQ":"964","IE":"353","IL":"972","IT":"39","JM":"1","JP":"81","JO":"962","KZ":"77","KE":"254","KI":"686","KW":"965","KG":"996","LV":"371","LB":"961","LS":"266","LR":"231","LI":"423","LT":"370","LU":"352","MG":"261","MW":"265","MY":"60","MV":"960","ML":"223","MT":"356","MH":"692","MQ":"596","MR":"222","MU":"230","YT":"262","MX":"52","MC":"377","MN":"976","ME":"382","MS":"1","MA":"212","MM":"95","NA":"264","NR":"674","NP":"977","NL":"31","AN":"599","NC":"687","NZ":"64","NI":"505","NE":"227","NG":"234","NU":"683","NF":"672","MP":"1","NO":"47","OM":"968","PK":"92","PW":"680","PA":"507","PG":"675","PY":"595","PE":"51","PH":"63","PL":"48","PT":"351","PR":"1","QA":"974","RO":"40","RW":"250","WS":"685","SM":"378","SA":"966","SN":"221","RS":"381","SC":"248","SL":"232","SG":"65","SK":"421","SI":"386","SB":"677","ZA":"27","GS":"500","ES":"34","LK":"94","SD":"249","SR":"597","SZ":"268","SE":"46","CH":"41","TJ":"992","TH":"66","TG":"228","TK":"690","TO":"676","TT":"1","TN":"216","TR":"90","TM":"993","TC":"1","TV":"688","UG":"256","UA":"380","AE":"971","GB":"44","US":"1", "UY":"598","UZ":"998", "VU":"678", "WF":"681","YE":"967","ZM":"260","ZW":"263","BO":"591","BN":"673","CC":"61","CD":"243","CI":"225","FK":"500","GG":"44","VA":"379","HK":"852","IR":"98","IM":"44","JE":"44","KP":"850","KR":"82","LA":"856","LY":"218","MO":"853","MK":"389","FM":"691","MD":"373","MZ":"258","PS":"970","PN":"872","RE":"262","RU":"7","BL":"590","SH":"290","KN":"1","LC":"1","MF":"590","PM":"508","VC":"1","ST":"239","SO":"252","SJ":"47","SY":"963","TW":"886","TZ":"255","TL":"670","VE":"58","VN":"84","VG":"284","VI":"340"]

    func getCountryName() {
        // Sorting all keys
        let keys = countryDictionary.keys
        let keysValue = keys.sorted { (first, second) -> Bool in
            let key1: String = first
            let key2: String = second
            let result = key1.compare(key2) == .orderedAscending
            return result

        }
        print(keysValue)

        for key in keysValue{
            let countryKeyValue = CountryNameWithCode()
            print(countryDictionary[key] ?? "not")
            countryKeyValue.countryCode = countryDictionary[key]!
            countryKeyValue.countryName = Locale.current.localizedString(forRegionCode: key)!
            print(Locale.current.localizedString(forRegionCode: key)!)
            countryWithCode.append(countryKeyValue)
        }
    }

   class CountryNameWithCode: NSObject {
    var countryName = ""
    var countryCode = ""

    }  
}

答案 1 :(得分:5)

此处是国家/地区代码列表JSON

[ { "name": "Afghanistan", "dial_code": "+93", "code": "AF" }, { "name": "Åland Islands", "dial_code": "+358", "code": "AX" }, { "name": "Albania", "dial_code": "+355", "code": "AL" }, { "name": "Algeria", "dial_code": "+213", "code": "DZ" }, { "name": "American Samoa", "dial_code": "+1684", "code": "AS" }, { "name": "Andorra", "dial_code": "+376", "code": "AD" }, { "name": "Angola", "dial_code": "+244", "code": "AO" }, { "name": "Anguilla", "dial_code": "+1264", "code": "AI" }, { "name": "Antarctica", "dial_code": "+672", "code": "AQ" }, { "name": "Antigua and Barbuda", "dial_code": "+1268", "code": "AG" }, { "name": "Argentina", "dial_code": "+54", "code": "AR" }, { "name": "Armenia", "dial_code": "+374", "code": "AM" }, { "name": "Aruba", "dial_code": "+297", "code": "AW" }, { "name": "Australia", "dial_code": "+61", "code": "AU" }, { "name": "Austria", "dial_code": "+43", "code": "AT" }, { "name": "Azerbaijan", "dial_code": "+994", "code": "AZ" }, { "name": "Bahamas", "dial_code": "+1242", "code": "BS" }, { "name": "Bahrain", "dial_code": "+973", "code": "BH" }, { "name": "Bangladesh", "dial_code": "+880", "code": "BD" }, { "name": "Barbados", "dial_code": "+1246", "code": "BB" }, { "name": "Belarus", "dial_code": "+375", "code": "BY" }, { "name": "Belgium", "dial_code": "+32", "code": "BE" }, { "name": "Belize", "dial_code": "+501", "code": "BZ" }, { "name": "Benin", "dial_code": "+229", "code": "BJ" }, { "name": "Bermuda", "dial_code": "+1441", "code": "BM" }, { "name": "Bhutan", "dial_code": "+975", "code": "BT" }, { "name": "Bolivia, Plurinational State of bolivia", "dial_code": "+591", "code": "BO" }, { "name": "Bosnia and Herzegovina", "dial_code": "+387", "code": "BA" }, { "name": "Botswana", "dial_code": "+267", "code": "BW" }, { "name": "Bouvet Island", "dial_code": "+47", "code": "BV" }, { "name": "Brazil", "dial_code": "+55", "code": "BR" }, { "name": "British Indian Ocean Territory", "dial_code": "+246", "code": "IO" }, { "name": "Brunei Darussalam", "dial_code": "+673", "code": "BN" }, { "name": "Bulgaria", "dial_code": "+359", "code": "BG" }, { "name": "Burkina Faso", "dial_code": "+226", "code": "BF" }, { "name": "Burundi", "dial_code": "+257", "code": "BI" }, { "name": "Cambodia", "dial_code": "+855", "code": "KH" }, { "name": "Cameroon", "dial_code": "+237", "code": "CM" }, { "name": "Canada", "dial_code": "+1", "code": "CA" }, { "name": "Cape Verde", "dial_code": "+238", "code": "CV" }, { "name": "Cayman Islands", "dial_code": "+ 345", "code": "KY" }, { "name": "Central African Republic", "dial_code": "+236", "code": "CF" }, { "name": "Chad", "dial_code": "+235", "code": "TD" }, { "name": "Chile", "dial_code": "+56", "code": "CL" }, { "name": "China", "dial_code": "+86", "code": "CN" }, { "name": "Christmas Island", "dial_code": "+61", "code": "CX" }, { "name": "Cocos (Keeling) Islands", "dial_code": "+61", "code": "CC" }, { "name": "Colombia", "dial_code": "+57", "code": "CO" }, { "name": "Comoros", "dial_code": "+269", "code": "KM" }, { "name": "Congo", "dial_code": "+242", "code": "CG" }, { "name": "Congo, The Democratic Republic of the Congo", "dial_code": "+243", "code": "CD" }, { "name": "Cook Islands", "dial_code": "+682", "code": "CK" }, { "name": "Costa Rica", "dial_code": "+506", "code": "CR" }, { "name": "Cote d'Ivoire", "dial_code": "+225", "code": "CI" }, { "name": "Croatia", "dial_code": "+385", "code": "HR" }, { "name": "Cuba", "dial_code": "+53", "code": "CU" }, { "name": "Cyprus", "dial_code": "+357", "code": "CY" }, { "name": "Czech Republic", "dial_code": "+420", "code": "CZ" }, { "name": "Denmark", "dial_code": "+45", "code": "DK" }, { "name": "Djibouti", "dial_code": "+253", "code": "DJ" }, { "name": "Dominica", "dial_code": "+1767", "code": "DM" }, { "name": "Dominican Republic", "dial_code": "+1849", "code": "DO" }, { "name": "Ecuador", "dial_code": "+593", "code": "EC" }, { "name": "Egypt", "dial_code": "+20", "code": "EG" }, { "name": "El Salvador", "dial_code": "+503", "code": "SV" }, { "name": "Equatorial Guinea", "dial_code": "+240", "code": "GQ" }, { "name": "Eritrea", "dial_code": "+291", "code": "ER" }, { "name": "Estonia", "dial_code": "+372", "code": "EE" }, { "name": "Ethiopia", "dial_code": "+251", "code": "ET" }, { "name": "Falkland Islands (Malvinas)", "dial_code": "+500", "code": "FK" }, { "name": "Faroe Islands", "dial_code": "+298", "code": "FO" }, { "name": "Fiji", "dial_code": "+679", "code": "FJ" }, { "name": "Finland", "dial_code": "+358", "code": "FI" }, { "name": "France", "dial_code": "+33", "code": "FR" }, { "name": "French Guiana", "dial_code": "+594", "code": "GF" }, { "name": "French Polynesia", "dial_code": "+689", "code": "PF" }, { "name": "French Southern Territories", "dial_code": "+262", "code": "TF" }, { "name": "Gabon", "dial_code": "+241", "code": "GA" }, { "name": "Gambia", "dial_code": "+220", "code": "GM" }, { "name": "Georgia", "dial_code": "+995", "code": "GE" }, { "name": "Germany", "dial_code": "+49", "code": "DE" }, { "name": "Ghana", "dial_code": "+233", "code": "GH" }, { "name": "Gibraltar", "dial_code": "+350", "code": "GI" }, { "name": "Greece", "dial_code": "+30", "code": "GR" }, { "name": "Greenland", "dial_code": "+299", "code": "GL" }, { "name": "Grenada", "dial_code": "+1473", "code": "GD" }, { "name": "Guadeloupe", "dial_code": "+590", "code": "GP" }, { "name": "Guam", "dial_code": "+1671", "code": "GU" }, { "name": "Guatemala", "dial_code": "+502", "code": "GT" }, { "name": "Guernsey", "dial_code": "+44", "code": "GG" }, { "name": "Guinea", "dial_code": "+224", "code": "GN" }, { "name": "Guinea-Bissau", "dial_code": "+245", "code": "GW" }, { "name": "Guyana", "dial_code": "+592", "code": "GY" }, { "name": "Haiti", "dial_code": "+509", "code": "HT" }, { "name": "Heard Island and Mcdonald Islands", "dial_code": "+0", "code": "HM" }, { "name": "Holy See (Vatican City State)", "dial_code": "+379", "code": "VA" }, { "name": "Honduras", "dial_code": "+504", "code": "HN" }, { "name": "Hong Kong", "dial_code": "+852", "code": "HK" }, { "name": "Hungary", "dial_code": "+36", "code": "HU" }, { "name": "Iceland", "dial_code": "+354", "code": "IS" }, { "name": "India", "dial_code": "+91", "code": "IN" }, { "name": "Indonesia", "dial_code": "+62", "code": "ID" }, { "name": "Iran, Islamic Republic of Persian Gulf", "dial_code": "+98", "code": "IR" }, { "name": "Iraq", "dial_code": "+964", "code": "IQ" }, { "name": "Ireland", "dial_code": "+353", "code": "IE" }, { "name": "Isle of Man", "dial_code": "+44", "code": "IM" }, { "name": "Israel", "dial_code": "+972", "code": "IL" }, { "name": "Italy", "dial_code": "+39", "code": "IT" }, { "name": "Jamaica", "dial_code": "+1876", "code": "JM" }, { "name": "Japan", "dial_code": "+81", "code": "JP" }, { "name": "Jersey", "dial_code": "+44", "code": "JE" }, { "name": "Jordan", "dial_code": "+962", "code": "JO" }, { "name": "Kazakhstan", "dial_code": "+7", "code": "KZ" }, { "name": "Kenya", "dial_code": "+254", "code": "KE" }, { "name": "Kiribati", "dial_code": "+686", "code": "KI" }, { "name": "Korea, Democratic People's Republic of Korea", "dial_code": "+850", "code": "KP" }, { "name": "Korea, Republic of South Korea", "dial_code": "+82", "code": "KR" }, { "name": "Kosovo", "dial_code": "+383", "code": "XK" }, { "name": "Kuwait", "dial_code": "+965", "code": "KW" }, { "name": "Kyrgyzstan", "dial_code": "+996", "code": "KG" }, { "name": "Laos", "dial_code": "+856", "code": "LA" }, { "name": "Latvia", "dial_code": "+371", "code": "LV" }, { "name": "Lebanon", "dial_code": "+961", "code": "LB" }, { "name": "Lesotho", "dial_code": "+266", "code": "LS" }, { "name": "Liberia", "dial_code": "+231", "code": "LR" }, { "name": "Libyan Arab Jamahiriya", "dial_code": "+218", "code": "LY" }, { "name": "Liechtenstein", "dial_code": "+423", "code": "LI" }, { "name": "Lithuania", "dial_code": "+370", "code": "LT" }, { "name": "Luxembourg", "dial_code": "+352", "code": "LU" }, { "name": "Macao", "dial_code": "+853", "code": "MO" }, { "name": "Macedonia", "dial_code": "+389", "code": "MK" }, { "name": "Madagascar", "dial_code": "+261", "code": "MG" }, { "name": "Malawi", "dial_code": "+265", "code": "MW" }, { "name": "Malaysia", "dial_code": "+60", "code": "MY" }, { "name": "Maldives", "dial_code": "+960", "code": "MV" }, { "name": "Mali", "dial_code": "+223", "code": "ML" }, { "name": "Malta", "dial_code": "+356", "code": "MT" }, { "name": "Marshall Islands", "dial_code": "+692", "code": "MH" }, { "name": "Martinique", "dial_code": "+596", "code": "MQ" }, { "name": "Mauritania", "dial_code": "+222", "code": "MR" }, { "name": "Mauritius", "dial_code": "+230", "code": "MU" }, { "name": "Mayotte", "dial_code": "+262", "code": "YT" }, { "name": "Mexico", "dial_code": "+52", "code": "MX" }, { "name": "Micronesia, Federated States of Micronesia", "dial_code": "+691", "code": "FM" }, { "name": "Moldova", "dial_code": "+373", "code": "MD" }, { "name": "Monaco", "dial_code": "+377", "code": "MC" }, { "name": "Mongolia", "dial_code": "+976", "code": "MN" }, { "name": "Montenegro", "dial_code": "+382", "code": "ME" }, { "name": "Montserrat", "dial_code": "+1664", "code": "MS" }, { "name": "Morocco", "dial_code": "+212", "code": "MA" }, { "name": "Mozambique", "dial_code": "+258", "code": "MZ" }, { "name": "Myanmar", "dial_code": "+95", "code": "MM" }, { "name": "Namibia", "dial_code": "+264", "code": "NA" }, { "name": "Nauru", "dial_code": "+674", "code": "NR" }, { "name": "Nepal", "dial_code": "+977", "code": "NP" }, { "name": "Netherlands", "dial_code": "+31", "code": "NL" }, { "name": "Netherlands Antilles", "dial_code": "+599", "code": "AN" }, { "name": "New Caledonia", "dial_code": "+687", "code": "NC" }, { "name": "New Zealand", "dial_code": "+64", "code": "NZ" }, { "name": "Nicaragua", "dial_code": "+505", "code": "NI" }, { "name": "Niger", "dial_code": "+227", "code": "NE" }, { "name": "Nigeria", "dial_code": "+234", "code": "NG" }, { "name": "Niue", "dial_code": "+683", "code": "NU" }, { "name": "Norfolk Island", "dial_code": "+672", "code": "NF" }, { "name": "Northern Mariana Islands", "dial_code": "+1670", "code": "MP" }, { "name": "Norway", "dial_code": "+47", "code": "NO" }, { "name": "Oman", "dial_code": "+968", "code": "OM" }, { "name": "Pakistan", "dial_code": "+92", "code": "PK" }, { "name": "Palau", "dial_code": "+680", "code": "PW" }, { "name": "Palestinian Territory, Occupied", "dial_code": "+970", "code": "PS" }, { "name": "Panama", "dial_code": "+507", "code": "PA" }, { "name": "Papua New Guinea", "dial_code": "+675", "code": "PG" }, { "name": "Paraguay", "dial_code": "+595", "code": "PY" }, { "name": "Peru", "dial_code": "+51", "code": "PE" }, { "name": "Philippines", "dial_code": "+63", "code": "PH" }, { "name": "Pitcairn", "dial_code": "+64", "code": "PN" }, { "name": "Poland", "dial_code": "+48", "code": "PL" }, { "name": "Portugal", "dial_code": "+351", "code": "PT" }, { "name": "Puerto Rico", "dial_code": "+1939", "code": "PR" }, { "name": "Qatar", "dial_code": "+974", "code": "QA" }, { "name": "Romania", "dial_code": "+40", "code": "RO" }, { "name": "Russia", "dial_code": "+7", "code": "RU" }, { "name": "Rwanda", "dial_code": "+250", "code": "RW" }, { "name": "Reunion", "dial_code": "+262", "code": "RE" }, { "name": "Saint Barthelemy", "dial_code": "+590", "code": "BL" }, { "name": "Saint Helena, Ascension and Tristan Da Cunha", "dial_code": "+290", "code": "SH" }, { "name": "Saint Kitts and Nevis", "dial_code": "+1869", "code": "KN" }, { "name": "Saint Lucia", "dial_code": "+1758", "code": "LC" }, { "name": "Saint Martin", "dial_code": "+590", "code": "MF" }, { "name": "Saint Pierre and Miquelon", "dial_code": "+508", "code": "PM" }, { "name": "Saint Vincent and the Grenadines", "dial_code": "+1784", "code": "VC" }, { "name": "Samoa", "dial_code": "+685", "code": "WS" }, { "name": "San Marino", "dial_code": "+378", "code": "SM" }, { "name": "Sao Tome and Principe", "dial_code": "+239", "code": "ST" }, { "name": "Saudi Arabia", "dial_code": "+966", "code": "SA" }, { "name": "Senegal", "dial_code": "+221", "code": "SN" }, { "name": "Serbia", "dial_code": "+381", "code": "RS" }, { "name": "Seychelles", "dial_code": "+248", "code": "SC" }, { "name": "Sierra Leone", "dial_code": "+232", "code": "SL" }, { "name": "Singapore", "dial_code": "+65", "code": "SG" }, { "name": "Slovakia", "dial_code": "+421", "code": "SK" }, { "name": "Slovenia", "dial_code": "+386", "code": "SI" }, { "name": "Solomon Islands", "dial_code": "+677", "code": "SB" }, { "name": "Somalia", "dial_code": "+252", "code": "SO" }, { "name": "South Africa", "dial_code": "+27", "code": "ZA" }, { "name": "South Sudan", "dial_code": "+211", "code": "SS" }, { "name": "South Georgia and the South Sandwich Islands", "dial_code": "+500", "code": "GS" }, { "name": "Spain", "dial_code": "+34", "code": "ES" }, { "name": "Sri Lanka", "dial_code": "+94", "code": "LK" }, { "name": "Sudan", "dial_code": "+249", "code": "SD" }, { "name": "Suriname", "dial_code": "+597", "code": "SR" }, { "name": "Svalbard and Jan Mayen", "dial_code": "+47", "code": "SJ" }, { "name": "Swaziland", "dial_code": "+268", "code": "SZ" }, { "name": "Sweden", "dial_code": "+46", "code": "SE" }, { "name": "Switzerland", "dial_code": "+41", "code": "CH" }, { "name": "Syrian Arab Republic", "dial_code": "+963", "code": "SY" }, { "name": "Taiwan", "dial_code": "+886", "code": "TW" }, { "name": "Tajikistan", "dial_code": "+992", "code": "TJ" }, { "name": "Tanzania, United Republic of Tanzania", "dial_code": "+255", "code": "TZ" }, { "name": "Thailand", "dial_code": "+66", "code": "TH" }, { "name": "Timor-Leste", "dial_code": "+670", "code": "TL" }, { "name": "Togo", "dial_code": "+228", "code": "TG" }, { "name": "Tokelau", "dial_code": "+690", "code": "TK" }, { "name": "Tonga", "dial_code": "+676", "code": "TO" }, { "name": "Trinidad and Tobago", "dial_code": "+1868", "code": "TT" }, { "name": "Tunisia", "dial_code": "+216", "code": "TN" }, { "name": "Turkey", "dial_code": "+90", "code": "TR" }, { "name": "Turkmenistan", "dial_code": "+993", "code": "TM" }, { "name": "Turks and Caicos Islands", "dial_code": "+1649", "code": "TC" }, { "name": "Tuvalu", "dial_code": "+688", "code": "TV" }, { "name": "Uganda", "dial_code": "+256", "code": "UG" }, { "name": "Ukraine", "dial_code": "+380", "code": "UA" }, { "name": "United Arab Emirates", "dial_code": "+971", "code": "AE" }, { "name": "United Kingdom", "dial_code": "+44", "code": "GB" }, { "name": "United States", "dial_code": "+1", "code": "US" }, { "name": "Uruguay", "dial_code": "+598", "code": "UY" }, { "name": "Uzbekistan", "dial_code": "+998", "code": "UZ" }, { "name": "Vanuatu", "dial_code": "+678", "code": "VU" }, { "name": "Venezuela, Bolivarian Republic of Venezuela", "dial_code": "+58", "code": "VE" }, { "name": "Vietnam", "dial_code": "+84", "code": "VN" }, { "name": "Virgin Islands, British", "dial_code": "+1284", "code": "VG" }, { "name": "Virgin Islands, U.S.", "dial_code": "+1340", "code": "VI" }, { "name": "Wallis and Futuna", "dial_code": "+681", "code": "WF" }, { "name": "Yemen", "dial_code": "+967", "code": "YE" }, { "name": "Zambia", "dial_code": "+260", "code": "ZM" }, { "name": "Zimbabwe", "dial_code": "+263", "code": "ZW" } ]

答案 2 :(得分:3)

您可以使用以下网址获取完整的国家/地区名称:

let networkInformation = CTTelephonyNetworkInfo()

if let carrier = networkInformation.subscriberCellularProvider {
    print("phone code:" + carrier.mobileNetworkCode!)

    print("ISO country code: " + carrier.isoCountryCode!)

    // Convert ISO country code to full country name
    let currentLocale = NSLocale.init(localeIdentifier:  NSLocale.current.identifier)
    let fullCountryName = currentLocale.displayName(forKey: NSLocale.Key.countryCode, value: carrier.isoCountryCode!)
    print(fullCountryName)
}

对于国家/地区前缀号码,我认为最好的方法是使用带有[country:code]值的自定义词典。

答案 3 :(得分:0)

感谢@Sai kumar Reddy, 这是一段python(<= 3.6)代码,它会生成一个快速的结构,并使用国家/地区名称,拨号代码和代码写入文件。

file = open("CountryCodes.swift", "x")
file.write("import Foundation\n\n")
file.write("struct CountryCode {\n")
file.write("\tvar name: String\n")
file.write("\tvar dialCode: String\n")
file.write("\tvar code: String\n")
file.write("}\n\n")
file.write("var countryCodes: [CountryCode] = [\n")


codes = [ { "name": "Afghanistan", "dial_code": "+93", "code": "AF" }, { "name": "Åland Islands", "dial_code": "+358", "code": "AX" }, { "name": "Albania", "dial_code": "+355", "code": "AL" }, { "name": "Algeria", "dial_code": "+213", "code": "DZ" }, { "name": "American Samoa", "dial_code": "+1684", "code": "AS" }, { "name": "Andorra", "dial_code": "+376", "code": "AD" }, { "name": "Angola", "dial_code": "+244", "code": "AO" }, { "name": "Anguilla", "dial_code": "+1264", "code": "AI" }, { "name": "Antarctica", "dial_code": "+672", "code": "AQ" }, { "name": "Antigua and Barbuda", "dial_code": "+1268", "code": "AG" }, { "name": "Argentina", "dial_code": "+54", "code": "AR" }, { "name": "Armenia", "dial_code": "+374", "code": "AM" }, { "name": "Aruba", "dial_code": "+297", "code": "AW" }, { "name": "Australia", "dial_code": "+61", "code": "AU" }, { "name": "Austria", "dial_code": "+43", "code": "AT" }, { "name": "Azerbaijan", "dial_code": "+994", "code": "AZ" }, { "name": "Bahamas", "dial_code": "+1242", "code": "BS" }, { "name": "Bahrain", "dial_code": "+973", "code": "BH" }, { "name": "Bangladesh", "dial_code": "+880", "code": "BD" }, { "name": "Barbados", "dial_code": "+1246", "code": "BB" }, { "name": "Belarus", "dial_code": "+375", "code": "BY" }, { "name": "Belgium", "dial_code": "+32", "code": "BE" }, { "name": "Belize", "dial_code": "+501", "code": "BZ" }, { "name": "Benin", "dial_code": "+229", "code": "BJ" }, { "name": "Bermuda", "dial_code": "+1441", "code": "BM" }, { "name": "Bhutan", "dial_code": "+975", "code": "BT" }, { "name": "Bolivia, Plurinational State of bolivia", "dial_code": "+591", "code": "BO" }, { "name": "Bosnia and Herzegovina", "dial_code": "+387", "code": "BA" }, { "name": "Botswana", "dial_code": "+267", "code": "BW" }, { "name": "Bouvet Island", "dial_code": "+47", "code": "BV" }, { "name": "Brazil", "dial_code": "+55", "code": "BR" }, { "name": "British Indian Ocean Territory", "dial_code": "+246", "code": "IO" }, { "name": "Brunei Darussalam", "dial_code": "+673", "code": "BN" }, { "name": "Bulgaria", "dial_code": "+359", "code": "BG" }, { "name": "Burkina Faso", "dial_code": "+226", "code": "BF" }, { "name": "Burundi", "dial_code": "+257", "code": "BI" }, { "name": "Cambodia", "dial_code": "+855", "code": "KH" }, { "name": "Cameroon", "dial_code": "+237", "code": "CM" }, { "name": "Canada", "dial_code": "+1", "code": "CA" }, { "name": "Cape Verde", "dial_code": "+238", "code": "CV" }, { "name": "Cayman Islands", "dial_code": "+ 345", "code": "KY" }, { "name": "Central African Republic", "dial_code": "+236", "code": "CF" }, { "name": "Chad", "dial_code": "+235", "code": "TD" }, { "name": "Chile", "dial_code": "+56", "code": "CL" }, { "name": "China", "dial_code": "+86", "code": "CN" }, { "name": "Christmas Island", "dial_code": "+61", "code": "CX" }, { "name": "Cocos (Keeling) Islands", "dial_code": "+61", "code": "CC" }, { "name": "Colombia", "dial_code": "+57", "code": "CO" }, { "name": "Comoros", "dial_code": "+269", "code": "KM" }, { "name": "Congo", "dial_code": "+242", "code": "CG" }, { "name": "Congo, The Democratic Republic of the Congo", "dial_code": "+243", "code": "CD" }, { "name": "Cook Islands", "dial_code": "+682", "code": "CK" }, { "name": "Costa Rica", "dial_code": "+506", "code": "CR" }, { "name": "Cote d'Ivoire", "dial_code": "+225", "code": "CI" }, { "name": "Croatia", "dial_code": "+385", "code": "HR" }, { "name": "Cuba", "dial_code": "+53", "code": "CU" }, { "name": "Cyprus", "dial_code": "+357", "code": "CY" }, { "name": "Czech Republic", "dial_code": "+420", "code": "CZ" }, { "name": "Denmark", "dial_code": "+45", "code": "DK" }, { "name": "Djibouti", "dial_code": "+253", "code": "DJ" }, { "name": "Dominica", "dial_code": "+1767", "code": "DM" }, { "name": "Dominican Republic", "dial_code": "+1849", "code": "DO" }, { "name": "Ecuador", "dial_code": "+593", "code": "EC" }, { "name": "Egypt", "dial_code": "+20", "code": "EG" }, { "name": "El Salvador", "dial_code": "+503", "code": "SV" }, { "name": "Equatorial Guinea", "dial_code": "+240", "code": "GQ" }, { "name": "Eritrea", "dial_code": "+291", "code": "ER" }, { "name": "Estonia", "dial_code": "+372", "code": "EE" }, { "name": "Ethiopia", "dial_code": "+251", "code": "ET" }, { "name": "Falkland Islands (Malvinas)", "dial_code": "+500", "code": "FK" }, { "name": "Faroe Islands", "dial_code": "+298", "code": "FO" }, { "name": "Fiji", "dial_code": "+679", "code": "FJ" }, { "name": "Finland", "dial_code": "+358", "code": "FI" }, { "name": "France", "dial_code": "+33", "code": "FR" }, { "name": "French Guiana", "dial_code": "+594", "code": "GF" }, { "name": "French Polynesia", "dial_code": "+689", "code": "PF" }, { "name": "French Southern Territories", "dial_code": "+262", "code": "TF" }, { "name": "Gabon", "dial_code": "+241", "code": "GA" }, { "name": "Gambia", "dial_code": "+220", "code": "GM" }, { "name": "Georgia", "dial_code": "+995", "code": "GE" }, { "name": "Germany", "dial_code": "+49", "code": "DE" }, { "name": "Ghana", "dial_code": "+233", "code": "GH" }, { "name": "Gibraltar", "dial_code": "+350", "code": "GI" }, { "name": "Greece", "dial_code": "+30", "code": "GR" }, { "name": "Greenland", "dial_code": "+299", "code": "GL" }, { "name": "Grenada", "dial_code": "+1473", "code": "GD" }, { "name": "Guadeloupe", "dial_code": "+590", "code": "GP" }, { "name": "Guam", "dial_code": "+1671", "code": "GU" }, { "name": "Guatemala", "dial_code": "+502", "code": "GT" }, { "name": "Guernsey", "dial_code": "+44", "code": "GG" }, { "name": "Guinea", "dial_code": "+224", "code": "GN" }, { "name": "Guinea-Bissau", "dial_code": "+245", "code": "GW" }, { "name": "Guyana", "dial_code": "+592", "code": "GY" }, { "name": "Haiti", "dial_code": "+509", "code": "HT" }, { "name": "Heard Island and Mcdonald Islands", "dial_code": "+0", "code": "HM" }, { "name": "Holy See (Vatican City State)", "dial_code": "+379", "code": "VA" }, { "name": "Honduras", "dial_code": "+504", "code": "HN" }, { "name": "Hong Kong", "dial_code": "+852", "code": "HK" }, { "name": "Hungary", "dial_code": "+36", "code": "HU" }, { "name": "Iceland", "dial_code": "+354", "code": "IS" }, { "name": "India", "dial_code": "+91", "code": "IN" }, { "name": "Indonesia", "dial_code": "+62", "code": "ID" }, { "name": "Iran, Islamic Republic of Persian Gulf", "dial_code": "+98", "code": "IR" }, { "name": "Iraq", "dial_code": "+964", "code": "IQ" }, { "name": "Ireland", "dial_code": "+353", "code": "IE" }, { "name": "Isle of Man", "dial_code": "+44", "code": "IM" }, { "name": "Israel", "dial_code": "+972", "code": "IL" }, { "name": "Italy", "dial_code": "+39", "code": "IT" }, { "name": "Jamaica", "dial_code": "+1876", "code": "JM" }, { "name": "Japan", "dial_code": "+81", "code": "JP" }, { "name": "Jersey", "dial_code": "+44", "code": "JE" }, { "name": "Jordan", "dial_code": "+962", "code": "JO" }, { "name": "Kazakhstan", "dial_code": "+7", "code": "KZ" }, { "name": "Kenya", "dial_code": "+254", "code": "KE" }, { "name": "Kiribati", "dial_code": "+686", "code": "KI" }, { "name": "Korea, Democratic People's Republic of Korea", "dial_code": "+850", "code": "KP" }, { "name": "Korea, Republic of South Korea", "dial_code": "+82", "code": "KR" }, { "name": "Kosovo", "dial_code": "+383", "code": "XK" }, { "name": "Kuwait", "dial_code": "+965", "code": "KW" }, { "name": "Kyrgyzstan", "dial_code": "+996", "code": "KG" }, { "name": "Laos", "dial_code": "+856", "code": "LA" }, { "name": "Latvia", "dial_code": "+371", "code": "LV" }, { "name": "Lebanon", "dial_code": "+961", "code": "LB" }, { "name": "Lesotho", "dial_code": "+266", "code": "LS" }, { "name": "Liberia", "dial_code": "+231", "code": "LR" }, { "name": "Libyan Arab Jamahiriya", "dial_code": "+218", "code": "LY" }, { "name": "Liechtenstein", "dial_code": "+423", "code": "LI" }, { "name": "Lithuania", "dial_code": "+370", "code": "LT" }, { "name": "Luxembourg", "dial_code": "+352", "code": "LU" }, { "name": "Macao", "dial_code": "+853", "code": "MO" }, { "name": "Macedonia", "dial_code": "+389", "code": "MK" }, { "name": "Madagascar", "dial_code": "+261", "code": "MG" }, { "name": "Malawi", "dial_code": "+265", "code": "MW" }, { "name": "Malaysia", "dial_code": "+60", "code": "MY" }, { "name": "Maldives", "dial_code": "+960", "code": "MV" }, { "name": "Mali", "dial_code": "+223", "code": "ML" }, { "name": "Malta", "dial_code": "+356", "code": "MT" }, { "name": "Marshall Islands", "dial_code": "+692", "code": "MH" }, { "name": "Martinique", "dial_code": "+596", "code": "MQ" }, { "name": "Mauritania", "dial_code": "+222", "code": "MR" }, { "name": "Mauritius", "dial_code": "+230", "code": "MU" }, { "name": "Mayotte", "dial_code": "+262", "code": "YT" }, { "name": "Mexico", "dial_code": "+52", "code": "MX" }, { "name": "Micronesia, Federated States of Micronesia", "dial_code": "+691", "code": "FM" }, { "name": "Moldova", "dial_code": "+373", "code": "MD" }, { "name": "Monaco", "dial_code": "+377", "code": "MC" }, { "name": "Mongolia", "dial_code": "+976", "code": "MN" }, { "name": "Montenegro", "dial_code": "+382", "code": "ME" }, { "name": "Montserrat", "dial_code": "+1664", "code": "MS" }, { "name": "Morocco", "dial_code": "+212", "code": "MA" }, { "name": "Mozambique", "dial_code": "+258", "code": "MZ" }, { "name": "Myanmar", "dial_code": "+95", "code": "MM" }, { "name": "Namibia", "dial_code": "+264", "code": "NA" }, { "name": "Nauru", "dial_code": "+674", "code": "NR" }, { "name": "Nepal", "dial_code": "+977", "code": "NP" }, { "name": "Netherlands", "dial_code": "+31", "code": "NL" }, { "name": "Netherlands Antilles", "dial_code": "+599", "code": "AN" }, { "name": "New Caledonia", "dial_code": "+687", "code": "NC" }, { "name": "New Zealand", "dial_code": "+64", "code": "NZ" }, { "name": "Nicaragua", "dial_code": "+505", "code": "NI" }, { "name": "Niger", "dial_code": "+227", "code": "NE" }, { "name": "Nigeria", "dial_code": "+234", "code": "NG" }, { "name": "Niue", "dial_code": "+683", "code": "NU" }, { "name": "Norfolk Island", "dial_code": "+672", "code": "NF" }, { "name": "Northern Mariana Islands", "dial_code": "+1670", "code": "MP" }, { "name": "Norway", "dial_code": "+47", "code": "NO" }, { "name": "Oman", "dial_code": "+968", "code": "OM" }, { "name": "Pakistan", "dial_code": "+92", "code": "PK" }, { "name": "Palau", "dial_code": "+680", "code": "PW" }, { "name": "Palestinian Territory, Occupied", "dial_code": "+970", "code": "PS" }, { "name": "Panama", "dial_code": "+507", "code": "PA" }, { "name": "Papua New Guinea", "dial_code": "+675", "code": "PG" }, { "name": "Paraguay", "dial_code": "+595", "code": "PY" }, { "name": "Peru", "dial_code": "+51", "code": "PE" }, { "name": "Philippines", "dial_code": "+63", "code": "PH" }, { "name": "Pitcairn", "dial_code": "+64", "code": "PN" }, { "name": "Poland", "dial_code": "+48", "code": "PL" }, { "name": "Portugal", "dial_code": "+351", "code": "PT" }, { "name": "Puerto Rico", "dial_code": "+1939", "code": "PR" }, { "name": "Qatar", "dial_code": "+974", "code": "QA" }, { "name": "Romania", "dial_code": "+40", "code": "RO" }, { "name": "Russia", "dial_code": "+7", "code": "RU" }, { "name": "Rwanda", "dial_code": "+250", "code": "RW" }, { "name": "Reunion", "dial_code": "+262", "code": "RE" }, { "name": "Saint Barthelemy", "dial_code": "+590", "code": "BL" }, { "name": "Saint Helena, Ascension and Tristan Da Cunha", "dial_code": "+290", "code": "SH" }, { "name": "Saint Kitts and Nevis", "dial_code": "+1869", "code": "KN" }, { "name": "Saint Lucia", "dial_code": "+1758", "code": "LC" }, { "name": "Saint Martin", "dial_code": "+590", "code": "MF" }, { "name": "Saint Pierre and Miquelon", "dial_code": "+508", "code": "PM" }, { "name": "Saint Vincent and the Grenadines", "dial_code": "+1784", "code": "VC" }, { "name": "Samoa", "dial_code": "+685", "code": "WS" }, { "name": "San Marino", "dial_code": "+378", "code": "SM" }, { "name": "Sao Tome and Principe", "dial_code": "+239", "code": "ST" }, { "name": "Saudi Arabia", "dial_code": "+966", "code": "SA" }, { "name": "Senegal", "dial_code": "+221", "code": "SN" }, { "name": "Serbia", "dial_code": "+381", "code": "RS" }, { "name": "Seychelles", "dial_code": "+248", "code": "SC" }, { "name": "Sierra Leone", "dial_code": "+232", "code": "SL" }, { "name": "Singapore", "dial_code": "+65", "code": "SG" }, { "name": "Slovakia", "dial_code": "+421", "code": "SK" }, { "name": "Slovenia", "dial_code": "+386", "code": "SI" }, { "name": "Solomon Islands", "dial_code": "+677", "code": "SB" }, { "name": "Somalia", "dial_code": "+252", "code": "SO" }, { "name": "South Africa", "dial_code": "+27", "code": "ZA" }, { "name": "South Sudan", "dial_code": "+211", "code": "SS" }, { "name": "South Georgia and the South Sandwich Islands", "dial_code": "+500", "code": "GS" }, { "name": "Spain", "dial_code": "+34", "code": "ES" }, { "name": "Sri Lanka", "dial_code": "+94", "code": "LK" }, { "name": "Sudan", "dial_code": "+249", "code": "SD" }, { "name": "Suriname", "dial_code": "+597", "code": "SR" }, { "name": "Svalbard and Jan Mayen", "dial_code": "+47", "code": "SJ" }, { "name": "Swaziland", "dial_code": "+268", "code": "SZ" }, { "name": "Sweden", "dial_code": "+46", "code": "SE" }, { "name": "Switzerland", "dial_code": "+41", "code": "CH" }, { "name": "Syrian Arab Republic", "dial_code": "+963", "code": "SY" }, { "name": "Taiwan", "dial_code": "+886", "code": "TW" }, { "name": "Tajikistan", "dial_code": "+992", "code": "TJ" }, { "name": "Tanzania, United Republic of Tanzania", "dial_code": "+255", "code": "TZ" }, { "name": "Thailand", "dial_code": "+66", "code": "TH" }, { "name": "Timor-Leste", "dial_code": "+670", "code": "TL" }, { "name": "Togo", "dial_code": "+228", "code": "TG" }, { "name": "Tokelau", "dial_code": "+690", "code": "TK" }, { "name": "Tonga", "dial_code": "+676", "code": "TO" }, { "name": "Trinidad and Tobago", "dial_code": "+1868", "code": "TT" }, { "name": "Tunisia", "dial_code": "+216", "code": "TN" }, { "name": "Turkey", "dial_code": "+90", "code": "TR" }, { "name": "Turkmenistan", "dial_code": "+993", "code": "TM" }, { "name": "Turks and Caicos Islands", "dial_code": "+1649", "code": "TC" }, { "name": "Tuvalu", "dial_code": "+688", "code": "TV" }, { "name": "Uganda", "dial_code": "+256", "code": "UG" }, { "name": "Ukraine", "dial_code": "+380", "code": "UA" }, { "name": "United Arab Emirates", "dial_code": "+971", "code": "AE" }, { "name": "United Kingdom", "dial_code": "+44", "code": "GB" }, { "name": "United States", "dial_code": "+1", "code": "US" }, { "name": "Uruguay", "dial_code": "+598", "code": "UY" }, { "name": "Uzbekistan", "dial_code": "+998", "code": "UZ" }, { "name": "Vanuatu", "dial_code": "+678", "code": "VU" }, { "name": "Venezuela, Bolivarian Republic of Venezuela", "dial_code": "+58", "code": "VE" }, { "name": "Vietnam", "dial_code": "+84", "code": "VN" }, { "name": "Virgin Islands, British", "dial_code": "+1284", "code": "VG" }, { "name": "Virgin Islands, U.S.", "dial_code": "+1340", "code": "VI" }, { "name": "Wallis and Futuna", "dial_code": "+681", "code": "WF" }, { "name": "Yemen", "dial_code": "+967", "code": "YE" }, { "name": "Zambia", "dial_code": "+260", "code": "ZM" }, { "name": "Zimbabwe", "dial_code": "+263", "code": "ZW" } ]

for code in codes:
        file.write("\tCountryCode(name: \"{}\", dialCode: \"{}\", code: \"{}\"),\n".format(code.get("name"), code.get("dial_code"), code.get("code")))

file.write("]\n")
file.close()

只需在终端中运行以下命令:

$ python3.6 file.py

答案 4 :(得分:0)

我以这种简单的方式做到了:

struct CountryCodeCategory {
    let id: Int
    let name: String
    let countries: [CountryCode]
}
struct CountryCode {
    let id: Int
    let code: String
    let name: String
    let categoryId: Int
}

class CountryCodeList{
    
    let categories = [
        CountryCodeCategory(id: 0, name: "A", countries: [
            
            CountryCode(id: 0, code: "+7 840", name: "Abkhazia", categoryId: 0),
            CountryCode(id: 1, code: "+93", name: "Afghanistan", categoryId: 0),
            CountryCode(id: 2, code: "+355", name: "Albania", categoryId: 0),
            CountryCode(id: 3, code: "+213", name: "Algeria", categoryId: 0),
            CountryCode(id: 4, code: "+1 684", name: "American Samoa", categoryId: 0),
            CountryCode(id: 5, code: "+376", name: "Andorra", categoryId: 0),
            CountryCode(id: 6, code: "+244", name: "Angola", categoryId: 0),
            CountryCode(id: 7, code: "+1 264", name: "Anguilla", categoryId: 0),
            CountryCode(id: 8, code: "+1 268", name: "Antigua and Barbuda", categoryId: 0),
            CountryCode(id: 9, code: "+54", name: "Argentina", categoryId: 0),
            CountryCode(id: 10, code: "+374", name: "Armenia", categoryId: 0),
            CountryCode(id: 11, code: "+297", name: "Aruba", categoryId: 0),
            CountryCode(id: 12, code: "+247", name: "Ascension", categoryId: 0),
            CountryCode(id: 13, code: "+61", name: "Australia", categoryId: 0),
            CountryCode(id: 14, code: "+672", name: "Australian External Territories", categoryId: 0),
            CountryCode(id: 15, code: "+43", name: "Austria", categoryId: 0),
            CountryCode(id: 16, code: "+994", name: "Azerbaijan", categoryId: 0),
        ]),
        
        
        CountryCodeCategory(id: 1, name: "B", countries: [
            CountryCode(id: 17, code: "+1 242", name: "Bahamas", categoryId: 1),
            CountryCode(id: 18, code: "+973", name: "Bahrain", categoryId: 1),
            CountryCode(id: 19, code: "+880", name: "Bangladesh", categoryId: 1),
            CountryCode(id: 20, code: "+1 246", name: "Barbados", categoryId: 1),
            CountryCode(id: 21, code: "+1 268", name: "Barbuda", categoryId: 1),
            CountryCode(id: 22, code: "+375", name: "Belarus", categoryId: 1),
            CountryCode(id: 23, code: "+32", name: "Belgium", categoryId: 1),
            CountryCode(id: 24, code: "+501", name: "Belize", categoryId: 1),
            CountryCode(id: 25, code: "+229", name: "Benin", categoryId: 1),
            CountryCode(id: 26, code: "+1 441", name: "Bermuda", categoryId: 1),
            CountryCode(id: 27, code: "+975", name: "Bhutan", categoryId: 1),
            CountryCode(id: 28, code: "+591", name: "Bolivia", categoryId: 1),
            CountryCode(id: 29, code: "+387", name: "Bosnia and Herzegovina", categoryId: 1),
            CountryCode(id: 30, code: "+267", name: "Botswana", categoryId: 1),
            CountryCode(id: 31, code: "+55", name: "Brazil", categoryId: 1),
            CountryCode(id: 32, code: "+246", name: "British Indian Ocean Territory", categoryId: 1),
            CountryCode(id: 33, code: "+1 284", name: "British Virgin Islands", categoryId: 1),
            CountryCode(id: 34, code: "+673", name: "Brunei", categoryId: 1),
            CountryCode(id: 35, code: "+359", name: "Bulgaria", categoryId: 1),
            CountryCode(id: 36, code: "+226", name: "Burkina Faso", categoryId: 1),
            CountryCode(id: 37, code: "+257", name: "Burundi", categoryId: 1),
        ]),
        
        
        CountryCodeCategory(id: 2, name: "C", countries: [
            
            CountryCode(id: 38, code: "+855", name: "Cambodia", categoryId: 2),
            CountryCode(id: 39, code: "+237", name: "Cameroon", categoryId: 2),
            CountryCode(id: 40, code: "+1", name: "Canada", categoryId: 2),
            CountryCode(id: 41, code: "+238", name: "Cape Verde", categoryId: 2),
            CountryCode(id: 42, code: "+ 345", name: "Cayman Islands", categoryId: 2),
            CountryCode(id: 43, code: "+236", name: "Central African Republic", categoryId: 2),
            CountryCode(id: 44, code: "+235", name: "Chad", categoryId: 2),
            CountryCode(id: 45, code: "+56", name: "Chile", categoryId: 2),
            CountryCode(id: 46, code: "+86", name: "China", categoryId: 2),
            CountryCode(id: 47, code: "+61", name: "Christmas Island", categoryId: 2),
            CountryCode(id: 48, code: "+61", name: "Cocos-Keeling Islands", categoryId: 2),
            CountryCode(id: 49, code: "+57", name: "Colombia", categoryId: 2),
            CountryCode(id: 50, code: "+269", name: "Comoros", categoryId: 2),
            CountryCode(id: 51, code: "+242", name: "Congo", categoryId: 2),
            CountryCode(id: 52, code: "+243", name: "Congo, Dem. Rep. of (Zaire)", categoryId: 2),
            CountryCode(id: 53, code: "+682", name: "Cook Islands", categoryId: 2),
            CountryCode(id: 54, code: "+506", name: "Costa Rica", categoryId: 2),
            CountryCode(id: 55, code: "+385", name: "Croatia", categoryId: 2),
            CountryCode(id: 56, code: "+53", name: "Cuba", categoryId: 2),
            CountryCode(id: 57, code: "+599", name: "Curacao", categoryId: 2),
            CountryCode(id: 58, code: "+537", name: "Cyprus", categoryId: 2),
            CountryCode(id: 59, code: "+420", name: "Czech Republic", categoryId: 2),
        ]),
        
        
        CountryCodeCategory(id: 3, name: "D", countries: [
            
            CountryCode(id: 60, code: "+45", name: "Denmark", categoryId: 3),
            CountryCode(id: 61, code: "+246", name: "Diego Garcia", categoryId: 3),
            CountryCode(id: 62, code: "+253", name: "Djibouti", categoryId: 3),
            CountryCode(id: 63, code: "+1 767", name: "Dominica", categoryId: 3),
            CountryCode(id: 64, code: "+1 809", name: "Dominican Republic", categoryId: 3),
        ]),
        
        
        CountryCodeCategory(id: 4, name: "E", countries: [
            
            
            CountryCode(id: 65, code: "+670", name: "East Timor", categoryId: 4),
            CountryCode(id: 66, code: "+56", name: "Easter Island", categoryId: 4),
            CountryCode(id: 67, code: "+593", name: "Ecuador", categoryId: 4),
            CountryCode(id: 68, code: "+20", name: "Egypt", categoryId: 4),
            CountryCode(id: 69, code: "+503", name: "El Salvador", categoryId: 4),
            CountryCode(id: 70, code: "+240", name: "Equatorial Guinea", categoryId: 4),
            CountryCode(id: 71, code: "+291", name: "Eritrea", categoryId: 4),
            CountryCode(id: 72, code: "+372", name: "Estonia", categoryId: 4),
            CountryCode(id: 73, code: "+251", name: "Ethiopia", categoryId: 4),
        ]),
        
        
        CountryCodeCategory(id: 5, name: "F", countries: [
            
            CountryCode(id: 74, code: "+500", name: "Falkland Islands", categoryId: 5),
            CountryCode(id: 75, code: "+298", name: "Faroe Islands", categoryId: 5),
            CountryCode(id: 76, code: "+679", name: "Fiji", categoryId: 5),
            CountryCode(id: 77, code: "+358", name: "Finland", categoryId: 5),
            CountryCode(id: 78, code: "+33", name: "France", categoryId: 5),
            CountryCode(id: 79, code: "+596", name: "French Antilles", categoryId: 5),
            CountryCode(id: 80, code: "+594", name: "French Guiana", categoryId: 5),
            CountryCode(id: 81, code: "+689", name: "French Polynesia", categoryId: 5),
        ]),
        
        CountryCodeCategory(id: 6, name: "G", countries: [
            
            CountryCode(id: 82, code: "+241", name: "Gabon", categoryId: 6),
            CountryCode(id: 83, code: "+220", name: "Gambia", categoryId: 6),
            CountryCode(id: 84, code: "+995", name: "Georgia", categoryId: 6),
            CountryCode(id: 85, code: "+49", name: "Germany", categoryId: 6),
            CountryCode(id: 86, code: "+233", name: "Ghana", categoryId: 6),
            CountryCode(id: 87, code: "+350", name: "Gibraltar", categoryId: 6),
            CountryCode(id: 88, code: "+30", name: "Greece", categoryId: 6),
            CountryCode(id: 89, code: "+299", name: "Greenland", categoryId: 6),
            CountryCode(id: 90, code: "+1 473", name: "Grenada", categoryId: 6),
            CountryCode(id: 91, code: "+590", name: "Guadeloupe", categoryId: 6),
            CountryCode(id: 92, code: "+1 671", name: "Guam", categoryId: 6),
            CountryCode(id: 93, code: "+502", name: "Guatemala", categoryId: 6),
            CountryCode(id: 94, code: "+224", name: "Guinea", categoryId: 6),
            CountryCode(id: 95, code: "+245", name: "Guinea-Bissau", categoryId: 6),
            CountryCode(id: 96, code: "+595", name: "Guyana", categoryId: 6),
        ]),
        
        CountryCodeCategory(id: 7, name: "H", countries: [
            
            CountryCode(id: 97, code: "+509", name: "Haiti", categoryId: 7),
            CountryCode(id: 98, code: "+504", name: "Honduras", categoryId: 7),
            CountryCode(id: 99, code: "+852", name: "Hong Kong SAR China", categoryId: 7),
            CountryCode(id: 100, code: "+36", name: "Hungary", categoryId: 7),
        ]),
        
        
        CountryCodeCategory(id: 8, name: "I", countries: [
            
            
            CountryCode(id: 101, code: "+354", name: "Iceland", categoryId: 8),
            CountryCode(id: 102, code: "+91", name: "India", categoryId: 8),
            CountryCode(id: 103, code: "+62", name: "Indonesia", categoryId: 8),
            CountryCode(id: 104, code: "+98", name: "Iran", categoryId: 8),
            CountryCode(id: 105, code: "+964", name: "Iraq", categoryId: 8),
            CountryCode(id: 106, code: "+353", name: "Ireland", categoryId: 8),
            CountryCode(id: 107, code: "+972", name: "Israel", categoryId: 8),
            CountryCode(id: 108, code: "+39", name: "Italy", categoryId: 8),
            CountryCode(id: 109, code: "+225", name: "Ivory Coast", categoryId: 8),
        ]),
        
        
        CountryCodeCategory(id: 9, name: "J", countries: [
            
            CountryCode(id: 110, code: "+1 876", name: "Jamaica", categoryId: 9),
            CountryCode(id: 111, code: "+81", name: "Japan", categoryId: 9),
            CountryCode(id: 112, code: "+962", name: "Jordan", categoryId: 9),
        ]),
        
        
        CountryCodeCategory(id: 10, name: "K", countries: [
            
            
            CountryCode(id: 113, code: "+7 7", name: "Kazakhstan", categoryId: 10),
            CountryCode(id: 114, code: "+254", name: "Kenya", categoryId: 10),
            CountryCode(id: 115, code: "+686", name: "Kiribati", categoryId: 10),
            CountryCode(id: 116, code: "+965", name: "Kuwait", categoryId: 10),
            CountryCode(id: 117, code: "+996", name: "Kyrgyzstan", categoryId: 10),
        ]),
        
        
        CountryCodeCategory(id: 11, name: "L", countries: [
            
            
            CountryCode(id: 118, code: "+856", name: "Laos", categoryId: 11),
            CountryCode(id: 119, code: "+371", name: "Latvia", categoryId: 11),
            CountryCode(id: 120, code: "+961", name: "Lebanon", categoryId: 11),
            CountryCode(id: 121, code: "+266", name: "Lesotho", categoryId: 11),
            CountryCode(id: 122, code: "+231", name: "Liberia", categoryId: 11),
            CountryCode(id: 123, code: "+218", name: "Libya", categoryId: 11),
            CountryCode(id: 124, code: "+423", name: "Liechtenstein", categoryId: 11),
            CountryCode(id: 125, code: "+370", name: "Lithuania", categoryId: 11),
            CountryCode(id: 126, code: "+352", name: "Luxembourg", categoryId: 11),
        ]),
        
        
        CountryCodeCategory(id: 12, name: "M", countries: [
            
            CountryCode(id: 127, code: "+853", name: "Macau SAR China", categoryId: 12),
            CountryCode(id: 128, code: "+389", name: "Macedonia", categoryId: 12),
            CountryCode(id: 129, code: "+261", name: "Madagascar", categoryId: 12),
            CountryCode(id: 130, code: "+265", name: "Malawi", categoryId: 12),
            CountryCode(id: 131, code: "+60", name: "Malaysia", categoryId: 12),
            CountryCode(id: 132, code: "+960", name: "Maldives", categoryId: 12),
            CountryCode(id: 133, code: "+223", name: "Mali", categoryId: 12),
            CountryCode(id: 134, code: "+356", name: "Malta", categoryId: 12),
            CountryCode(id: 135, code: "+692", name: "Marshall Islands", categoryId: 12),
            CountryCode(id: 136, code: "+596", name: "Martinique", categoryId: 12),
            CountryCode(id: 137, code: "+222", name: "Mauritania", categoryId: 12),
            CountryCode(id: 138, code: "+230", name: "Mauritius", categoryId: 12),
            CountryCode(id: 139, code: "+262", name: "Mayotte", categoryId: 12),
            CountryCode(id: 140, code: "+52", name: "Mexico", categoryId: 12),
            CountryCode(id: 141, code: "+691", name: "Micronesia", categoryId: 12),
            CountryCode(id: 142, code: "+1 808", name: "Midway Island", categoryId: 12),
            CountryCode(id: 143, code: "+373", name: "Moldova", categoryId: 12),
            CountryCode(id: 144, code: "+377", name: "Monaco", categoryId: 12),
            CountryCode(id: 145, code: "+976", name: "Mongolia", categoryId: 12),
            CountryCode(id: 146, code: "+382", name: "Montenegro", categoryId: 12),
            CountryCode(id: 147, code: "+1664", name: "Montserrat", categoryId: 12),
            CountryCode(id: 148, code: "+212", name: "Morocco", categoryId: 12),
            CountryCode(id: 149, code: "+95", name: "Myanmar", categoryId: 12),
        ]),
        
        
        CountryCodeCategory(id: 13, name: "N", countries: [
            
            
            CountryCode(id: 150, code: "+264", name: "Namibia", categoryId: 13),
            CountryCode(id: 151, code: "+674", name: "Nauru", categoryId: 13),
            CountryCode(id: 152, code: "+977", name: "Nepal", categoryId: 13),
            CountryCode(id: 153, code: "+31", name: "Netherlands", categoryId: 13),
            CountryCode(id: 154, code: "+599", name: "Netherlands Antilles", categoryId: 13),
            CountryCode(id: 155, code: "+1 869", name: "Nevis", categoryId: 13),
            CountryCode(id: 156, code: "+687", name: "New Caledonia", categoryId: 13),
            CountryCode(id: 157, code: "+64", name: "New Zealand", categoryId: 13),
            CountryCode(id: 158, code: "+505", name: "Nicaragua", categoryId: 13),
            CountryCode(id: 159, code: "+227", name: "Niger", categoryId: 13),
            CountryCode(id: 160, code: "+234", name: "Nigeria", categoryId: 13),
            CountryCode(id: 161, code: "+683", name: "Niue", categoryId: 13),
            CountryCode(id: 162, code: "+672", name: "Norfolk Island", categoryId: 13),
            CountryCode(id: 163, code: "+850", name: "North Korea", categoryId: 13),
            CountryCode(id: 164, code: "+1 670", name: "Northern Mariana Islands", categoryId: 13),
            CountryCode(id: 165, code: "+47", name: "Norway", categoryId: 13),
        ]),
        
        
        CountryCodeCategory(id: 14, name: "O", countries: [
            
            CountryCode(id: 166, code: "+968", name: "Oman", categoryId: 14),
        ]),
        
        
        CountryCodeCategory(id: 15, name: "P", countries: [
            
            
            CountryCode(id: 167, code: "+92", name: "Pakistan", categoryId: 15),
            CountryCode(id: 168, code: "+680", name: "Palau", categoryId: 15),
            CountryCode(id: 169, code: "+970", name: "Palestinian Territory", categoryId: 15),
            CountryCode(id: 170, code: "+507", name: "Panama", categoryId: 15),
            CountryCode(id: 171, code: "+675", name: "Papua New Guinea", categoryId: 15),
            CountryCode(id: 172, code: "+595", name: "Paraguay", categoryId: 15),
            CountryCode(id: 173, code: "+51", name: "Peru", categoryId: 15),
            CountryCode(id: 174, code: "+63", name: "Philippines", categoryId: 15),
            CountryCode(id: 175, code: "+48", name: "Poland", categoryId: 15),
            CountryCode(id: 176, code: "+351", name: "Portugal", categoryId: 15),
            CountryCode(id: 177, code: "+1 787", name: "Puerto Rico", categoryId: 15),
        ]),
        
        
        CountryCodeCategory(id: 16, name: "Q", countries: [
            
            CountryCode(id: 178, code: "+974", name: "Qatar", categoryId: 16),
        ]),
        
        
        CountryCodeCategory(id: 17, name: "R", countries: [
            
            CountryCode(id: 179, code: "+262", name: "Reunion", categoryId: 17),
            CountryCode(id: 180, code: "+40", name: "Romania", categoryId: 17),
            CountryCode(id: 181, code: "+7", name: "Russia", categoryId: 17),
            CountryCode(id: 182, code: "+250", name: "Rwanda", categoryId: 17),
        ]),
        
        
        CountryCodeCategory(id: 18, name: "S", countries: [
            
            CountryCode(id: 183, code: "+685", name: "Samoa", categoryId: 18),
            CountryCode(id: 184, code: "+378", name: "San Marino", categoryId: 18),
            CountryCode(id: 185, code: "+966", name: "Saudi Arabia", categoryId: 18),
            CountryCode(id: 186, code: "+221", name: "Senegal", categoryId: 18),
            CountryCode(id: 187, code: "+381", name: "Serbia", categoryId: 18),
            CountryCode(id: 188, code: "+248", name: "Seychelles", categoryId: 18),
            CountryCode(id: 189, code: "+232", name: "Sierra Leone", categoryId: 18),
            CountryCode(id: 190, code: "+65", name: "Singapore", categoryId: 18),
            CountryCode(id: 191, code: "+421", name: "Slovakia", categoryId: 18),
            CountryCode(id: 192, code: "+386", name: "Slovenia", categoryId: 18),
            CountryCode(id: 193, code: "+677", name: "Solomon Islands", categoryId: 18),
            CountryCode(id: 194, code: "+27", name: "South Africa", categoryId: 18),
            CountryCode(id: 195, code: "+500", name: "South Georgia and the South Sandwich Islands", categoryId: 18),
            CountryCode(id: 196, code: "+82", name: "South Korea", categoryId: 18),
            CountryCode(id: 197, code: "+34", name: "Spain", categoryId: 18),
            CountryCode(id: 198, code: "+94", name: "Sri Lanka", categoryId: 18),
            CountryCode(id: 199, code: "+249", name: "Sudan", categoryId: 18),
            CountryCode(id: 200, code: "+597", name: "Suriname", categoryId: 18),
            CountryCode(id: 201, code: "+268", name: "Swaziland", categoryId: 18),
            CountryCode(id: 202, code: "+46", name: "Sweden", categoryId: 18),
            CountryCode(id: 203, code: "+41", name: "Switzerland", categoryId: 18),
            CountryCode(id: 204, code: "+963", name: "Syria", categoryId: 18),
        ]),
        
        
        CountryCodeCategory(id: 19, name: "T", countries: [
            
            CountryCode(id: 205, code: "+886", name: "Taiwan", categoryId: 19),
            CountryCode(id: 206, code: "+992", name: "Tajikistan", categoryId: 19),
            CountryCode(id: 207, code: "+255", name: "Tanzania", categoryId: 19),
            CountryCode(id: 208, code: "+66", name: "Thailand", categoryId: 19),
            CountryCode(id: 209, code: "+670", name: "Timor Leste", categoryId: 19),
            CountryCode(id: 210, code: "+228", name: "Togo", categoryId: 19),
            CountryCode(id: 211, code: "+690", name: "Tokelau", categoryId: 19),
            CountryCode(id: 212, code: "+676", name: "Tonga", categoryId: 19),
            CountryCode(id: 213, code: "+1 868", name: "Trinidad and Tobago", categoryId: 19),
            CountryCode(id: 214, code: "+216", name: "Tunisia", categoryId: 19),
            CountryCode(id: 215, code: "+90", name: "Turkey", categoryId: 19),
            CountryCode(id: 216, code: "+993", name: "Turkmenistan", categoryId: 19),
            CountryCode(id: 217, code: "+1 649", name: "Turks and Caicos Islands", categoryId: 19),
            CountryCode(id: 218, code: "+688", name: "Tuvalu", categoryId: 19),
        ]),
        
        
        CountryCodeCategory(id: 20, name: "U", countries: [
            
            CountryCode(id: 219, code: "+1 340", name: "U.S. Virgin Islands", categoryId: 20),
            CountryCode(id: 220, code: "+256", name: "Uganda", categoryId: 20),
            CountryCode(id: 221, code: "+380", name: "Ukraine", categoryId: 20),
            CountryCode(id: 222, code: "+971", name: "United Arab Emirates", categoryId: 20),
            CountryCode(id: 223, code: "+44", name: "United Kingdom", categoryId: 20),
            CountryCode(id: 224, code: "+1", name: "United States", categoryId: 20),
            CountryCode(id: 225, code: "+598", name: "Uruguay", categoryId: 20),
            CountryCode(id: 226, code: "+998", name: "Uzbekistan", categoryId: 20),
        ]),
        
        
        CountryCodeCategory(id: 21, name: "V", countries: [
            
            CountryCode(id: 227, code: "+678", name: "Vanuatu", categoryId: 21),
            CountryCode(id: 228, code: "+58", name: "Venezuela", categoryId: 21),
            CountryCode(id: 229, code: "+84", name: "Vietnam", categoryId: 21),
        ]),
        
        
        CountryCodeCategory(id: 22, name: "W", countries: [
            
            CountryCode(id: 230, code: "+1 808", name: "Wake Island", categoryId: 22),
            CountryCode(id: 231, code: "+681", name: "Wallis and Futuna", categoryId: 22),
            
        ]),
        
        
        CountryCodeCategory(id: 23, name: "Y", countries: [
            
            CountryCode(id: 232, code: "+967", name: "Yemen", categoryId: 23)
        ]),
        
        
        CountryCodeCategory(id: 24, name: "Z", countries: [
            
            CountryCode(id: 233, code: "+260", name: "Zambia", categoryId: 24),
            CountryCode(id: 234, code: "+255", name: "Zanzibar", categoryId: 24),
            CountryCode(id: 235, code: "+263", name: "Zimbabwe", categoryId: 24)
        ]),
    ]
    

答案 5 :(得分:0)

我尝试通过 Raj Joshi 创建一个更简单的答案版本。

class CountryCodes {
    
    struct CountryWithCode {
        var countryCode: String
        var countryName: String
        var dialCode: String
    }
    
    private static let countryDictionary = ["AF":"93", "AL":"355", "DZ":"213","AS":"1", "AD":"376", "AO":"244", "AI":"1","AG":"1","AR":"54","AM":"374","AW":"297","AU":"61","AT":"43","AZ":"994","BS":"1","BH":"973","BD":"880","BB":"1","BY":"375","BE":"32","BZ":"501","BJ":"229","BM":"1","BT":"975","BA":"387","BW":"267","BR":"55","IO":"246","BG":"359","BF":"226","BI":"257","KH":"855","CM":"237","CA":"1","CV":"238","KY":"345","CF":"236","TD":"235","CL":"56","CN":"86","CX":"61","CO":"57","KM":"269","CG":"242","CK":"682","CR":"506","HR":"385","CU":"53","CY":"537","CZ":"420","DK":"45","DJ":"253","DM":"1","DO":"1","EC":"593","EG":"20","SV":"503","GQ":"240","ER":"291","EE":"372","ET":"251","FO":"298","FJ":"679","FI":"358","FR":"33","GF":"594","PF":"689","GA":"241","GM":"220","GE":"995","DE":"49","GH":"233","GI":"350","GR":"30","GL":"299","GD":"1","GP":"590","GU":"1","GT":"502","GN":"224","GW":"245","GY":"595","HT":"509","HN":"504","HU":"36","IS":"354","IN":"91","ID":"62","IQ":"964","IE":"353","IL":"972","IT":"39","JM":"1","JP":"81","JO":"962","KZ":"77","KE":"254","KI":"686","KW":"965","KG":"996","LV":"371","LB":"961","LS":"266","LR":"231","LI":"423","LT":"370","LU":"352","MG":"261","MW":"265","MY":"60","MV":"960","ML":"223","MT":"356","MH":"692","MQ":"596","MR":"222","MU":"230","YT":"262","MX":"52","MC":"377","MN":"976","ME":"382","MS":"1","MA":"212","MM":"95","NA":"264","NR":"674","NP":"977","NL":"31","AN":"599","NC":"687","NZ":"64","NI":"505","NE":"227","NG":"234","NU":"683","NF":"672","MP":"1","NO":"47","OM":"968","PK":"92","PW":"680","PA":"507","PG":"675","PY":"595","PE":"51","PH":"63","PL":"48","PT":"351","PR":"1","QA":"974","RO":"40","RW":"250","WS":"685","SM":"378","SA":"966","SN":"221","RS":"381","SC":"248","SL":"232","SG":"65","SK":"421","SI":"386","SB":"677","ZA":"27","GS":"500","ES":"34","LK":"94","SD":"249","SR":"597","SZ":"268","SE":"46","CH":"41","TJ":"992","TH":"66","TG":"228","TK":"690","TO":"676","TT":"1","TN":"216","TR":"90","TM":"993","TC":"1","TV":"688","UG":"256","UA":"380","AE":"971","GB":"44","US":"1", "UY":"598","UZ":"998", "VU":"678", "WF":"681","YE":"967","ZM":"260","ZW":"263","BO":"591","BN":"673","CC":"61","CD":"243","CI":"225","FK":"500","GG":"44","VA":"379","HK":"852","IR":"98","IM":"44","JE":"44","KP":"850","KR":"82","LA":"856","LY":"218","MO":"853","MK":"389","FM":"691","MD":"373","MZ":"258","PS":"970","PN":"872","RE":"262","RU":"7","BL":"590","SH":"290","KN":"1","LC":"1","MF":"590","PM":"508","VC":"1","ST":"239","SO":"252","SJ":"47","SY":"963","TW":"886","TZ":"255","TL":"670","VE":"58","VN":"84","VG":"284","VI":"340"]

    static func values() -> [CountryWithCode] {
        var countriesWithCode = [CountryWithCode]()
        
        let countryCodes = countryDictionary.keys
        for countryCode in countryCodes {
            let countryName = Locale.current.localizedString(forRegionCode: countryCode) ?? "N/A"
            let dialCode = countryDictionary[countryCode] ?? "N/A"
            
            let countryValue = CountryWithCode(countryCode: countryCode,
                                               countryName: countryName,
                                               dialCode: dialCode)
            countriesWithCode.append(countryValue)
        }
        
        let sortedCountries = countriesWithCode.sorted { (firstCountry, secondCountry) -> Bool in
            let sortedByName = firstCountry.countryName.compare(secondCountry.countryName) == .orderedAscending
            return sortedByName
        }
        
        countriesWithCode.removeAll()
        countriesWithCode.append(contentsOf: sortedCountries)
        
        return countriesWithCode
    }
}

上面的代码可以实现为:

let countryCodes = CountryCodes.values()
print(countryCodes.first?.dialCode)