Objective-C -> Remove Last Element In NSDictionary - iphone

EDIT:
The Code:
//stores dictionary of questions
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSData *responseData = [request responseData];
NSString *json = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSDictionary *qs = [json objectFromJSONString];
self.questions = qs;
NSLog(#"%#", questions);
[json release];
[self setQuestions];
[load fadeOut:load.view withDuration:0.7 andWait:0];
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Start" style:UIBarButtonItemStylePlain target:self action:#selector(start:)];
self.navigationItem.rightBarButtonItem = anotherButton;
}
I have the following items in an NSDictionary:
(
{
max = 120;
min = 30;
question = "Morning Bodyweight (Kg)";
questionId = 1;
questionNumber = 1;
sectionId = 1;
type = TextInput;
},
{
question = "Morning Urine Colour";
questionId = 2;
questionNumber = 2;
sectionId = 1;
type = ImagePicker;
},
{
max = 120;
min = 30;
question = "Evening Bodyweight (Kg)";
questionId = 3;
questionNumber = 3;
sectionId = 1;
type = TextInput;
},
{
question = "Evening Urine Colour";
questionId = 4;
questionNumber = 4;
sectionId = 1;
type = ImagePicker;
},
{
max = 90;
min = 40;
question = "Morning Heart Rate (BPM)";
questionId = 5;
questionNumber = 5;
sectionId = 1;
type = TextInput;
},
{
question = "Time of Month (TOM)";
questionId = 6;
questionNumber = 6;
sectionId = 1;
type = Option;
}
)
I want to remove the last element:
{
question = "Time of Month (TOM)";
questionId = 6;
questionNumber = 6;
sectionId = 1;
type = Option;
}
Is there a pop() equivalent for the NSDictionary? If not how is it possible to remove the last element?

There is no order to dictionaries so there is no 'last object'
However, this might solve your problem, though it might not always remove what you are thinking the 'last object' is:
[dictionaryName removeObjectForKey:[[dictionaryName allKeys] lastObject]];

This looks to be (or could be made to be) an array of dictionaries. If you have these dictionaries as the objects of an NSMutableArray, then you can use – removeLastObject. Otherwise, you're SOL since even NSMutableDictionary has no such method.

There is no last element in a dictionary, as elements in a dictionary are not ordered.

Can you somehow get the element by using the key value? NSDictionaries don't have an ordering, so there's no such thing as removing the "last" element.

I think that's an array of NSDictionaries you got yourself there. In which case it's very easy to do:
NSMutableArray *mArray = [NSMutableArray arrayWithArray:array]; // if not mutable
[mArray removeLastObject];

Related

How to filter array of NSDictionary for key separated by comma

I have an array of NSDictionary.NSDictionary has a key named as multiple_image key that contain string separated by ,.
I want set of array that contain 123.png for multiple_images key.
Can some one show me how to do this using NSPredicate or without predicate.
//Array
{
Image = "<UIImage: 0xf72df30>";
active = yes;
"admin_id" = 169;
"category_id" = 32;
"chef_id" = 175;
descr = "Cool tea to cool down the mind.";
id = 110;
"multiple_images" = "Jellyfish.jpg,345.png";
name = "Southern Sweet Ice Tea";
price = 160;
rating = 3;
selected = 0;
"subcat_id" = 23;
"tag_id" = 45;
"tax_id" = 10;
"tax_value" = "12.00";
},
{
Image = "<UIImage: 0xf72ebd0>";
active = yes;
"admin_id" = 169;
"category_id" = 31;
"chef_id" = 175;
descr = "Ingredients are almonds or cashews. No hydrogenated stuff, no extra weirdo ingredients";
id = 107;
"multiple_images" = "Jellyfish.jpg,123.png";
name = "Butter Chicken";
price = 300;
rating = 3;
selected = 0;
"subcat_id" = 24;
"tag_id" = 43;
"tax_id" = 9;
"tax_value" = "0.00";
},
{
Image = "<UIImage: 0xf72f870>";
active = yes;
"admin_id" = 169;
"category_id" = 31;
"chef_id" = 173;
descr = "Raw vegetables including carrots, cucumbers.";
id = 100;
"multiple_images" = "Jellyfish.jpg,shake.png,";
name = Salads;
price = 50;
rating = 3;
selected = 0;
"subcat_id" = 22;
"tag_id" = 44;
"tax_id" = 9;
"tax_value" = "0.00";
}
Using predicates,
[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"multiple_images CONTAINS '123'"]];
Using predicates, but with blocks
NSArray *filtered = [test filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:
^BOOL(NSDictionary *evaluatedObject, NSDictionary * bindings) {
NSString *key = #"123";
return ([evaluatedObject[#"multiple_images"] rangeOfString:key].location != NSNotFound);
}]];
Try
NSDictionary *item = mainArray[0];
NSString *imagesString = item[#"multiple_images"];
NSArray *images = [imagesString componentsSeparatedByString:#","];
Now you can use the images
Try this code
for (int i=0; i<[mainArray count]; i++) {
NSDictionary *item = [mainArray objectAtIndex:i];
NSString *imagesString = item[#"multiple_images"];
NSArray *images = [imagesString componentsSeparatedByString:#","];
for (int j=0;j<[images count]; j++) {
if ([[images objectAtIndex:j] isEqualToString:#"123.png"]) {
///Your Required code;
}
}
}
So you want to save the dictionary that has 123.png.
maybe you can try something like this:
NSMutableArray *arr = [NSMutableArray new];// your array of objects(dictionaries)
__block NSMutableArray *images123 = [NSMutableArray new];
[arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSMutableDictionary *dictionary = (NSMutableDictionary *)obj;
if([[dictionary allKeys]containsObject:#"multiple_images"]){
NSString *imageList = (NSString *)[dictionary objectForKey:#"multiple_images"];
NSArray *arrayImages = [imageList componentsSeparatedByString:#","];
if([arrayImages containsObject:#"123.png"]){
[images123 addObject:dictionary];
}
}
}];

Remove objects using NSPredicate

I have the folloving dictionary which has many sub dictionaries.
How can I remove objects where isChanged = 1 from parent dictionary using NSPredicate?
{
"0_496447097042228" = {
cellHeight = 437;
isChanged = 1;
};
"100000019882803_193629104095337" = {
cellHeight = 145;
isChanged = 0;
};
"100002140902243_561833243831980" = {
cellHeight = 114;
isChanged = 1;
};
"100004324964792_129813607172804" = {
cellHeight = 112;
isChanged = 0;
};
"100004324964792_129818217172343" = {
cellHeight = 127;
isChanged = 0;
};
"100004324964792_129835247170640" = {
cellHeight = 127;
isChanged = 1;
};
}
As a simple alternative to using NSPredicate, you can use the NSDictionary's built in keysOfEntriesPassingTest: This answer assumes "isChanged" is an NSString and the value 0 or 1 is an NSNumber:
NSSet *theSet = [dict keysOfEntriesPassingTest:^(id key, id obj, BOOL *stop) {
return [obj[#"isChanged"] isEqualToNumber: #1];
}];
The returned set is a list of keys that pass the test. From there, you could remove all that matched with:
[dict removeObjectsForKeys:[theSet allObjects]];
I solved my problem in the following way:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"isChanged == %d", 1];
NSArray *allObjs = [parentDict.allValues filteredArrayUsingPredicate:predicate];
[allObjs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSMutableArray *keys = [[NSMutableArray alloc] initWithCapacity:0];
[keys setArray:[parentDict allKeysForObject:obj]];
[parentDict removeObjectsForKeys:keys];
[keys release];
}];
when you have array of dictionary than you can remove selected category's data using NSPredicate
here is code
NSString *selectedCategory = #"1";
//filter array by category using predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"isChanged == %#", selectedCategory];
NSArray *filteredArray = [yourAry filteredArrayUsingPredicate:predicate];
[yourAry removeObject:[filteredArray objectAtIndex:0]];
But in your problem data is not in array it is in dictionary
your data should be in this format
(
{
cellHeight = 437;
isChanged = 1;
},
{
cellHeight = 145;
isChanged = 0;
},
{
cellHeight = 114;
isChanged = 1;
}
)

sorting array of nested NSDictionary containing an array

Im quite new to iOS and objective.. heres my question..
if my array looks like this:
myArray = {
parentdict = {
childdict = {
aname = "Aname";
bname - "Bname";
cname = "Cname";
};
childarray = {
{
counter = "1";
close = "25236";
},
{
counter = "2";
close = "12458";
};
};
};
},
{
parentdict = {
childdict = {
aname = "Aname";
bname - "Bname";
cname = "Cname";
};
childarray = {
{
counter = "1";
close = "28556";
},
{
counter = "2";
close = "12118";
};
};
};
},
{
parentdict = {
childdict = {
aname = "Aname";
bname - "Bname";
cname = "Cname";
};
childarray = {
{
counter = "1";
close = "24356";
},
{
counter = "2";
close = "155628";
};
};
};
};
basically its an array of nested dictionary and inside one of the dictionary contains an array of dictionary (childarray) if i want to sort myArray by #"close" of array index 1, which is the one next to counter 2, exactly how should i do this?..(perhaps i should use NSSortDescriptor?)
thanks for the reply
You have presented your structure in the JSON format. I assume it is for sake of presenting it to the audience here. If you are actually starting with JSON string, you will have to convert it to nested NSArrayies and NSDictionaryies using some third-party libs or iOS 5 built-in classes..
Assuming you already have your top level NSArray* myArray, give it a try to the following code:
NSArray* myArray = // ... this is your array
NSArray* sorted_array = [myArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSDictionary* left = obj1;
NSDictionary* right = obj2;
NSDictionary* left_parent_dict = [left objectForKey: #"parentdict"];
NSDictionary* right_parent_dict = [right objectForKey: #"parentdict"];
NSArray* left_child_array = [left_parent_dict objectForKey: #"childarray"];
NSArray* right_child_array = [right_parent_dict objectForKey: #"childarray"];
NSDictionary* left_child_first = [left_child_array objectAtIndex: 1];
NSDictionary* right_child_first = [right_child_array objectAtIndex: 1];
NSString* left_close = [left_child_first objectForKey: #"close"];
NSString* right_close = [right_child_first objectForKey: #"close"];
NSNumber* left_val = [NSNumber numberWithInt: [left_close intValue]];
NSNumber* right_val = [NSNumber numberWithInt: [right_close intValue]];
return [left_val compare: right_val];
} ];
You will need, of course, to add some checks which I omitted for simplicity.
If you want to get descending order modify the last statement to:
return [right_val compare: left_val];
I would also suggest you considering SQL database for complex data structures as pointed out by Niko.
Sorting an array can be quite complex, especially when the array contains nested objects. May be you should create an SQLite database
You should use iOS 5.0's built in JSON API. [Here's a link to a great tutorial.] (http://www.raywenderlich.com/5492/working-with-json-in-ios-5). JSON is really easy to work with. And that site also has great tutorials on Core Data (iOS's SQLite API) here. There are two more parts to that tutorial as well.
Try this
[array sortUsingComparator:(NSComparator)^(id obj1, id obj2){
int firstValue = [[obj1 objectForKey:#"someKey"] intValue];
int secondValue = [[obj2 objectForKey:#"someKey"] intValue];
int valueDiff = firstValue - secondValue;
return (valueDiff == 0) ? NSOrderedSame : (valueDiff < 0) ? NSOrderedAscending : NSOrderedDescending;
}];

Trouble reading JSON object using Obj-C

I am trying to read the following json object using the json-framework and obj-C
{
Sections = {
Now = "Wednesday 9 February 2011 02:40";
Section = (
{
Article = (
{
Exceprt = "text here";
ID = 49011;
Title = "text here";
Type = Politics;
audioCounter = 0;
commentsCounter = 0;
hasMore = false;
important = False;
likesCounter = 0;
photoCounter = 0;
time = "21:12";
timeStamp = "2/8/2011 9:14:16 PM";
timeStatus = True;
videoCounter = 0;
viewsCounter = 0;
},
{
Exceprt = "text here";
ID = 49010;
Title = "text here";
Type = Politics;
audioCounter = 0;
commentsCounter = 0;
hasMore = false;
important = True;
likesCounter = 0;
photoCounter = 0;
time = "20:45";
timeStamp = "2/8/2011 9:10:59 PM";
timeStatus = True;
videoCounter = 0;
viewsCounter = 0;
},
{
Exceprt = "text here";
ID = 49008;
Title = "text here";
Type = Politics;
audioCounter = 0;
commentsCounter = 0;
hasMore = false;
important = False;
likesCounter = 0;
photoCounter = 0;
time = "20:28";
timeStamp = "2/8/2011 9:09:44 PM";
timeStatus = True;
videoCounter = 0;
viewsCounter = 0;
}
);
ID = 22;
Name = "EN Live";
totalNews = 3416;
}
);
};
}
My intent is to have a list of the articles (list of dictionaries) so that I can later access them easily. I have been stuck a while on this and my code is giving me an error about calling a non existent method for NSArray which has led me to suspect that I am misunderstanding the json object. I am totally new to this and any help is greatly appreciated.
Here's my code:
NSDictionary *results = [jsonString JSONValue];
NSDictionary *Articles = [[results objectForKey:#"Sections"] objectForKey:#"Section"];
NSArray *ListOfArticles = [Articles objectForKey:#"Article"];
for (NSDictionary *article in ListOfArticles)
{
NSString *title = [article objectForKey:#"Title"];
NSLog(title);
}
Thanks !
First of all, those aren’t valid JSON data. Names (in name/value pairs) are strings and must be quoted. String values must always be quoted. Boolean values must be either true or false (lowercase). Check http://json.org/ and http://www.ietf.org/rfc/rfc4627.txt?number=4627 and http://jsonlint.com
Here’s the structure of your data:
The top level value is an object (dictionary)
This object has a name (key) called Sections whose value is itself another object (dictionary)
Sections has a name (key) called Section whose value is an array
Each element in the Section array is an object (dictionary)
Each element in the Section array has a name (key) called Article whose value is an array, as well as other names (keys): ID, title, totalNews
Each element in the Article array is an object
If your JSON data were valid, you could parse them as follows:
// 1.
NSDictionary *results = [jsonString JSONValue];
// 2.
NSDictionary *sections = [results objectForKey:#"Sections"];
// 3.
NSArray *sectionsArray = [sections objectForKey:#"Section"];
// 4.
for (NSDictionary *section in sectionsArray) {
// 5.
NSLog(#"Section ID = %#", [section objectForKey:#"ID"];
NSLog(#"Section Title = %#", [section objectForKey:#"Title"];
NSArray *articles = [section objectForKey:#"Article"];
// 6.
for (NSDictionary *article in articles) {
NSLog(#"Article ID = %#", [article objectForKey:#"ID"];
NSLog(#"Article Title = %#", [article objectForKey:#"Title"];
// …
}
}
Your JSON framework is probably parsing out an NSDictionary where you're expecting an NSArray. It'll let you assign an NSDictionary to an NSArray, but then you'll get a runtime exception when you attempt to call a method on your "array". Judging by the JSON you posted (which isn't correct JSON), this is what I would have my parsing code look like. The names of the NSDictionaries and NSArrays are simply named after the JSON attributes they represent.
NSDictionary* results = [jsonString JSONValue];
NSDictionary* sections = [results valueForKey:#"Sections"];
NSArray* section = [sections valueForKey:#"Section"];
NSArray article = [[section objectAtIndex:0] valueForKey:#"Article"];
for (NSDictionary* anArticle in article) {
NSLog(#"%#", [anArticle valueForKey:#"Title"]);
}

NSSortDescriptor can't count numbers at all

With this
NSSortDescriptor *lessonDescriptor = [[NSSortDescriptor alloc] initWithKey:#"lesson" ascending:YES];
[resultArray sortUsingDescriptors:[NSArray arrayWithObject:lessonDescriptor]];
it's sorting the "lesson"-objects to
(
{
lesson = 9;
subject = bg;
},
{
lesson = 8;
subject = bg;
},
{
lesson = 11;
subject = CAE;
},
{
lesson = 11;
subject = CAE;
},
{
lesson = 10;
subject = CAE;
},
{
lesson = 10;
subject = CAE;
},
{
lesson = 5;
subject = Gg;
},
{
lesson = 4;
subject = G;
},
{
lesson = 3;
subject = G;
},
{
lesson = 2;
subject = M;
},
{
lesson = 1;
subject = M;
}
)
as you can see it's not counting correctly.. Can somebody help?
thanks alot
edit:
don't know how to implement the value thing =)
else if ([currentElement isEqualToString:#"lesson"])
{
NSString *trimmedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(#"trimmed: %#",trimmedString);
int value = [trimmedString intValue]; ??
[currentSummary appendString:value]; ??
[currentSummary appendString:trimmedString];
}
You have to trim the string before converting the 'lesson' into numbers in order for the sort descriptors to work correctly. Otherwise, the sort descriptor sees only string values not numbers. This is how you remove the whitespace and newline characters:
NSString *stringValue = [parsedValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
A call to [stringValue intValue] will get the integer value of the string
int value = [stringValue intValue];
Save this value into the array before calling the sort descriptors on this attribute.
Tell me if it works.
It sorted it just right. After all, every entry is a string, and in a string "10" is before "9" after all...
Now if you wanted to sort it the way you expected to sort it, you either need to hold NSNuumber objects and sort that, or have strings held in normalized form like "08", "09", "10", and then possibly trim off leading 0's from the string later.
[NSSortDescriptor sortDescriptorWithKey:#"self"
ascending:YES
comparator:^(id obj1, id obj2){
return [(NSString*)obj1 compare:(NSString*)obj2
options:NSNumericSearch];
}];
sorts a range from 0 - 1000000..... onwards.