Create an array in swift - ios

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

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
}

Swift 3: how to sort Dictionary's key and value of struct

This is my Struct,by Swift 3. I know the Dictionary is not stored sequence like an Array and that is my problem. I want to get my Dictionary sequence as I set in ViewArray. I can get the ctC Dictionary, but how can i sort the keys or values as i set in ViewArrayplease and appreciate the help.
struct CTArray {
var ctname: String
var ctkey: String
var ctC: [String:String]
}
var ViewArray:[CTArray] = []
ViewArray += [CTArray(ctname: "kerish", ctkey: "KH", ctC: ["mon":"Apple", "kis":"aone", "Bat":"Best", "orlno":"bOne"])]
ViewArray += [CTArray(ctname: "tainers", ctkey: "TNN", ctC: ["letGor":"one", "washi":"testing", "monk":"lasth"])]
ViewArray += [CTArray(ctname: "techiu", ctkey: "TCU", ctC: ["22":"tt", "wke":"303", "lenth":"highest"])]
i want to show them in my TableView Cell sorted like these:
the ViewArray[0].ctC.key sorted like [mon, kis, Bat, orlno]
the ViewArray[1].ctC.key sorted like [letGor, washi, monk]
the ViewArray[2].ctC.value sorted like [tt, 303, highest]
It is not clear to me what you are asking, but I'll offer this in case it helps.
I know the Dictionary is not stored sequence like an Array and that is my problem.
If you want to access a dictionary by a certain order of its keys then you can create an array of just the keys in the order you required and use that to access the dictionary. An example is probably easier to follow:
Starting with one of your dictionaries:
let dict = ["mon":"Apple", "kis":"aone", "Bat":"Best", "orlno":"bOne"]
print(dict)
this output:
["Bat": "Best", "kis": "aone", "orlno": "bOne", "mon": "Apple"]
which is not what you want. Now introduce a key array and use that to access the dictionary:
let keyOrder = ["mon", "kis", "Bat", "orlno"]
for key in keyOrder
{
print("\(key): \(dict[key]!)")
}
this outputs:
mon: Apple
kis: aone
Bat: Best
orlno: bOne
which is the order you wish.
The same idea can be used anywhere you want to use/show/etc. the keys in a particular order, by using the keyOrder array as part of dictionary access you are making it appear as though the dictionary entries are "stored in sequence" as you put it.
HTH
var object1 = viewArray[0].ctC.flatMap({$0.key})
var object2 = viewArray[1].ctC.flatMap({$0.key})
var object3 = viewArray[2].ctC.flatMap({$0.value})
print(object1)
print(object2)
print(object3)
Outputs:
["Bat", "kis", "orlno", "mon"]
["letGor", "monk", "washi"]
["highest", "tt", "303"]

Can't get dictionary item by index - cannot subscript String with an Int

I have a dictionary where key is string and value is array of strings.
var someItems : [String: [String]] = [String: [String]]()
I am trying to get item by it's index...
var temp = someItems[0]
But I am gettings error:
'subscript' is unavailable: cannot subscript String with an Int
I don't understand why this doesn't work?
Dictionary is key based not index based and the key is declared as String
var temp = someItems["key"]
Dictionary items are not sorted and therefore you can't call them with index. You should be able to access its value by writing someItems ["theString"], where theString is the selected key.
try this
if let array = someItems["your key for array"] as NSArray{
print(array)
}
it will return your array for the entered key

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