I want to add " 5 " in my arrays in every 3rd position
var myList: [String] = ["U.S", "Spain", "China", "Brazil", "India" , "Canada", "Germany", "UK", "Dubai", "Russia", "Japan", "Italy", "France", "Australia", "Thailand", "Mexico"]
var i = 0
for name in 0...myList.count {
i = i + 1
if i % 3 == 0 {
myList.insert("\(5)", at: i )
}
}
print("\(myList)")
OutPut is:
["U.S", "Spain", "China", "5", "Brazil", "India", "5", "Canada", "Germany", "5", "UK", "Dubai", "5", "Russia", "Japan", "5", "Italy", "France", "Australia", "Thailand", "Mexico"]
but after 16 position in array , I am not able to put all 5 in my arrays because of arrays that is myList have total 16 counts ....so how I can able to put 5 in my Arrays properly
The problem there is that you are mutating the array (changing its elements while iterating it), so what you actually need to do is to insert your elements in reverse order. Try like this:
var myList = ["U.S", "Spain", "China", "Brazil", "India", "Canada", "Germany", "UK", "Dubai", "Russia", "Japan", "Italy", "France", "Australia", "Thailand", "Mexico" ]
for index in myList.indices.dropFirst().reversed() where index % 3 == 0 {
myList.insert("5", at: index)
}
print(myList) // "["U.S", "Spain", "China", "5", "Brazil", "India", "Canada", "5", "Germany", "UK", "Dubai", "5", "Russia", "Japan", "Italy", "5", "France", "Australia", "Thailand", "5", "Mexico"]\n"
Related
I am working with the Spotify API and having trouble with the data model due to the JSON data being more complex than any other data I've seen via tutorials or courses. How would I make my struct for the "items" in this JSON data? I understand a majority of the parameters, for example, "album_group": String and "available_markets": [String] but I don't understand what to do with "artists", "external_urls", and "images". Any help would be appreciated.
Below is the first "item" from the data.
Side Note When I'm creating my struct do I have to include EVERY parameter shown below for my API call to work?
{
"href": "https://api.spotify.com/v1/artists/3qiHUAX7zY4Qnjx8TNUzVx/albums?offset=0&limit=20&include_groups=album,single,compilation,appears_on&locale=en-US,en;q=0.9",
"items": [
{
"album_group": "album",
"album_type": "album",
"artists": [
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/3qiHUAX7zY4Qnjx8TNUzVx"
},
"href": "https://api.spotify.com/v1/artists/3qiHUAX7zY4Qnjx8TNUzVx",
"id": "3qiHUAX7zY4Qnjx8TNUzVx",
"name": "Yeat",
"type": "artist",
"uri": "spotify:artist:3qiHUAX7zY4Qnjx8TNUzVx"
}
],
"available_markets": [
"AD",
"AE",
"AG",
"AL",
"AM",
"AO",
"AR",
"AT",
"AU",
"AZ",
"BA",
"BB",
"BD",
"BE",
"BF",
"BG",
"BH",
"BI",
"BJ",
"BN",
"BO",
"BR",
"BS",
"BT",
"BW",
"BY",
"BZ",
"CA",
"CD",
"CG",
"CH",
"CI",
"CL",
"CM",
"CO",
"CR",
"CV",
"CY",
"CZ",
"DE",
"DJ",
"DK",
"DM",
"DO",
"DZ",
"EC",
"EE",
"EG",
"ES",
"FI",
"FJ",
"FM",
"FR",
"GA",
"GB",
"GD",
"GE",
"GH",
"GM",
"GN",
"GQ",
"GR",
"GT",
"GW",
"GY",
"HK",
"HN",
"HR",
"HT",
"HU",
"ID",
"IE",
"IL",
"IN",
"IQ",
"IS",
"IT",
"JM",
"JO",
"JP",
"KE",
"KG",
"KH",
"KI",
"KM",
"KN",
"KR",
"KW",
"KZ",
"LA",
"LB",
"LC",
"LI",
"LK",
"LR",
"LS",
"LT",
"LU",
"LV",
"LY",
"MA",
"MC",
"MD",
"ME",
"MG",
"MH",
"MK",
"ML",
"MN",
"MO",
"MR",
"MT",
"MU",
"MV",
"MW",
"MX",
"MY",
"MZ",
"NA",
"NE",
"NG",
"NI",
"NL",
"NO",
"NP",
"NR",
"NZ",
"OM",
"PA",
"PE",
"PG",
"PH",
"PK",
"PL",
"PS",
"PT",
"PW",
"PY",
"QA",
"RO",
"RS",
"RU",
"RW",
"SA",
"SB",
"SC",
"SE",
"SG",
"SI",
"SK",
"SL",
"SM",
"SN",
"SR",
"ST",
"SV",
"SZ",
"TD",
"TG",
"TH",
"TJ",
"TL",
"TN",
"TO",
"TR",
"TT",
"TV",
"TW",
"TZ",
"UA",
"UG",
"US",
"UY",
"UZ",
"VC",
"VE",
"VN",
"VU",
"WS",
"XK",
"ZA",
"ZM",
"ZW"
],
"external_urls": {
"spotify": "https://open.spotify.com/album/1x55Z0fYARLdeJVjG2UESs"
},
"href": "https://api.spotify.com/v1/albums/1x55Z0fYARLdeJVjG2UESs",
"id": "1x55Z0fYARLdeJVjG2UESs",
"images": [
{
"height": 640,
"url": "https://i.scdn.co/image/ab67616d0000b273b20fdc3ee4c262693cfdf005",
"width": 640
},
{
"height": 300,
"url": "https://i.scdn.co/image/ab67616d00001e02b20fdc3ee4c262693cfdf005",
"width": 300
},
{
"height": 64,
"url": "https://i.scdn.co/image/ab67616d00004851b20fdc3ee4c262693cfdf005",
"width": 64
}
],
"name": "Up 2 Më",
"release_date": "2021-09-10",
"release_date_precision": "day",
"total_tracks": 22,
"type": "album",
"uri": "spotify:album:1x55Z0fYARLdeJVjG2UESs"
}
]
}
Good rule of thumb is, that you need a separate Decodable object for every JSON object (marked with {}) when working with such API.
You can nest Decodable structs. Just don't forget that here, ExternalUrl has to be Decodable too, otherwise you will get an error
struct SpotifyItem: Decodable {
// ...
let artists: [Artist]
let images: [SpotifyImage]
// ...
}
struct Artist: Decodable {
let external_urls: ExternalUrl
let href: String
let id: String
// ...
}
struct ExternalUrl: Decodable {
let spotify: String
}
struct SpotifyImage: Decodable {
let url: String
let height: Int
let width: Int
}
Side Note Answer: I would advise you to do so. It seems like there are ways to decode JSON elements into swift dictionary, but it goes against the type safety that you are trying to achieve. You are parsing JSON into Decodable structs precisely because you want to know ahead of time what are you dealing with.
Don't forget you can still do optional attributes, for example you could have
struct ExternalUrl: Decodable {
let spotify: String
let instagram: String?
}
In that case, if instagram is not present, it will be set to nil. My advice would be follow this more modern and safe way of doing things. You can of course parse the entire JSON into a huge [String: Any] object, but you will have to write out everything you want to access anyway. Plus there will be a LOT of type casting and checks, which Codable does for you.
I have a pickerview tied to a textbox that i'm trying to fill with a range of numbers, 48-96. I don't want to type them all out so i'm using the below:
var height = Array(48...96)
but i get the below error:
Cannot convert return expression of type 'Int' to return type 'String?'
I've tried the below but it doesn't work so i'm helping you kind folks could help:
var height = String(Array(48...96))
I expect to be able to use an array and convert it to a string versus having to type our 48, 49, 50...96 but cant figure out how to convert the array of ints to strings.
You want an array of String containing "48" through "96". You can do that by using map on a range:
var height = (48...96).map(String.init)
print(height)
["48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96"]
Can someone help me out here? I am using Playground for this example, so you can put the whole code into your Playground and see the results.
I found out that when I remove this line:
"address_format": "{{recipient}}\n{{street}}\n{{postalcode}} {{city}}\n{{country}}",
it seems to work.
(The JSON is validated)
import UIKit
struct Country : Decodable {
enum CodingKeys: String, CodingKey {
case continent
case alpha2
case name
}
var name : String?
var continent : String?
var alpha2 : String?
}
let json = """
[
{
"continent": "Europe",
"alpha2": "AD",
"alpha3": "AND",
"country_code": "376",
"currency": "EUR",
"international_prefix": "00",
"ioc": "AND",
"latitude": "42 30 N",
"longitude": "1 30 E",
"name": "Andorra",
"names": [
"Andorre",
"Andorra",
"アンドラ"],
"translations": {
"en": "Andorre",
"it": "Andorra",
"de": "Andorra",
"fr": "Andorre",
"es": null,
"ja": "アンドラ",
"nl": "Andorra",
"ru": "Андорра"},
"national_destination_code_lengths": [2],
"national_number_lengths": [6,7,8,9],
"national_prefix": "None",
"number": "020",
"region": "Europe",
"subregion": "Southern Europe",
"un_locode": "AD",
"languages": ["ca"],
"nationality": "Andorran"},
{
"continent": "Asia",
"address_format": "{{recipient}}\n{{street}}\n{{postalcode}} {{city}}\n{{country}}",
"alpha2": "AE",
"alpha3": "ARE",
"country_code": "971",
"currency": "AED",
"international_prefix": "00",
"ioc": "UAE",
"latitude": "24 00 N",
"longitude": "54 00 E",
"name": "United Arab Emirates",
"names": [
"United Arab Emirates",
"Vereinigte Arabische Emirate",
"Émirats Arabes Unis",
"Emiratos Árabes Unidos",
"アラブ首長国連邦",
"Verenigde Arabische Emiraten"],
"translations": {
"en": "United Arab Emirates",
"it": "Emirati Arabi Uniti",
"de": "Vereinigte Arabische Emirate",
"fr": "Émirats Arabes Unis",
"es": "Emiratos Árabes Unidos",
"ja": "アラブ首長国連邦",
"nl": "Verenigde Arabische Emiraten",
"ru": "Объединенные Арабские Эмираты"},
"national_destination_code_lengths": [2],
"national_number_lengths": [7,8,9],
"national_prefix": "0",
"number": "784",
"region": "Asia",
"subregion": "Western Asia",
"un_locode": "AE",
"languages": ["ar"],
"nationality": "Emirian"}
]
""".data(using: .utf8)!
let decoder = JSONDecoder()
do {
let countries = try decoder.decode([Country].self, from: json)
print(countries)
} catch {
print("erro")
}
If you would print the actually error information
"Unescaped control character around character 722."
rather than meaningless literal string "erro"
the solution is obvious:
The linefeed characters \n must be escaped:
"address_format": "{{recipient}}\\n{{street}}\\n{{postalcode}} {{city}}\\n{{country}}",
You can fix the issue with
let json = """
[
...
]
""".replacingOccurrences(of: "}\n{", with: "}\\n{").data(using: .utf8)!
I am developing a app for ios and I am trying to get each letter of a member of dictionary which I have defined like this :
var Morse = ["a": "01", "b": "1000", "c": "1010", "d": "100", "e": "0", "f": "0010", "g": "110", "h": "0000", "i": "00", "j": "0111", "k": "101", "l": "0100", "m": "11", "n": "10", "o": "111", "p": "0110", "q": "1101", "r": "010", "s": "000", "t": "1", "u": "001", "v": "0001", "w": "011", "x": "1001", "y": "1011", "z": "1100", "1": "01111", "2": "00111", "3": "00011", "4": "00001", "5": "00000", "6": "10000", "7": "11000", "8": "11100", "9": "11110", "0": "11111", " ": "2"]
So for example, if the user enters a I would like to get "0" and then "1". To do this I use a counter :
var counter = 0
var letter: String = ""
var strings_letter: String = ""
letter = Morse[strings_letter]!
var number = Array(letter)[counter]
But this gives me an issue :
Argument type 'String' does not conform to expect type 'Sequence'
What am I doing wrong?
The characters property of a String instance contains a sequence of the characters contained in the String. You could, for a given key (say "a") re-map the .characters of the corresponding value ("01") to single-character String instances to obtain a String array:
if let charsForKeyA = Morse["a"]?.characters.map({ String($0) }) {
charsForKeyA.forEach { print($0) }
} /* 0
1 */
If I got it right, you want to get an array of characters for the value of the inserted key, based on your example, the output should be like:
"a" => ["0", "1"]
"b" => ["1", "0", "0", "0"]
"c" => ["1", "0", "1", "0"]
and so on...
var Morse = ["a": "01", "b": "1000", "c": "1010", "d": "100", "e": "0", "f": "0010", "g": "110", "h": "0000", "i": "00", "j": "0111", "k": "101", "l": "0100", "m": "11", "n": "10", "o": "111", "p": "0110", "q": "1101", "r": "010", "s": "000", "t": "1", "u": "001", "v": "0001", "w": "011", "x": "1001", "y": "1011", "z": "1100", "1": "01111", "2": "00111", "3": "00011", "4": "00001", "5": "00000", "6": "10000", "7": "11000", "8": "11100", "9": "11110", "0": "11111", " ": "2"]
let insertedKey = "a"
if let value = Morse[insertedKey] {
let array = Array(value.characters)
// here is your array!
print(array) // ["0", "1"]
}
I have dictionary in swift
var data = ["GenInfo":Dictionary<String,String>(),"LangInfo":Array<String>(),"EduInfo":Array<Dictionary<String,String>>(),"JobInfo":Array<Dictionary<String,String>>(),"SkillInfo":Array<Dictionary<String,String>>()]
Now I want to add values to this dictionary, How can I do that.
Suppose if I want to add these
"FirstName": "Varun",
"Email": "varun#gmail.com",
"State": "Rajasthan",
"Address": "Plot No. 00, Bhagwan Nagar 31,",
"Zip": "21354",
"Phone": "123456789",
"LastName": "Sharma"
value to the valueForKey "GenInfo"
Your where making a Dictionary (i.e. NSDictionary) whose not mutable after it's declaration (unlike NSMutableDictionary).
That said, you can either do like this :
var data : NSMutableDictionary = ["GenInfo":Dictionary<String,String>(),"LangInfo":Array<String>(),"EduInfo":Array<Dictionary<String,String>>(),"JobInfo":Array<Dictionary<String,String>>(),"SkillInfo":Array<Dictionary<String,String>>()]
data["GenInfo"] = ["FirstName": "Varun",
"Email": "varun#gmail.com",
"State": "Rajasthan",
"Address": "Plot No. 00, Bhagwan Nagar 31,",
"Zip": "21354",
"Phone": "123456789",
"LastName": "Sharma"
]
data["LangInfo"] = ["English", "French", "Italian"]
data["EduInfo"] = [["Degree": "MCA", "School": "University of Kota", "Year": "2013"], ["Degree": "Another degree", "School": "University of London", "Year": "2015"]]
// And so on...
Or like this :
var data = [String: AnyObject]()
data["GenInfo"] = [String: String]() // Dictionary<String,String>()
data["LangInfo"] = [String]() // Array<String>()
data["EduInfo"] = [[String: String]]() // Array<Dictionary<String,String>>()
data["JobInfo"] = [[String: String]]() // Array<Dictionary<String,String>>()
data["SkillInfo"] = [[String: String]]() // Array<Dictionary<String,String>>()
data["GenInfo"] = ["FirstName": "Varun",
"Email": "varun#gmail.com",
"State": "Rajasthan",
"Address": "Plot No. 00, Bhagwan Nagar 31,",
"Zip": "21354",
"Phone": "123456789",
"LastName": "Sharma"
]
data["LangInfo"] = ["English", "French", "Italian"]
data["EduInfo"] = [["Degree": "MCA", "School": "University of Kota", "Year": "2013"], ["Degree": "Another degree", "School": "University of London", "Year": "2015"]]
// And so on...
Like this
var data: NSMutableDictionary = ["GenInfo":Dictionary<String,String>(),"LangInfo":Array<String>(),"EduInfo":Array<Dictionary<String,String>>(),"JobInfo":Array<Dictionary<String,String>>(),"SkillInfo":Array<Dictionary<String,String>>()]
data["GenInfo"] = ["FirstName": "Varun",
"Email": "varun#gmail.com",
"State": "Rajasthan",
"Address": "Plot No. 00, Bhagwan Nagar 31,",
"Zip": "21354",
"Phone": "123456789",
"LastName": "Sharma"
]