How to get last entered value in a Dictionary in Swift? - swift

How do I get the last entered value in a Dictionary in Swift? For example how would I get the value "CCC" from below:
var dictionary = Dictionary<String, String>()
dictionary.updateValue("AAA", forKey: "111")
dictionary.updateValue("BBB", forKey: "222")
dictionary.updateValue("CCC", forKey: "333")

What you are after is generally known as a Ordered Dictionary, i.e. a dictionary that remembers the order that items have been added. A quick Google search turns up a GitHub project that may suit you.
https://github.com/lukaskubanek/OrderedDictionary
With this as the type of your dictionary, you could just use:
dictionary.last // returns Optional(("CCC", "333"))
The GitHub page has details on how you would go about adding this class to your project.

If your key is sorted you can try:
var dictionary = Dictionary<String, String>()
dictionary.updateValue("AAA", forKey: "111")
dictionary.updateValue("BBB", forKey: "222")
dictionary.updateValue("CCC", forKey: "333")
print(dictionary.keys.sorted().last.map({ ($0, dictionary[$0]!) }))

The whole point of dictionaries is they're NOT ordered. This is why their lookups are generally so fast is because they partition elements without regard for their order.
If you want items in a particular order, you should use an array. Yes, "sorted dictionaries" exist, but they're really just dictionary-like interfaces on top of arrays. They contain none of the benefits that traditional dictionaries bring.
If you think you need an ordered dictionary, you probably need to consider a new structure for your data.

//If you need to build a query string for http:
let httpBody:[String : String] = [
"Street" : "1234 W 1st Street",
"Building" : "Suite 500",
"City" : "Los Angeles",
"State" : "CA",
"Zip" : "92005"
]
var idx = 0
var queryStr = ""
for (k, v) in httpBody {
if idx == httpBody.count - 1 {
queryStr += k + "=" + v
} else {
queryStr += k + "=" + v + "&"
idx += 1
}
}
print(queryStr)
//Example: for some REST API
//let bodyData:Data = queryStr.data(using: .utf8)!
//request.httpBody = bodyData
//Or this option:
var newStr = ""
for (k, v) in httpBody {
let index = httpBody.index(httpBody.startIndex, offsetBy: httpBody.count - 1)
if k == httpBody.keys[index] {
newStr += k + "=" + v
} else {
newStr += k + "=" + v + "&"
}
}
print( "\n", newStr)

Related

Get all values into dictionary and create a String with a specific format

I have a dictionary with this structure:
a: [1,2]
b: [3,4]
c: [5,6]
and I need to return a string with this structure.
a,b,c\n1,3,5\n2,4,6
I solved the first part of the string. But to get the rest of the String. I try to iterate into my dictionary to get the first elements for each key in my dictionary and then get the rest for each value into the array.
Is there an easier way to get this?
Once you know what's the order of the keys (alpha ?), you can use this:
let dict: [String: [Int]] = ["a": [1,2], "b": [3, 4], "c": [5, 6]]
let keys = dict.keys.sorted() //Or do whatever you want here to get your target order
var matrix: [[String]] = []
keys.forEach {
guard let arrayAsInt = dict[$0] else { return }
let arrayAsString = arrayAsInt.map{ "\($0)" }
matrix.append( [$0] + arrayAsString)
}
print("Matrix: \(matrix)")
let transposed = matrix.transposed()
print("Transposed Matrix: \(transposed)")
let output = transposed.map { $0.joined(separator: ",")}.joined(separator: "\n")
print(output)
The outputs:
$>Matrix: [["a", "1", "2"], ["b", "3", "4"], ["c", "5", "6"]]
$>Transposed Matrix: [["a", "b", "c"], ["1", "3", "5"], ["2", "4", "6"]]
$>a,b,c
1,3,5
2,4,6
Obvisouly the "\n" might be invisible and be an actual new line
a,b,c
1,3,5
2,4,6
Being
a,b,c\n1,3,5\n2,4,6
What's the idea behind that? Create a matrix and use the transpose (it's used in maths with matrix, it's one of the basic modification of a matrix).
First transform the [String: [Int]] into a [[String]], where each element would be key followed by its values. I transformed it there as String for simpler code later.
Why doing that? Because the matrix value is easy to get from your initial dict. the transposed value is harder (not impossible) to get from dict but easier from matrix, and the transposed is quickly transformed into your format.
So my thinking was the reverse:
Get a structure from your output, then how to get it, it's a transpose, so I need to get the initial input as it, etc.
With the help of a code for Transpose Matrix (that accept String elements).
extension Collection where Self.Iterator.Element: RandomAccessCollection {
// PRECONDITION: `self` must be rectangular, i.e. every row has equal size.
func transposed() -> [[Self.Iterator.Element.Iterator.Element]] {
guard let firstRow = self.first else { return [] }
return firstRow.indices.map { index in
self.map{ $0[index] }
}
}
}
Any code (there a various) working ones, should the trick. I took it from here.
As pointed by #Leo Dabus, you can remove the Self.Iterator.Element
from the extension code (twice). I just wanted to it as such, not modifying the initial answer since it's not mind.
What you are looking for, besides composing the final string, is how to transpose a collection (this would work with collections of different sizes as well):
extension Sequence {
func max<T: Comparable>(_ predicate: (Element) -> T) -> Element? {
self.max(by: { predicate($0) < predicate($1) })
}
}
extension Collection where Element: RandomAccessCollection, Element.Indices == Range<Int> {
func transposed() -> [[Element.Element]] {
(0..<(max(\.count)?.count ?? .zero)).map {
index in compactMap { $0.indices ~= index ? $0[index] : nil }
}
}
}
let dict = ["a": [1,2,3],
"b": [4,5,6],
"c": [7,8,9]]
let sorted = dict.sorted(by: {$0.key < $1.key})
let result = sorted.map(\.key).joined(separator: ",") + "\n" +
sorted.map(\.value).transposed().map {
$0.map(String.init).joined(separator: ",")
}.joined(separator: "\n")
result // "a,b,c\n1,4,7\n2,5,8\n3,6,9"
A dictionary is an unordered collection so you need to sort it according to any specific key. Here I sort the dictionary according to the key if you don't care about an order you can just remove sort.
let dict: [String: Any] = ["a": [1,2], "b": [3,4], "c": [5,6]]
let sortedKey = dict.keys.sorted(by: <)
let key = sortedKey.joined(separator: ",")
var firstValues: [String] = []
var secondValues: [String] = []
sortedKey.forEach { (key) in
if let dictValue = dict[key] as? [Int],
let firstValue = dictValue.first,
let secondValue = dictValue.last {
firstValues.append("\(firstValue)")
secondValues.append("\(secondValue)")
}
}
let finalString = key + "\n" + firstValues.joined(separator: ",") + "\n" + secondValues.joined(separator: ",")
print(finalString) // "a,b,c\n1,3,5\n2,4,6"

how to check if a key equal a value in swift dictionary?

let say I have this dictionary
var myDic = ["Item" : " Item1", "Item2" : " Item3"]
how can I write an if statement to check if Item and Item1 are a key-value paire?
According to the https://developer.apple.com/documentation/swift/dictionary
Every dictionary is an unordered collection of key-value pairs. You
can iterate over a dictionary using a for-in loop, decomposing each
key-value pair into the elements of a tuple.
This means you can write the following code:
let myDict = ["Item" : " Item1", "Item2" : " Item3"]
for (key, val) in myDict {
if (key == "Item2" && val == " Item3") {
print("match!") //prints match!
}
}
This is obviously kind of a "naive" approach where the values are hardcoded in the if-statement. A one-line alternative:
print(myDict.contains(where: { $0.key == "Item2" && $0.value == " Item3" })) //true
Let's say you need to check for two pairs. Then you can use KeyValuePairs which preserves the order and simply iterate:
let toFind: KeyValuePairs = [ "Item2" : " Item3", "Itemx" : " Item4"]
for tup in toFind {
print(myDict.contains(where: { $0 == tup })) //true, false
}
If you want to follow this approach, I recommend reading about the important differences between Dictionary and KeyValuePairs https://developer.apple.com/documentation/swift/keyvaluepairs

Removing Non Duplicate Keys from Two Dictionary

I have two dictionaries in Swift with few similar values which are in dynamic mode:
dict1 = ["a1":"value 1", "b1":"value2", "c1":"value 3"]
dict2 = ["b1": "value2", "d1": "value4"]
If I want to compare these two dictionaries and want to extract only the matching keys even nested, how do I about to do that?
If you want the common keys with the value in one of them :
let intersectionDict = dict1.filter { dict2.keys.contains($0.key) }
//Or
let intersectionDict2 = dict2.filter { dict1.keys.contains($0.key) }
If you want the values to match too:
let intersectionDict3 = dict1.filter { dict2[$0.key] == $0.value }
And the result is:
print(intersectionDict3) //["b1": "value2"]
As others have shown, you can do this using a filter statement. You can make it even quicker by always filtering the smaller of the two dicts, improving the time complexity from O(dict1.size) to O(min(dict1.size, dict2.size).
extension Dictionary {
func intersectingByKeys(with other: Dictionary) -> Dictionary {
let (smallerDict, largerDict) = (self.count < other.count) ? (self, other) : (other, self)
return smallerDict.filter { key, _ in largerDict.keys.contains(key) }
}
}
let dict1 = ["a1":"value 1", "b1":"value2", "c1":"value 3"]
let dict2 = ["b1": "value2", "d1": "value4"]
print(dict1.intersectingByKeys(with: dict2))
You can create a Set from the keys of one of the dictionaries and call intersection on the Set with the keys of the other dictionary.
let matchingKeys = Set(dict1.keys).intersection(dict2.keys) // {"b1"}

Swift Dictionary: join key and value into string

I'm looking for a best syntax for this:
let responseParameters = ["keyA" : "valueA", "keyB" : "valueB"]
var responseString = ""
for (key, value) in responseParameters {
responseString += "\(key):\(value)"
if Array(responseParameters.keys).last != key {
responseString += "+"
}
}
//responseString: keyA:valueA+keyB:valueB
Something like an array joinWithSeparator, using a flatMap or something like that. (study purpose)
You can map over key/value pairs in dictionaries to convert them to an Array of Strings, then you can join those with +. But remember, dictionaries are unordered, so this will not preserve the input ordering.
let responseParameters = ["keyA" : "valueA", "keyB" : "valueB"]
let responseString = responseParameters.map{ "\($0):\($1)" }
.joined(separator: "+")
A dictionary is not an ordered collection, so you'll have to sort the keys prior to accessing the "ordered version" of the key-value pairs. E.g.
let responseParameters = ["keyA" : "valueA", "keyB" : "valueB", "keyC" : "valueC"]
let responseString = responseParameters
.sort { $0.0 < $1.0 }
.map { $0 + ":" + $1 }
.joinWithSeparator("+")
print(responseString) // keyA:valueA+keyB:valueB+keyC:valueC
Updated answer for swift 5 :
let responseParameters = ["keyA": "valueA", "keyB": "valueB"]
let responseString = responseParameters.map { "\($0):\($1)" }
.joined(separator: "+")
Actually, you can use reduce like: 🙌
let responseParameters = ["keyA": "valueA", "keyB": "valueB"]
let responseString = responseParameters.reduce("") { $0.isEmpty ? "\($1.key):\($1.value)" : "\($0)+\($1.key):\($1.value)" }

Concatenate two dictionaries in Swift

Swift gives us plenty new abilities like (at last!) concatenating strings and even arrays. But no support for dictionaries. Is the only way to concatenate dictionaries is to overload + operation for them?
let string = "Hello" + "World" // "HelloWorld"
let array = ["Hello"] + ["World"] // ["Hello", "World"]
let dict = ["1" : "Hello"] + ["2" : "World"] // error =(
Use it like this:
Put this anywhere, e.g. Dictionary+Extension.swift:
func +<Key, Value> (lhs: [Key: Value], rhs: [Key: Value]) -> [Key: Value] {
var result = lhs
rhs.forEach{ result[$0] = $1 }
return result
}
Now your code just work
let string = "Hello" + "World" // "HelloWorld"
let array = ["Hello"] + ["World"] // ["Hello", "World"]
let dict = ["1" : "Hello"] + ["2" : "World"] // okay =)
That is not possible because there can be matching keys in the second dictionary. But you can do it manually and the values in the dictionary will be replaced in that case.
var dict = ["1" : "Hello"]
let dict2 = ["2" : "World"]
for key in dict2.keys {
dict[key] = dict2[key]
}
You also can use Swift provided functions to do the merge:
public func +<K, V>(left: [K:V], right: [K:V]) -> [K:V] {
return left.merging(right) { $1 }
}
$1 will take common keys from right dictionary, you can use $0 if you want to give priority to left dictionay.
This can be done by using for each loop and inout keyword
func mergDict(firstDict:inout [String:Any], secondDict:[String:Any]){
secondDict.forEach { (key, value) in
firstDict[key] = value
}
}
than call like
mergDict(firstDict: &firstDictToMerge, secondDict: secondDictToMerge)
you will see that your firstDictToMerge will be merged with second one without using any other variable