I have a NSDictionary, which looks like:
0 = {
content = Test;
date = "2012-02-02 18:36:46 +0000";
};
1 = {
content = "#### da ####";
date = "2012-02-02 18:36:46 +0000";
};
2 = {
content = dfdffdfddfdffddf;
date = "2012-02-03 20:30:31 +0000";
};
But if I delete the second key (number 1 in the example) the dictionary ends up like:
0 = {
content = Test;
date = "2012-02-02 18:36:46 +0000";
};
2 = {
content = dfdffdfddfdffddf;
date = "2012-02-03 20:30:31 +0000";
};
How could I reorganizate them so it maintains the order (like 0, 1, 2, 3) when I need to?
NSDictionaries are unordered, meaning that you cannot control the order of keys and they may change randomly or in undocumented ways between iOS versions.
You have a few options:
Maintain your own, separate NSArray of keys, and use that to index your dictionary.
Use this OrderedDictionary class that I wrote, which basically does that but wraps it all up in an NSDictionary subclass so you don't have to do any extra work:
http://charcoaldesign.co.uk/source/cocoa#ordered-dictionary
If your dictionary keys are numbers (or if they are alphabetical, or something else that's easy to sort automatically), you could just sort the keys whenever you need to display them in order using:
NSArray *keys = [dict allKeys];
keys = [keys sortedArrayUsingSelector:#selector(compare:)];
//now loop over keys array instead of directly over dictionary
Related
I want to know that remove blank index from this array.please see the code
array=( {
email = "regor#gmail.com";
fullname = regor;
photourl = "/download/userphoto/b239eb04-338d-4e93-9387-e0234850edfe.jpg";
userid = 56b09ea9088e7c6c118c9baa;
username = regor;
},
{
}
)
Instead of removing the blank index, You can store the first object into a separate dictionary.
Simply do
NSDictionary *dict=array[0];
Or If you want to remove the null from an array, try
[array removeObjectIdenticalTo:[NSNull null]];
How do I access each property in cloudKit Users. I have an array of recordsIDs that I use to fetch. I get back the records (2 in this example) in one dictionary.
NSMutableArray *recordIDs;
for (NSString *recordName in recordNames) {
CKRecordID *recordID = [[CKRecordID alloc] initWithRecordName:recordName];
[recordIDs addObject:recordID];
}
CKFetchRecordsOperation *fetchOperation = [[CKFetchRecordsOperation alloc] initWithRecordIDs:recordIDs];
fetchOperation.fetchRecordsCompletionBlock = ^(NSDictionary /* CKRecordID * -> CKRecord */ *recordsByRecordID, NSError *operationError) {
NSLog(#"recordsByRecordID %#", recordsByRecordID);
};
Console logs the following:
2015-04-11 11:04:15.829 projectName[10154:555807] recordsByRecordID {
"<CKRecordID: 0x7fea3bd86f80; _c835c6d554eafe23xxxxb65a1a9dd94d:(_defaultZone:__defaultOwner__)>" = "<CKRecord: 0x7fea44596fc0; recordType=Users, recordID=_c835c6d554eafe23xxxxb65a1a9dd94d:(_defaultZone:__defaultOwner__), recordChangeTag=i7rxe5ce, values={\n identifier = \"3E7837F6-C23C-44B2-89E8-5D9DF9AB061E\";\n points = 50;\n username = \"_c835c6d554eafe23xxxxb65a1a9dd94d\";\n}>";
"<CKRecordID: 0x7fea3e0614d0; _cd5f8486186028eaxxx6b6597489619:(_defaultZone:__defaultOwner__)>" = "<CKRecord: 0x7fea3e2567a0; recordType=Users, recordID=_cd5f8486186028eaxxx6b6597489619:(_defaultZone:__defaultOwner__), recordChangeTag=i7s7706j, values={\n identifier = \"4B3B374E-42B0-47B7-8638-A8967C23FA21\";\n points = 250;\n username = \"_cd5f8486186028eaxxx6b6597489619\";\n}>";
}
I want to retrieve the username and points and add it to a local dictionary.
I got it. Looks like I need to pass in the recordID for each record then the key to retrieve.
for (CKRecordID *recordID in recordsByRecordID) {
NSString *pointsString = recordsByRecordID[recordID][PointsKey];
NSLog(#"string %#", pointsString);
}
I have this dictionary in objective c:
{
awayScore = 2;
awayTeam = "Kode IF";
events = (
{
eventType = "01:00";
name = "01:00";
},
{
eventType = "01:00";
name = "01:00";
}
);
homeScore = 1;
homeTeam = "Partille IF FK ";
time = "01:00";
}
Now for populating a tableview i would need to know the size of "events" items so i can return the number of rows. Also how could i iterate then through them? (now I'm thinking when I'm going to use the values to populate the cells)
so 1) How can i get the size of it
and 2) How can i iterate through them and get the different values in uitableview
I know how to iterate through ALL THE KEYS, but since i don't know how to just loop through a specific key that has many items i can't show you what i have done so far
Thanks!!
Your JSon events object should be a JSon Array, so you can fast enumerate through it.
`
NSArray *events = jsonDictonary[#"events"];
for (NSDictonary *event in events) {
//parse each event
}
Where JsonDictionary is your entire Json object.
submittedTestArray = [{
scheduledTestId = "F7F15169-2FB6-4E47-A971-BAAD40D152AB";
studentId = "8b3df16f-ff2f-4ad4-839c-6a937b79854d";
submittedTestId = "37F20871-FB3A-47AA-B254-9E5409C5E4C4";
}, {
scheduledTestId = "440947DD-0A01-4DB8-8DD5-CCFA8F852FD3";
studentId = "8b3df16f-ff2f-4ad4-839c-6a937b79854d";
submittedTestId = "681689DC-B35C-491C-A737-AB19D9116FD9";
}, {
scheduledTestId = "440947DD-0A01-4DB8-8DD5-CCFA8F852FD3";
studentId = "8b3df16f-ff2f-4ad4-839c-6a937b79854d";
submittedTestId = "681689DC-B35C-491C-A737-AB19D9116FD9";
}, {
scheduledTestId = "440947DD-0A01-4DB8-8DD5-CCFA8F852FD3";
studentId = "e18ead0a-d0fc-4e15-96ee-ecbd32880d97";
submittedTestId = "A46965C6-14B1-401E-97C5-AD5BADCC02EE";
}]
I want all the NSDictionary objects uniquely based on scheduledTestId
If i will consider above example then it will give me two NSDictionary objects.
Temporarily i am using following code.
NSMutableArray *uniqueKeys = [NSMutableArray arrayWithArray:[submittedTestArray valueForKeyPath:#"#distinctUnionOfObjects.scheduledTestId"]];
NSMutableArray *submittedTestUniqueArray = [[NSMutableArray alloc] initWithCapacity:uniqueKeys.count];
for (SubmittedTest *submittedTest in submittedTestArray) {
if ([uniqueKeys containsObject:submittedTest.scheduledTestId ]) {
[uniqueKeys removeObject:submittedTest.scheduledTestId];
[submittedTestUniqueArray addObject:submittedTest];
}
}
submittedTestUniqueArray is contain my desired output i.e.
[{
scheduledTestId = "F7F15169-2FB6-4E47-A971-BAAD40D152AB";
studentId = "8b3df16f-ff2f-4ad4-839c-6a937b79854d";
submittedTestId = "37F20871-FB3A-47AA-B254-9E5409C5E4C4";
}, {
scheduledTestId = "440947DD-0A01-4DB8-8DD5-CCFA8F852FD3";
studentId = "8b3df16f-ff2f-4ad4-839c-6a937b79854d";
submittedTestId = "681689DC-B35C-491C-A737-AB19D9116FD9";
}]
Instead of for-in loop, go with a for loop and after iterating each time
if ([uniqueKeys containsObject:submittedTest.scheduledTestId ]) {
[uniqueKeys removeObject:submittedTest.scheduledTestId];
}
else
{
[submittedTestArray removeObjectAtIndex:currentIndex];
}
I dont know what I am writing is up-to the mark but still I am daring to answer.
Ideally what you have tried is perfect. Coz to remove duplicate records from the dictionary, there is a need to have a bunch of records, in this case
an array named uniqueKeys with scheduledTestId values, to compare each dictionary element with all other elements for duplication.
And this is what exactly you have done.
The only improvements I can suggest in this code is instead of NSMutableArray *uniqueKeys
change it to NSArray *uniqueKeys; and after finishing the comparison & collecting the dictionary objects with the unique records, flush the array elements instead of removing them one by one.
Coz having 2 mutable arrays, one having simply unique keys & other having dictionary objects with unique records doesn't make any sense. Of course I presume that you dont need an array with only unique keys. What you need is dictionary not an array, right?
So in short I dont find any performance improvements in your code.
#ALL: Please let me know if there are any improvements in my technical knowledge.
CHEERS HAPPY CODING
I am very new to programming in Objective C (as in a few days), and unfortunately have an iPhone app due as a project for a class in a few days (in retrospect I probably should have chosen something different). One of the main features my app needs is the ability to select a dish from a menu by first selecting the meal on one page, and then the category on the next page, and then the dish on the final page. I have gotten my app to download a JSON file from an internet API and store it as an NSArray variable, but now I need to use that array to populate my tableview. Below is a sample from the downloaded array from my app's debug window. I already have the basic framework of the app (i.e. several different views) in place. This came from a sample app that populates the tableview rows with data from a plist file, but I want to use this JSON data. How would I use this array to populate the rows with the JSON data stored in this NSArray variable?
Thanks!
2012-12-05 03:29:48.973 MLBMobile[3858:c07] {
category = "BREAKFAST MEATS";
date = "2012-12-05";
meal = BREAKFAST;
name = "Low-Sodium Ham";
portion = 1;
recipe = 319044;
unit = oz;
}
2012-12-05 03:29:48.975 MLBMobile[3858:c07] {
category = "BREAKFAST MEATS";
date = "2012-12-05";
meal = BREAKFAST;
name = "Roasted Low-Sodium Turkey";
portion = 4;
recipe = 113503;
unit = oz;
}
2012-12-05 03:29:48.976 MLBMobile[3858:c07] {
category = "BREAKFAST ENTREES";
date = "2012-12-05";
meal = BREAKFAST;
name = "Cheddar Cheese";
portion = 1;
recipe = 130029;
unit = oz;
}
2012-12-05 03:29:48.976 MLBMobile[3858:c07] {
category = "BREAKFAST ENTREES";
date = "2012-12-05";
meal = BREAKFAST;
name = "Hard Cooked Eggs - Cage Free";
portion = 1;
recipe = 061009;
unit = each;
}
With your array variable you can iterate through each element. Use fast enumeration:
for(NSDictionary *dict in yourArrayVariable){
NSString *category = [dict objectForKey:#"name"];
//...you get the point
}
Now you can populate your row by just setting the properties of the row. For example, if you are using the default UITableViewCell:
cell.textLabel.text = name;
There is a bit more to this that you can do for better data/model/view separation, but I have just included examples as if it were all done locally in one file.