Swift: How to Create Array Holding Dictionaries - ios

I'm teaching myself swift and am trying to create an array holding dictionaries. The dictionaries are in the following format: String: Any (usually Strings, but sometimes additional arrays of dictionaries).
Is this the correct way to create an array capable of holding dictionaries?
var contentArray: [Dictionary<String, Any>] = []

I went through the question history and saw your edit. indexOfObjectPassingTest is a method on NSArray. You are asking about Array which is a different (though related) type.
Here's how to declare the array:
var contentArray = [[String: Any]]()
Here's how to get the index of an item:
let index1 = contentArray.index(of: aDict)
let index2 = contentArray.index(where: { (some conditions) })

Related

Array to Array copy

I have two arrays. One is:
var array = [[String]]()
The second one is:
var finalArray = NSMutableArray()
array is blank.
I want to copy all the data from the final array to array.
For these two different types of array, direct this code won't work.
array = finalArray
An NSArray can be converted to a Swift array of a given element type with the as? operator, like so:
array = finalArray as? [[String]] ?? []
Note that we have to use the conditional typecast operator as? because it's not known at compile-time whether finalArray actually is an array of string arrays (since NSArray does not use generics in Swift).

I have and array inside the value of dictionary

I have an dictionary and the value containts the array of string as follows
arr = ["key":"["a","b","c","d","e","f","g"]"]
I want the new array to be like
let array = ["a","b","c","d","e","f","g"]
How to parse it
You can access dictionary items in few different ways, the easiest is:
let array = arr["key"]
You may need to conditionally unwrap it
if let array = arr["key"] as? [String] {
// rest of code with array
}

2 arrays into one dictionary while maintaining the indexes

I have two separate arrays that I want to import into a dictionary. Order is extremely important because both arrays must match in index
struct MyVariables {
static var users:NSArray!
static var img:NSArray!
}
var data = SearchVC.getData()
MyVariables.users = data.users; //array 1 (key)
MyVariables.img = data.img; //array 2
// Goal is to insert these arrays into a dictionary while maintaing the matching indexes on both arrays
// Dictonary (MyVariables.img, key: MyVariables.users)
A Dictionary does not have a particular order. However, if both arrays have the same length, it is quite easy to iterate over them together:
var dictionary = [NSString: AnyObject]()
for var index = 0; index < data.users.count; index++ {
let img = data.img as! NSString
dictionary[img] = data.users[index]
}
Or, as #robertvojta suggested, use the zip() method:
var dictionary = [NSString: AnyObject]()
for (user, image) in zip(data.users, data.img) {
let img = image as! NSString
dictionary[img] = user
}
The key in a dictionary in swift must be hashable. i.e., not AnyObject.
Assuming you can replace some of your untyped Swift arrays, or cast them like so:
struct MyVariables {
var users:Array<AnyObject>
var img:Array<String>
}
then you can iterate through 1 array using a preferred Swift method and access the second using indexing:
var dictionary = Dictionary<String, AnyObject>()
for (index, element) in enumerate(MyVariables.img) {
dictionary[element] = MyVariables.users[index]
}
Use for loop for travels the array in that as per index access keys and values respective array and add it in dictionary. Its so simple so you can achive your goal using it.
I hope it will help you!

Create an array in swift

I'm searching really much, but maybe I can't understand the results.
I found only that a array in SWIFT have as index int-values
var myArray = [String]()
myArray.append("bla")
myArray.append("blub")
println(myArray[0]) // -> print the result bla
But I will add a String with an String as index-key
var myArray = [String:String]()
myArray.append("Comment1":"bla")
myArray.append("Comment2":"blub")
println(myArray["Comment1"]) // -> should print the result bla
How should i declare the array and how I can append a value then to this array?
Your second example is dictionary
myArray["key"] = "value"
If you want array of dictionaries you would have to declare it like this
var myArray: [[String: String]]
Your first example is an array. Your second example is a dictionary.
For a dictionary you use key value pairing...
myArray["Comment1"] = "Blah"
You use the same to fetch values...
let value = myArray["Comment1"]
println(value)
You got the concept of array in the first example but for the second one you need a dictionary as they operate on key value pair
// the first String denotes the key while the other denotes the value
var myDictionary :[String:String] = ["username":"NSDumb"]
let value = myDictionary["username"]!;
println(value)
Quick reference for dictionaries collection type can be found here

How to put an array of array into another array in Swift 1.2

var dic: [String: [[Item]]] //dic with string key and value of array in array of my Class Object
How can i take the values from this dic and store it in an array as this:
var array: [[Item]]//Array of array
How can I store the values from dic Into this array I tried using the for(key, value) statement. But it wouldn't let me append the values to the array variable. If you need more information I'm happy to give it, but if you understand what I'm trying to do and you know how to do it I appreciate your answer and it's very much needed!!!
If you want to get all the values in dictionary use dictionary.values, it will return you an array of all the values.
var array = dic.values
If you want to go through each value in the dictionary use the following:
for value in dic.values {
// println("Value: \(value)")
array.append(value)
}
For your first line i give you suggestion that you should create your dictionary by
var array = ["123","456","789"]
var array1 = NSMutableArray()
array1 .addObject(array)
var dict = ["String":array1]
var array3 :NSArray = dict["String"]!
println("value ::\(array3[0])")
Output that you get is::
"value ::(\n 123,\n 456,\n 789\n)"
May this help you

Resources