I have a dictionary like <String,Loto> and Loto is object like below;
Loto:
{
"success": true,
"data": {
"oid": "64kbbqi8dbxygb00",
"hafta": 961,
"buyukIkramiyeKazananIl": "",
"cekilisTarihi": "11/04/2015",
"cekilisTuru": "SAYISAL_LOTO",
"rakamlar": "03#02#48#16#15#08",
"rakamlarNumaraSirasi": "02 - 03 - 08 - 15 - 16 - 48",
"devretti": false,
"devirSayisi": 0,
"bilenKisiler": [
{
"oid": "64kbbxi8dbxyg403",
"kisiBasinaDusenIkramiye": 7.35,
"kisiSayisi": 185712,
"tur": "$3_BILEN"
},
{
"oid": "64kbbxi8dbxyg402",
"kisiBasinaDusenIkramiye": 53.05,
"kisiSayisi": 9146,
"tur": "$4_BILEN"
},
{
"oid": "64kbbxi8dbxyg401",
"kisiBasinaDusenIkramiye": 4532.2,
"kisiSayisi": 142,
"tur": "$5_BILEN"
},
{
"oid": "64kbbxi8dbxyg400",
"kisiBasinaDusenIkramiye": 1528438.75,
"kisiSayisi": 1,
"tur": "$6_BILEN"
}
],
"buyukIkrKazananIlIlceler": [
{
"il": "10",
"ilView": "BALIKESÄ°R",
"ilce": "01001",
"ilceView": "AYVALIK"
}
],
"kibrisHasilati": 51127,
"devirTutari": 0.09,
"kolonSayisi": 10537872,
"kdv": 1599672.97,
"toplamHasilat": 10537872,
"hasilat": 8938199.03,
"sov": 893819.9,
"ikramiyeEH": 8044379.129999999,
"buyukIkramiye": 1528432.03,
"haftayaDevredenTutar": 0
}
}
So my dictionary like <"11042015",Loto> and i want to sort this dictionary by "hafta" property of loto object.
How can i do this? Please help me!
If Loto is an object with a hafta property, you can sort your dictionary by passing it into the sorted function, along with a closure that tells it how to order the entries:
sorted(dict) { $0.1.hafta < $1.1.hafta }
($0.1 and $1.1 because dictionaries present as a sequence of key/value pairs - you want to sort by a property of the value i.e. tuple entry 1)
Note, this will give you back a sorted array, of type [(String,Loto)] pairs, rather than a dictionary (as Swift dictionaries are unordered).
(if Loto is not really an object but rather another dictionary, you might need to do {$0.1["hafta"] < $1.1["hafta"]} - it really depends on how you’re holding your data - the good news is you don’t need to worry about optionals, since they can be compared with <)
If you don’t need the keys, just sort the values:
sorted(dict.values) { $0.hafta < $1.hafta }
which will give you back a sorted array of type [Loto]
Dictionaries are not ordered. What you need is to convert it to array, and then sort it. So something like this.
let lotoArray = lotoDictionary.allObjects as [Loto]
lotoArray.sort { $0.hafta < $1.hafta }
You can't. The keys of a dictionary are not sorted. You can get an array of the values and sort that array.
Related
I have a random dictionary, I want to iterate over all objects that are inside that dictionary. Is there a way to find keyPath of object that is inside the dictionary?
Let's say we have this dictionary .
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": [
"GML",
"XML"
]
},
"GlossSee": "markup"
}
}
}
}
}
Now I want to find keyPath of GlossEntry or any other object.
Consider:
You can obtain all the keys in a dictionary and iterate over them. E.g. for (NSString *key in dictionary) {...}
Given a key you can obtain the matching value and test whether it is itself a dictionary.
Recursion is your friend. You could write a function which takes a current key path prefix, a dictionary, and a mutable array to add found key paths to. The implementation of this would involve (1), (2) and a recursive call.
Now write yourself some code. If you get stuck ask a new question, include your code, and explain where you are stuck.
HTH
In a JSON model coming from a REST api I have a field that is supposed to be a Dictionary (a map, hashtable, call it whatever you want), e.g.:
"moduleMap": {
"677e55b2-d80c-4b32-bcbb-e99074fbfcf6": {
"id": "677e55b2-d80c-4b32-bcbb-e99074fbfcf6",
"startTime": 1496054599227,
"status": "ACTIVE"
},
"c20acde2-639f-4cb7-9b90-6d8d24c78166": {
"id": "c20acde2-639f-4cb7-9b90-6d8d24c78166",
"startTime": 1496054598997,
"status": "UNAVAILABLE"
}
}
As I understand RestKit (I am a newbie), usually this is mapped to an object in Swift. However, since the keys in this map are arbitrary uuids, I cannot write a class with these properties.
Can anybody point me to a direction of how to get this map into a Swift dictionary using RestKit mapping?
I would like to map it to var moduleMap: [String:DomainObject], or at least var moduleMap: NSMutableDictionary. Also, I need it to be mappable back to the same JSON.
So if I have a dictionary like the following:
{
"banners": [
{
"imageUrl": "www.google.com",
"destination": "home",
"position": 1
},
{
"imageUrl": "www.reddit.com",
"destination": "work",
"position": 2
},
{
"imageUrl": "www.imgur.com",
"destination": "play",
"position": 3
}
]
}
is there a way for me to say, "Get me the dictionary object where the value for the key 'position' = 3 without using a for loop?
Let's think about it. Oooh, there are a lot of ways! It's amazing what looking for just a moment at the documentation will tell you.
What you have as the value of the banners key is an array of dictionaries. So what you are looking for is an NSArray method. An NSArray method such as filteredArrayUsingPredicate:!
So you can write an NSPredicate that describes position as being equal to 3, and you'll get back an NSArray of all the dictionaries where that is true (in this case, an array of one dictionary).
Oh, here's another possibility: indexOfObjectPassingTest:. With this, you supply a block that specifies that the dictionary's position is 3, and you'll get back the index of that dictionary within this array (namely 2 in this case).
I could go on and on, but wouldn't it be better for you to learn to read the documentation for yourself? Here it is.
I've got a JSON as below.
odds: {
0501: {
x: 2.75,
description: "a"
},
0502: {
x: 3.25,
description: "b"
},
0513: {
x: 3.5,
description: "c"
},
0503: {
x: 3.5,
description: "d"
},
0505: {
x: 7.5,
description: "e"
},
0504: {
x: 7.5,
description: "f"
},
0512: {
x: 10,
description: "g"
}
}
This hash comes from HTTP response as I want to show but the thing that I use JSONModel to map it and there is only way to map that NSDictionary. When map this JSON to NSDictionary (as you can guess) this an unordered and sequence of data comes up mixed.
So, how to map this JSON, without broke up its sequence using JSONModel and NSDictionary ?
NSDictionary is inherently unordered:
Are keys and values in an NSDictionary ordered?
If you want to preserve the order of key-value entries, you need to use a data structure other than NSDictionary. Any library that passes your data through an NSDictionary cannot preserve the order.
Something I've done in this situation is to sort the dictionary keys in a separate array, in addition to the dictionary. Use the ordered key array to determine how to display your dictionary values.
Dictionaries can not be sorted, but as your JSON seems to be an array of objects, iterate thru your resulting NSDictionary with for...in and add the elements to a mutable array.
Afterwards sort the resulting array using .sortInPlace by comparing the x-value.
I have an NSDictionary like this :
{
"name": "Hayagreeva", //String
"age": 3, //Number
"subjects": { //Array
"rhymes": { //Dictionary 1
"test1": 10,
"test2": 20,
"test3": 30
},
"games": { //Dictionary 2
"test1": 40,
"test2": 50,
"test3": 60
},
"crayoning": { //Dictionary 3
"test1": 70,
"test2": 80,
"test3": 90
}
},
"date": "2008-02-16T10:06:00Z" //Date
}
Now I need to replace the value for key subjects > games > test1 from 40 to value 60. I know the lenghty process of taking another dictionary objects and create a similar dictionary but that process is time consuming and may be a wrong strategy. I have searched many questions also here but all of them have the way to remove the key for the first level dictionary (like removing key "subjects" in the above code). So I want to know if there is an easy and efficient way to update the value for key in an inner level as I need to execute this process at many situations in my current project. Thanks in advance.
Edit : I also tried something like
[dict removeValueForKey:#"subjects.games.test1"];
But that doesn't work. I hope that this line helps you to understand the desired functionality by me.
If that structure is set in stone and will not change then it's as simple as:
NSDictionary *topLevelDict = ...;
topLevelDict[#"subjects"][#"games"] = #{
#"test1" : #(60),
#"test2" : #(50),
#"test3" : #(60)
};
Dictionaries of dictionaries are a pain to manipulate and it's always better to create a custom model object that allows easier manipulation, validation with the additional benefit of domain-specific functionality like serialization etc.