Swift: Filter a Dictionary with Array as Value - swift

I'm new to Swift programming. For my particular project, I'm trying to filter a dictionary with some user input, and the dictionary's value consists of an array.
Here is some sample code, and what I'm trying to accomplish:
var dictionary = ["a": ["aberration", "abc"], "b" : ["babel", "bereft"]]
var filteredDictionary = [String: [String]]()
var searchText = "aberration"
//getting the first letter of string
var firstLetter = searchText[searchText.startIndex]
With this particular searchText, I'm trying to get:
filteredDictionary = ["a": ["aberration"]]
Edit: I want the dictionary to return with the first letter as its key, and the values with what searchText matches up with. Sorry if it I wasn't clear.
Here is some code I have tried, but obviously, I can't get it to work:
filteredDictionary = dictionary.filter{$0.key == firstLetter && for element in $0.value { element.hasPrefix(searchText) }}
Any help would be appreciated. Thanks.

Here's a solution that maps the values based on the search and then filters out the empty results.
var dictionary = ["a": ["aberration", "abc"], "b" : ["babel", "bereft"]]
var searchText = "aberration"
let filteredDictionary = dictionary.mapValues { $0.filter { $0.hasPrefix(searchText) } }.filter { !$0.value.isEmpty }
print(filteredDictionary)
Output:
["a": ["aberration"]]

Try this:
var dictionary = ["a": ["aberration", "abc"], "b" : ["babel", "bereft"]]
var searchText = "aberration"
var filteredDictionary = dictionary.filter { (key, value) -> Bool in
return (value as! [String]).contains(searchText)
}.mapValues { (values) -> [String] in
return [searchText]
}
print(filteredDictionary)
You can use a combination of filter and map to achieve the desired result.
Output:
["a": ["aberration"]]

let firstLetter = String(searchText[searchText.startIndex])
let filteredDictionary = dictionary
.reduce(into: [String: [String]]()) { (result, object) in
if object.key == firstLetter {
let array = object.value.filter({ $0.hasPrefix(searchText) })
if array.count > 0 {
result[object.key] = array
}
}
}
Output:
["a": ["aberration"]]

Related

Swift 4 - Remove Items from array based off another array

I have two table views
#IBOutlet var specialitiesAll: UITableView!
#IBOutlet var specialitiesAdded: UITableView!
and they each populate separate arrays
var allArray = Array<Dictionary<String, Any>>()
var addedArray = Array<Dictionary<String, Any>>()
One is populated with all items, the other is items the user added, both I get from an API
let newAddedArray = self.userProfile["specialities"] as! Array<Dictionary<String, Any>>
for item in newAddedArray
{
self.addedArray.append(["id" : item["id"]!, "text" : item["name"]!])
}
self.specialitiesAdded.reloadData()
getSpecialities(){ result in
for item in result
{
self.allArray.append(["id" : item["id"]!, "text" : item["text"]!])
}
self.specialitiesAll.reloadData()
}
My question is allArray has all the items, how would I either remove or skip the items that are in addedArray or newAddedArray?
You can first convert Array into Set and perform difference set processing. Hash table is very efficient and the order may be messy. Direct processing of Arrays is not efficient, but the order is guaranteed to be the same. According to the scene selection?
var employees: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"]
let neighbors: Set = ["Bethany", "Eric", "Forlani", "Greta"]
employees.subtract(neighbors)
print(employees)
try this.
extension Array {
func containsObject(_ object: Any) -> Bool {
let anObject = object as AnyObject
for obj in self {
let anObj = obj as AnyObject
return anObj.isEqual(anObject)
}
return false
}
}
let array1: [[String: Any]] = [["a": 1, "b": "123"], ["c": 456.5]]
let array2: [[String: Any]] = [["a": 1, "b": "123"]]
let result = array1.filter{ !array2.containsObject($0) }
print(result)
//[["c": 456.5]]
I decided to go this route with no extensions
getSpecialities(){ result in
for item in result
{
if (self.addedArray.contains(where: { $0["id"] as! Int == item["id"] as! Int }) == false) {
self.allArray.append(["id" : item["id"]!, "text" : item["text"]!])
}
}
self.specialitiesAll.reloadData()
}

How to sort JSON Data in Array in swift 4

I have JSON array like this
var json = NSArray() // array with json objects
//print json >>
json = (
{
Name = "Alen";
Score = 500;
},
{
Name = "John";
Score = 0;
},
{
Name = "Mark";
Score = 2000;
},
{
Name = "Steve";
Score = 300;
},
{
Name = "Ricky";
Score = 900;
}
)
and i can access its objects as
(json[0] as! NSDictionary).object(forKey: "Name")
(json[0] as! NSDictionary).object(forKey: "Score")
I want to sort this JSON array according to scores.
I found the answers like
let sortedArray = json.sorted(by: { $0.0 < $1.0 })
which gives error
Value of type 'Any' has no member '0'
Then I tried this
let sortedArray = (json as! NSDictionary).sorted {(aDic, bDic) -> Bool in
return aDic.key < bDic.key
}
It gave error
Binary operator '<' cannot be applied to two 'Any' operands
Can you please guide me to sort the array according to score in swift 4?
That's a very good example why you are strongly discouraged from using NSArray and NSDictionary in Swift.
Both collection types don't provide type information so everything is treated as Any. Most of the shared generic API of the Swift Standard library cannot be used with Any so you are not able to take advantage of the powerful generic functions unless you add a lot of ugly type casts.
If all values are String declare your array as
var json = [[String:String]]()
Then you can sort the array with
let sortedArray = json.sorted { $0["Score"]! < $1["Score"]! }
The most recommended solution is to decode the JSON directly into a custom struct
struct Player : Decodable {
let name : String
let score : String
private enum CodingKeys : String, CodingKey { case name = "Name", score = "Score" }
}
Then you get rid of all type casting and you can sort by the property name
var players = [Player]()
let jsonString = """
[{"Name" : "Alen", "Score" : "500"},
{"Name" : "John", "Score" : "0"},
{"Name" : "Mark", "Score" : "2000"},
{"Name" : "Steve", "Score" : "300"},
{"Name" : "Ricky", "Score" : "900"}]
"""
let data = Data(jsonString.utf8)
do {
players = try JSONDecoder().decode([Player].self, from: data)
let sortedPlayers = players.sorted{ $0.score.compare($1.score, options: .numeric) == .orderedAscending }
print(sortedPlayers)
} catch { print(error) }
Edit:
To load the JSON use an asynchronous way (URLSession)
Never load data from a remote URL with synchronous Data(contentsOf.
var players = [Player]()
let jsonUrl = URL(string: "url.json")!
let task = URLSession.shared.dataTask(with : url) { [unowned self] (data, _, error) in
if let error = error { print(error); return }
do {
players = try JSONDecoder().decode([Player].self, from: data!).sorted{ $0.score < $1.score }
DispatchQueue.main.async { // reload the table view if necessary
self.tableView.reloadData()
}
} catch { print(error) }
}
task.resume()
After parsing your json, you can sort your score array like this
var scoreArray = ["500", "0", "2000", "300", "900"]
array.sort { $0.compare($1, options: .numeric) == .orderedAscending }
I did something like this before
First I created two arrays of dictionary
var jsonArray = [(name:String, score:String)]()
var sortedscoreArray:[(name: String, score: String)] = []
and in getting json data you can create for loop
for I in 0..< jsonData.count{
Let jsonInfo = jsonData[i]
jsonArray.append((name: jsonInfo[“Name”].string!, score: jsonInfo[“Score"].string!))
}
and after you fill the json array pass it to sortedArray
sortedscoreArray = jsonArray.sorted(by: { $0.score < $1.score })
If array contains dictionary then you can use this code for sorting:
let sortedArray = json.sort { $0["Score"] as! Int < $1["Score"] as! Int }
print(sortedArray)
and if you are using bean class then you can use dot(.) properties for sorting:
let sortedArray = json.sort { $0.Score < $1.Score }
print(sortedArray)
let sortedResults = self.json?.sorted(by: {$0.name ?? EMPTY_STRING < $1.name ?? EMPTY_STRING }) ?? []

How to split string into Int:String Dictionary

So I'm trying to split a string that would look like this:
let Ingredients = "1:egg,4:cheese,2:flour,50:sugar"
and I'm attempting to get a dictionary output like this
var decipheredIngredients : [Int:String] = [
1 : "egg",
4 : "cheese",
2 : "flour",
50 : "sugar"
]
Here is the code that I am attempting this with
func decipherIngredients(input: String) -> [String:Int]{
let splitStringArray = input.split(separator: ",")
var decipheredIngredients : [String:Int] = [:]
for _ in splitStringArray {
decipheredIngredients.append(splitStringArray.split(separator: ":"))
}
return decipheredIngredients
}
When I try this I get an error saying I can't append to the dictionary. I've tried other methods like this:
func decipherIngredients(input: String) -> [String.SubSequence]{
let splitStringArray = input.split(separator: ",")
return splitStringArray
}
let newThing = decipherIngredients(input: "1:egg,4:cheese,2:flour,50:sugar").split(separator: ":")
print(newThing)
but I get this as the output of the function
[ArraySlice(["1:egg", "4:cheese", "2:flour", "50:sugar"])]
An alternative approach using Swift 4 and functional programming:
let ingredients = "1:egg,4:cheese,2:flour,50:sugar"
let decipheredIngredients = ingredients.split(separator: ",").reduce(into: [Int: String]()) {
let ingredient = $1.split(separator: ":")
if let first = ingredient.first, let key = Int(first), let value = ingredient.last {
$0[key] = String(value)
}
}
print(decipheredIngredients)
Swift 3
try this, assuming you want dictionary keys of type Int and values of type String
func decipherIngredients(_ input: String) -> [Int:String] {
var decipheredIngredients : [Int:String] = [:]
let keyValueArray = input.components(separatedBy: ",")
for keyValue in keyValueArray {
let components = keyValue.components(separatedBy: ":")
decipheredIngredients[Int(components[0])!] = components[1]
}
return decipheredIngredients
}

How to sort array according to number of occurrence of string?

How to sort array according to number of occurrence of string
Example :
var array = ["Hello","Me","That","Me","Hello","Me","as","the"]
and sorted array should be like this
["Me","Hello","That","as","the"]
Updated For Swift 3
var array = ["Hello","Me","That","Me","Hello","Me","as","the"]
var counts:[String:Int] = [:]
for item in array {
counts[item] = (counts[item] ?? 0) + 1
}
print(counts)
let result = counts.sorted { $0.value > $1.value }.map { $0.key }
print(result)
array.removeAll()
for string in result {
array.append(string)
}
print(array)
This is what I have been able to come up with:
var array = ["Hello","Me","That","Me","Hello","Me","as","the"]
// record the occurences of each item
var dict = [String: Int]()
for item in array {
if dict[item] == nil {
dict[item] = 1
} else {
dict[item]! += 1
}
}
// here I sort the dictionary by comparing the occurrences and map it so that the result contains only the key (the string)
let result = dict.sorted { $0.value > $1.value }.map { $0.key }
Try this -
It is tested and working as expected --
let arrayName = ["Hello","Me","That","Me","Hello","Me","as","the"]
var counts:[String:Int] = [:]
for item in arrayName {
counts[item] = (counts[item] ?? 0) + 1
}
let array = counts.keysSortedByValue(isOrderedBefore: >)
print(array) // Output - ["Me", "Hello", "the", "That", "as"]
Create Dictionary extension -
extension Dictionary {
func sortedKeys(isOrderedBefore:(Key,Key) -> Bool) -> [Key] {
return Array(self.keys).sorted(by: isOrderedBefore)
}
// Faster because of no lookups, may take more memory because of duplicating contents
func keysSortedByValue(isOrderedBefore:(Value, Value) -> Bool) -> [Key] {
return Array(self)
.sorted() {
let (_, lv) = $0
let (_, rv) = $1
return isOrderedBefore(lv, rv)
}
.map {
let (k, _) = $0
return k
}
}
}
It looks simple.
1. Take distinct from your array.
2. Make count according to distinct list.
3. Save results in collection - ie Dictionary.
4. Sort new collection.
Loop through the array and maintain a word count dictionary. Make sure the dictionary can be sorted based on values and finally obtain the set of keys and transform it back into an array.
This should work.
var array = ["Hello","Me","That","Me","Hello","Me","as","the"]
var tR : [String : Int] = [:]
let finalResult = array.reduce(tR) { result, item in
var tArr : [String: Int] = result
if let count = tArr[item] {
tArr[item] = count+1
} else {
tArr[item] = 1
}
return tArr
}
.sorted(by: { item1, item2 in
return item1.value > item2.value
}).map() { $0.key }
Please try this, hope it helps
var terms = ["Hello","Me","That","Me","Hello","Me","as","the"]
var termFrequencies = [String: Int]()
for t in terms {
if termFrequencies[t] == nil {
termFrequencies[t] = 1
} else {
termFrequencies[t] = termFrequencies[t]! + 1
}
}
for value in terms {
let index = termFrequencies[value] ?? 0
termFrequencies[value] = index + 1
}
let result = termFrequencies.sorted{$0.1 > $1.1}.map{$0.0}

calling filter on complex dictionary (associative array) in swift

I have this array:
class Filter {
var key = ""
var value = ""
init(key: String, value: String) {
self.key = key
self.value = value
}
}
let arry = [
"a":[Filter(key:"city",value:"aachen"),Filter(key:"city",value:"augsburg")],
"b":[Filter(key:"city",value:"bremen"),Filter(key:"city",value:"berlin")]
]
and I want to look for augsburg and remove it from the dictionary with the filter function so the output looks like this:
let arry = [
"a":[Filter(key:"city",value:"aachen")],
"b":[Filter(key:"city",value:"bremen"),Filter(key:"city",value:"berlin")]
]
I tried it with many filter and map constelations but I always get this structure as result:
let arry = [
["a":[Filter(key:"city",value:"aachen")]],
["b":[Filter(key:"city",value:"bremen"),Filter(key:"city",value:"berlin")]]
]
for example with this filter:
arry.map({ key,values in
return [key:values.filter{$0.value != "augsburg"}]
})
What is the problem here? How can I filter and map over more complex objects?
Maybe one thing you should know is that map method of Dictionary returns Array, not Dictionary.
public func map<T>(_ transform: (Key, Value) throws -> T) rethrows -> [T]
So, if you want the filtered result as Dictionary, you may need to use reduce:
class Filter: CustomStringConvertible {
var key = ""
var value = ""
init(key: String, value: String) {
self.key = key
self.value = value
}
//For debugging
var description: String {
return "<Filter: key=\(key), value=\(value)>"
}
}
let dict = [
"a":[Filter(key:"city",value:"aachen"),Filter(key:"city",value:"augsburg")],
"b":[Filter(key:"city",value:"bremen"),Filter(key:"city",value:"berlin")]
]
let filteredDict = dict.reduce([:]) {tempDict, nextPair in
var mutableDict = tempDict
mutableDict[nextPair.key] = nextPair.value.filter {$0.value != "augsburg"}
return mutableDict
}
(Generally, Swift Dictionary is a hash-table based implementation of associative array, but you should better avoid naming arry for Dictionary variables. Such naming is so confusing.)
Or else simply use for-in loop:
var resultDict: [String: [Filter]] = [:]
for (key, value) in dict {
resultDict[key] = value.filter {$0.value != "augsburg"}
}
print(resultDict) //->["b": [<Filter: key=city, value=bremen>, <Filter: key=city, value=berlin>], "a": [<Filter: key=city, value=aachen>]]