Stuck with adding edit/delete action buttons to angular datatable - angular7

I'm using angular 7 in my application. I've installed 'angular-datatables' in my application. I'm able to populate data. But i'm stuck with adding action buttons. Not sure how to do this. Also not getting much help from google.
Component.ts
export class ParentListComponent implements OnInit {
dtOptions: DataTables.Settings = {};
dtUsers = [
{ "fatherName": 101, "motherName": "Anil", "guardian": "Singh", "mobile": "121231313", "email": "abc#gmail.com" },
{ "fatherName": 102, "motherName": "Reena", "guardian": "Singh", "mobile": "121231313", "email": "abc#gmail.com" },
{ "fatherName": 103, "motherName": "Aradhay", "guardian": "Simgh", "mobile": "121231313", "email": "abc#gmail.com" },
{ "fatherName": 104, "motherName": "Dilip", "guardian": "Singh", "mobile": "121231313", "email": "abc#gmail.com" },
{ "fatherName": 105, "motherName": "Alok", "guardian": "Singh", "mobile": "121231313", "email": "abc#gmail.com" },
{ "fatherName": 106, "motherName": "Sunil", "guardian": "Singh", "mobile": "121231313", "email": "abc#gmail.com" },
{ "fatherName": 107, "motherName": "Sushil", "guardian": "Singh", "mobile": "121231313", "email": "abc#gmail.com" },
{ "fatherName": 108, "motherName": "Sheo", "guardian": "Shan", "mobile": "121231313", "email": "abc#gmail.com" },
{ "fatherName": 109, "motherName": "Niranjan", "guardian": "R", "mobile": "121231313", "email": "abc#gmail.com" },
{ "fatherName": 110, "motherName": "Lopa", "guardian": "Mudra", "mobile": "121231313", "email": "abc#gmail.com" },
{ "fatherName": 111, "motherName": "Paramanand", "guardian": "Tripathi", "mobile": "121231313", "email": "abc#gmail.com" }
];
constructor() { }
ngOnInit() {
this.dtOptions = {
//dom: 'lrtip', // to hide search bar
data: this.dtUsers,
columns: [
{ title: 'Father Name', data: 'fatherName' },
{ title: 'Mother Name', data: 'motherName' },
{ title: 'Guardian', data: 'guardian' },
{ title: 'Mobile', data: 'mobile' },
{ title: 'Email', data: 'email' }
]
};
}
}
component.html
<div class="panel-body table-responsive">
<table datatable [dtOptions]="dtOptions" class="table table-striped table-bordered dt-responsive nowrap"></table>
</div>

Related

How do I remove optional text from UILabel

I'm trying to remove the Optional text on my UILabel when I run the app. However, I already tried many ways and the Optional persists.
It's important to say that I got these values from JSON, so I created a Struct to decode all this Data. The property ibu is the only one that has Optional written, but if I remove the ? I got an error on JSONDecoder that say's:
Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index
23", intValue: 23), CodingKeys(stringValue: "ibu", intValue: nil)],
debugDescription: "Expected Double value but found null instead.",
underlyingError: nil))
How do I fix that?
App Picture:
Model Folder:
struct Cerveja:Decodable{
let name:String
let image_url:String
let description:String
let tagline:String
let abv:Double
let ibu:Double? //This one that I got the "Optional" written in the UILabel
//If I remove the "?" I got the error below
}
View Folder - Details Screen:
var item:Cerveja?
override func viewDidLoad() {
super.viewDidLoad()
labelName.text = item?.name
labelDescricao.text = item?.description
labelAmargor.text = "\(String(describing: item!.ibu))"
labelTeorAlc.text = "\(item!.abv)"
let resource = ImageResource(downloadURL: URL(string: "\(item?.image_url ?? "")")!, cacheKey: item?.image_url)
imageDetail.kf.setImage(with: resource)
}
Networking Folder:
func getApiData(completion: #escaping ([Cerveja]) -> ()){
guard let urlString = URL(string: "https://api.punkapi.com/v2/beers") else {
print("URL Error")
return
}
Alamofire.request(urlString).responseJSON { response in
if response.data == response.data{
do{
let decoder = try JSONDecoder().decode([Cerveja].self, from: response.data!)
completion(decoder)
}catch{
print(error)
}
}else{print("API Response is Empty")}
}
}
JSON:
[
{
"id": 1,
"name": "Buzz",
"tagline": "A Real Bitter Experience.",
"first_brewed": "09\/2007",
"description": "A light, crisp and bitter IPA brewed with English and American hops. A small batch brewed only once.",
"image_url": "https:\/\/images.punkapi.com\/v2\/keg.png",
"abv": 4.5,
"ibu": 60,
"target_fg": 1010,
"target_og": 1044,
"ebc": 20,
"srm": 10,
"ph": 4.4,
"attenuation_level": 75,
"volume": {
"value": 20,
"unit": "liters"
},
"boil_volume": {
"value": 25,
"unit": "liters"
},
"method": {
"mash_temp": [
{
"temp": {
"value": 64,
"unit": "celsius"
},
"duration": 75
}
],
"fermentation": {
"temp": {
"value": 19,
"unit": "celsius"
}
},
"twist": null
},
"ingredients": {
"malt": [
{
"name": "Maris Otter Extra Pale",
"amount": {
"value": 3.3,
"unit": "kilograms"
}
},
{
"name": "Caramalt",
"amount": {
"value": 0.2,
"unit": "kilograms"
}
},
{
"name": "Munich",
"amount": {
"value": 0.4,
"unit": "kilograms"
}
}
],
"hops": [
{
"name": "Fuggles",
"amount": {
"value": 25,
"unit": "grams"
},
"add": "start",
"attribute": "bitter"
},
{
"name": "First Gold",
"amount": {
"value": 25,
"unit": "grams"
},
"add": "start",
"attribute": "bitter"
},
{
"name": "Fuggles",
"amount": {
"value": 37.5,
"unit": "grams"
},
"add": "middle",
"attribute": "flavour"
},
{
"name": "First Gold",
"amount": {
"value": 37.5,
"unit": "grams"
},
"add": "middle",
"attribute": "flavour"
},
{
"name": "Cascade",
"amount": {
"value": 37.5,
"unit": "grams"
},
"add": "end",
"attribute": "flavour"
}
],
"yeast": "Wyeast 1056 - American Ale\u2122"
},
"food_pairing": [
"Spicy chicken tikka masala",
"Grilled chicken quesadilla",
"Caramel toffee cake"
],
"brewers_tips": "The earthy and floral aromas from the hops can be overpowering. Drop a little Cascade in at the end of the boil to lift the profile with a bit of citrus.",
"contributed_by": "Sam Mason <samjbmason>"
},
{
"id": 2,
"name": "Trashy Blonde",
"tagline": "You Know You Shouldn't",
"first_brewed": "04\/2008",
"description": "A titillating, neurotic, peroxide punk of a Pale Ale. Combining attitude, style, substance, and a little bit of low self esteem for good measure; what would your mother say? The seductive lure of the sassy passion fruit hop proves too much to resist. All that is even before we get onto the fact that there are no additives, preservatives, pasteurization or strings attached. All wrapped up with the customary BrewDog bite and imaginative twist.",
"image_url": "https:\/\/images.punkapi.com\/v2\/2.png",
"abv": 4.1,
"ibu": 41.5,
"target_fg": 1010,
"target_og": 1041.7,
"ebc": 15,
"srm": 15,
"ph": 4.4,
"attenuation_level": 76,
"volume": {
"value": 20,
"unit": "liters"
},
"boil_volume": {
"value": 25,
"unit": "liters"
},
"method": {
"mash_temp": [
{
"temp": {
"value": 69,
"unit": "celsius"
},
"duration": null
}
],
"fermentation": {
"temp": {
"value": 18,
"unit": "celsius"
}
},
"twist": null
},
"ingredients": {
"malt": [
{
"name": "Maris Otter Extra Pale",
"amount": {
"value": 3.25,
"unit": "kilograms"
}
},
{
"name": "Caramalt",
"amount": {
"value": 0.2,
"unit": "kilograms"
}
},
{
"name": "Munich",
"amount": {
"value": 0.4,
"unit": "kilograms"
}
}
],
"hops": [
{
"name": "Amarillo",
"amount": {
"value": 13.8,
"unit": "grams"
},
"add": "start",
"attribute": "bitter"
},
{
"name": "Simcoe",
"amount": {
"value": 13.8,
"unit": "grams"
},
"add": "start",
"attribute": "bitter"
},
{
"name": "Amarillo",
"amount": {
"value": 26.3,
"unit": "grams"
},
"add": "end",
"attribute": "flavour"
},
{
"name": "Motueka",
"amount": {
"value": 18.8,
"unit": "grams"
},
"add": "end",
"attribute": "flavour"
}
],
"yeast": "Wyeast 1056 - American Ale\u2122"
},
"food_pairing": [
"Fresh crab with lemon",
"Garlic butter dipping sauce",
"Goats cheese salad",
"Creamy lemon bar doused in powdered sugar"
],
"brewers_tips": "Be careful not to collect too much wort from the mash. Once the sugars are all washed out there are some very unpleasant grainy tasting compounds that can be extracted into the wort.",
"contributed_by": "Sam Mason <samjbmason>"
},
{
"id": 3,
"name": "Berliner Weisse With Yuzu - B-Sides",
"tagline": "Japanese Citrus Berliner Weisse.",
"first_brewed": "11\/2015",
"description": "Japanese citrus fruit intensifies the sour nature of this German classic.",
"image_url": "https:\/\/images.punkapi.com\/v2\/keg.png",
"abv": 4.2,
"ibu": 8,
"target_fg": 1007,
"target_og": 1040,
"ebc": 8,
"srm": 4,
"ph": 3.2,
"attenuation_level": 83,
"volume": {
"value": 20,
"unit": "liters"
},
"boil_volume": {
"value": 25,
"unit": "liters"
},
"method": {
"mash_temp": [
{
"temp": {
"value": 60,
"unit": "celsius"
},
"duration": 10
},
{
"temp": {
"value": 65,
"unit": "celsius"
},
"duration": 30
},
{
"temp": {
"value": 72,
"unit": "celsius"
},
"duration": 10
},
{
"temp": {
"value": 78,
"unit": "celsius"
},
"duration": 5
}
],
"fermentation": {
"temp": {
"value": 21,
"unit": "celsius"
}
},
"twist": "Soured naturally using the kettle souring technique, Yuzu fruit: 50g at middle, Yuzu juice: 200ml at FV"
},
"ingredients": {
"malt": [
{
"name": "Propino Pale Malt",
"amount": {
"value": 1.63,
"unit": "kilograms"
}
},
{
"name": "Wheat Malt",
"amount": {
"value": 1.63,
"unit": "kilograms"
}
},
{
"name": "Propino Pale Malt for kettle 0.03kg souring",
"amount": {
"value": 0.03,
"unit": "kilograms"
}
},
{
"name": "Acidulated Malt for kettle souring",
"amount": {
"value": 0.03,
"unit": "kilograms"
}
}
],
"hops": [
{
"name": "Bramling Cross",
"amount": {
"value": 10,
"unit": "grams"
},
"add": "middle",
"attribute": "bitter"
}
],
"yeast": "Wyeast 1056 - American Ale\u2122"
}
Use optional binding to avoid this problem
Use
if let ibu = item?.ibu {
labelAmargor.text = "\(String(describing: ibu))"
}
Instead of
labelAmargor.text = "\(String(describing: item!.ibu))"
I would recommend Nil-Coalescing Operator ( ?? ). You can find detailed info here
class Test {
var ibu: Double?
}
var tt: Test? = Test()
tt?.ibu = 3.14
print(String(describing: tt?.ibu)) // Optional("3.14")
print(tt?.ibu ?? 0) // 3.14
tt?.ibu = nil
print(String(describing: tt?.ibu)) // nil
print(tt?.ibu ?? 0) // 0

How to merge a JSON-object in Jbuilder

I am creating a rails app with Backbone/Marionette frontend.
I want to take the JSON-object below and transform it.
This is how it looks now
[{
"total_entries": 4
},
{
"entries": [{
"user": {
"id": 1,
"fullname": "Paul Paulsen"
},
"reports": [{
"id": 1,
"name": "Anna Pearson",
"relation": "wife",
"phone": "2232",
"email": "adsas#sss.se"
}
]
},
{
"user": {
"id": 2,
"fullname": "Anna Palmgren"
},
"reports": [{
"id": 3,
"name": "Mika",
"relation": "Andersen",
"phone": "12312321",
"email": "aas#sss.se"
}]
}
]
}
]
I want to make it look like this
[{
"total_entries": 4
},
{
"entries": [{
"id": 1,
"fullname": "Paul Paulsen"
}, {
"id": 1,
"name": "Anna Pearson",
"relation": "wife",
"phone": "2232",
"email": "adsas#sss.se"
}, {
"id": 1,
"fullname": "Anna Palmgren"
}, {
"id": 3,
"name": "Mika",
"relation": "Andersen",
"phone": "12312321",
"email": "aas#sss.se"
}
]
}
]
This is the Rabl file/code I use
json.array! [0,1] do |index|
if index == 0
json.total_entries #total
else
json.entries #reports.group_by(&:user) do |user, reports|
json.user user, :id, :fullname
json.reports reports do |report|
json.extract! report, :id, :name, :relation, :phone, :email
end
end
end
end
Try this ruby snippet.
x= [{
"total_entries": 4
},
{
"entries": [{
"user": {
"id": 1,
"fullname": "Paul Paulsen"
},
"reports": [{
"id": 1,
"name": "Anna Pearson",
"relation": "wife",
"phone": "2232",
"email": "adsas#sss.se"
}
]
},
{
"user": {
"id": 2,
"fullname": "Anna Palmgren"
},
"reports": [{
"id": 3,
"name": "Mika",
"relation": "Andersen",
"phone": "12312321",
"email": "aas#sss.se"
}]
}
]
}
]
x[1][:entries] = x[1][:entries].map{|p| [p[:user], p[:reports]]}.flatten

swift 3 countries list with phone code?

i am new with swift , i am trying to get list of countries with Phone code example +1 or +92 , i am trying to print our with :
let networkInfo = CTTelephonyNetworkInfo()
let carrier = networkInfo.subscriberCellularProvider
let phoneCode = carrier?.mobileNetworkCode
print(phoneCode)
let countryCode = NSLocale.isoCountryCodes
for country in countryCode {
print(country)
}
phoneCode output is 03 which is wrong it should be +961
and the result of country is ios name 2 , example ZA,YT etc how to get full country name ? should i use custom JSON !
Try This dictionary with [country:code] to get country name and country phone 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 = ""
}
}
Here It is the list of country codes 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" } ]
You can get full country name with:
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)
}
And for country prefix number I think the best way is to use custom dictionary with [country:code] values.
I did it in this simple way:
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)
]),
]
Thanks to #Sai kumar Reddy,
here's a piece of python (<= 3.6) code which generates a swift struct and writes to file with country-name, dial-code and code.
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()
Just run in the terminal this command:
$ python3.6 file.py
I have tried to created a simpler version of the answer by 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
}
}
The above code can be implemented as:
let countryCodes = CountryCodes.values()
print(countryCodes.first?.dialCode)

Beautify JSON parsing with Hashie::Mash Rash?

Trying to parse some ugly JSON:
image = product.images.find { |i| i["sizeName"] == "Large" }
If I use Hashie::Mash Rash, can I make it look like this instead?
image = product.images.find { |i| i["size_name"] == "large" }
If so, why am I getting undefined method 'each_pair' for #<Array:0x007f84a0408540>? Please see https://gist.github.com/frankie-loves-jesus/6b8012f9197ca6c675a9 for a full example including a live app.
Example JSON:
{
"metadata": {
"category": {
"id": "women",
"name": "Women's Fashion"
},
"showSizeFilter": false,
"showColorFilter": true,
"offset": 0,
"limit": 20,
"total": 974184
},
"products": [
{
"id": 377083005,
"name": "BCBGeneration Women's Contrast Sleeve Trench",
"currency": "USD",
"price": 168,
"priceLabel": "$168.00",
"salePrice": 106.43,
"salePriceLabel": "$106.43",
"inStock": true,
"retailer": {
"id": "849",
"name": "Amazon.com",
"url": "http://www.shopstyle.com/browse/Amazon.com-US?pid=uid9616-726296-93"
},
"locale": "en_US",
"description": "This jacket features contrasting leather sleeves",
"brand": {
"id": "51",
"name": "BCBG MAX AZRIA",
"url": "http://www.shopstyle.com/browse/BCBG-MAX-AZRIA?pid=uid9616-726296-93"
},
"clickUrl": "http://api.shopstyle.com/action/apiVisitRetailer?id=377083005&pid=uid9616-726296-93",
"images": [
{
"sizeName": "Small",
"width": 32,
"height": 40,
"url": "http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a_small.jpg"
},
{
"sizeName": "Medium",
"width": 112,
"height": 140,
"url": "http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a_medium.jpg"
},
{
"sizeName": "Large",
"width": 164,
"height": 205,
"url": "http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a.jpg"
}
],
"colors": [
{
"name": "Chino"
}
],
"sizes": [
{
"name": "XX-Small"
},
{
"name": "X-Small"
}
],
"categories": [
{
"id": "raincoats-and-trenchcoats",
"name": "Raincoats & Trenchcoats"
}
]
}
]
}
This is my working code
require 'json'
require 'rash'
#json_text = <<END
{
"metadata": {
"category": {
"id": "women",
"name": "Women's Fashion"
},
"showSizeFilter": false,
"showColorFilter": true,
"offset": 0,
"limit": 20,
"total": 974184
},
"products": [
{
"id": 377083005,
"name": "BCBGeneration Women's Contrast Sleeve Trench",
"currency": "USD",
"price": 168,
"priceLabel": "$168.00",
"salePrice": 106.43,
"salePriceLabel": "$106.43",
"inStock": true,
"retailer": {
"id": "849",
"name": "Amazon.com",
"url": "http://www.shopstyle.com/browse/Amazon.com-US?pid=uid9616-726296-93"
},
"locale": "en_US",
"description": "This jacket features contrasting leather sleeves",
"brand": {
"id": "51",
"name": "BCBG MAX AZRIA",
"url": "http://www.shopstyle.com/browse/BCBG-MAX-AZRIA?pid=uid9616-726296-93"
},
"clickUrl": "http://api.shopstyle.com/action/apiVisitRetailer?id=377083005&pid=uid9616-726296-93",
"images": [
{
"sizeName": "Small",
"width": 32,
"height": 40,
"url": "http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a_small.jpg"
},
{
"sizeName": "Medium",
"width": 112,
"height": 140,
"url": "http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a_medium.jpg"
},
{
"sizeName": "Large",
"width": 164,
"height": 205,
"url": "http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a.jpg"
}
],
"colors": [
{
"name": "Chino"
}
],
"sizes": [
{
"name": "XX-Small"
},
{
"name": "X-Small"
}
],
"categories": [
{
"id": "raincoats-and-trenchcoats",
"name": "Raincoats & Trenchcoats"
}
]
}
]
}
END
hash = JSON.parse(#json_text)
#rash = Hashie::Rash.new( hash )
images = []
#rash.products.each do |product|
images << product.images.find { |i| i.size_name.downcase == "large" }
end
puts images.inspect
#[#<Hashie::Rash height=205 size_name="Large" url="http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a.jpg" width=164>]
It doesn't raise the error you have mentioned.
And I use
$ruby --version
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin13.0.0]
gem list --local |grep 'rash'
rash (0.4.0)
$gem list --local |grep 'hashie'
hashie (3.2.0, 2.0.5)
Could you check yours?
And, if possible, dump the json at the moment that it raises the error.

Fetch photos from Instagram

Currently I'm able to fetch the photos of the logged in user in my app. But I want to fetch the photos from my instagram developer account in which I have registered my app. Is this possible? Currently,
NSString *urlString=[NSString stringWithFormat:#"https://api.instagram.com/v1/users/self/feed/?access_token=%#",appDelegate.instagram.accessToken];
fetches the detail of the logged in user. But I want to get the photos from my client's instagram account. Replacing 'self' in the above url with client's username doesn't work. Any ideas ?
Can't you use the /users/user-id/media/recent Endpoint?
Example from Instagram API Documentation, to get Feed for UserID = 3, use following
https://api.instagram.com/v1/users/3/media/recent/?access_token=ACCESS-TOKEN
Returns
{
"data": [{
"comments": {
"data": [],
"count": 0
},
"caption": {
"created_time": "1296710352",
"text": "Inside le truc #foodtruck",
"from": {
"username": "kevin",
"full_name": "Kevin Systrom",
"type": "user",
"id": "3"
},
"id": "26621408"
},
"likes": {
"count": 15,
"data": [{
"username": "mikeyk",
"full_name": "Mike Krieger",
"id": "4",
"profile_picture": "..."
}, {...subset of likers...}]
},
"link": "http://instagr.am/p/BWrVZ/",
"user": {
"username": "kevin",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_3_75sq_1295574122.jpg",
"id": "3"
},
"created_time": "1296710327",
"images": {
"low_resolution": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/02/6ea7baea55774c5e81e7e3e1f6e791a7_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/02/6ea7baea55774c5e81e7e3e1f6e791a7_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/02/6ea7baea55774c5e81e7e3e1f6e791a7_7.jpg",
"width": 612,
"height": 612
}
},
"type": "image",
"users_in_photo": [],
"filter": "Earlybird",
"tags": ["foodtruck"],
"id": "22721881",
"location": {
"latitude": 37.778720183610183,
"longitude": -122.3962783813477,
"id": "520640",
"street_address": "",
"name": "Le Truc"
}
},
{
"videos": {
"low_resolution": {
"url": "http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_102.mp4",
"width": 480,
"height": 480
},
"standard_resolution": {
"url": "http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_101.mp4",
"width": 640,
"height": 640
},
"comments": {
"data": [{
"created_time": "1279332030",
"text": "Love the sign here",
"from": {
"username": "mikeyk",
"full_name": "Mikey Krieger",
"id": "4",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1242695_75sq_1293915800.jpg"
},
"id": "8"
},
{
"created_time": "1279341004",
"text": "Chilako taco",
"from": {
"username": "kevin",
"full_name": "Kevin S",
"id": "3",
"profile_picture": "..."
},
"id": "3"
}],
"count": 2
},
"caption": null,
"likes": {
"count": 1,
"data": [{
"username": "mikeyk",
"full_name": "Mikeyk",
"id": "4",
"profile_picture": "..."
}]
},
"link": "http://instagr.am/p/D/",
"created_time": "1279340983",
"images": {
"low_resolution": {
"url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_7.jpg",
"width": 612,
"height": 612
}
},
"type": "video",
"users_in_photo": null,
"filter": "Vesper",
"tags": [],
"id": "363839373298",
"user": {
"username": "kevin",
"full_name": "Kevin S",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_3_75sq_1295574122.jpg",
"id": "3"
},
"location": null
},
]
}
Instagram only provides the API to get the feeds of self .. Have a look on
https://api.instagram.com/v1/users/userID/media/recent
this i hope it provides some recent medias of a user for more info look at this
http://instagram.com/developer/api-console/

Resources