Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to list all dictionary values. Like highest mark to lowest. How can I handle this?
I need to list all marks and corresponding names and details in a table view.
2015-04-06 14:48:53.381 camelProject[3310:597950] valueforkey title = (
{
CamelID = 237;
Color = bg;
Comments = "Fhjfnfnfjfihgjfmsndidm,almcmcuirowmvn vmmc Jdmdmcm";
CompID = 235;
DateOfBirth = "/Date(1424725200000)/";
Description = "rtrtttrtrehndskvn;lkdf;lm;mgemln;lm';' jknt;l";
FromUserID = 564987631;
FromUsername = 564987631;
ID = 4;
IndvMarks = "5;5;3;4;2;2;2;2;5;9";
Marks = 39;
Name = name3sv;
OutOF = 100;
ProfilePhoto = "2D0B36F3-71B3-4B5F-A375-7AD4D0AFF1AF.jpg\n";
TOname = hasnam;
ToUserName = 564987631;
Types = dsfv;
UserID = 564987631;
fromname = hasnam;
},
{
CamelID = 237;
Color = bg;
Comments = "";
CompID = 235;
DateOfBirth = "/Date(1424725200000)/";
Description = "rtrtttrtrehndskvn;lkdf;lm;mgemln;lm';' jknt;l";
FromUserID = 564987631;
FromUsername = 564987631;
ID = 5;
IndvMarks = "5;4;5;7;6;0;6;5;4;6";
Marks = 48;
Name = name3sv;
OutOF = 100;
ProfilePhoto = "2D0B36F3-71B3-4B5F-A375-7AD4D0AFF1AF.jpg\n";
TOname = hasnam;
ToUserName = 564987631;
Types = dsfv;
UserID = 564987631;
fromname = hasnam;
},
{
CamelID = 237;
Color = bg;
Comments = Gucjhkhvnkvnvnvknkjbkvjhvvkb;
CompID = 235;
DateOfBirth = "/Date(1424725200000)/";
Description = "rtrtttrtrehndskvn;lkdf;lm;mgemln;lm';' jknt;l";
FromUserID = 564987631;
FromUsername = 564987631;
ID = 6;
IndvMarks = "3;4;4;3;5;4;4;3;3;5";
Marks = 38;
Name = name3sv;
OutOF = 100;
ProfilePhoto = "2D0B36F3-71B3-4B5F-A375-7AD4D0AFF1AF.jpg\n";
TOname = hasnam;
ToUserName = 564987631;
Types = dsfv;
UserID = 564987631;
fromname = hasnam;
},
{
CamelID = 236;
Color = wejr;
Comments = "Jfj ki idhvjfn isms isn't if jfj Jen hik";
CompID = 235;
DateOfBirth = "/Date(1427058000000)/";
Description = jwenrfernkgr;
FromUserID = 564987631;
FromUsername = 564987631;
ID = 21;
IndvMarks = "8;6;10;10;10;7;5;8;9;5";
Marks = 78;
Name = name;
OutOF = 100;
ProfilePhoto = "E7735AF4-8EAB-41E7-A3F2-3280DBED0389.jpg\n";
TOname = lukman;
ToUserName = 564987634;
Types = wenjewfn;
UserID = 564987634;
fromname = hasnam;
}
)
Get the values into a array and sort them using a sort descriptor.
NSArray *array = [dictioanry objectForKey:#"title"];
NSSortDescriptor* marksDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"Marks"
ascending:YES];
NSArray* sortedObjects = [array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:marksDescriptor, nil]];
//YOu can put this object back into NSDictionary if you want it for later
I think your dictionary should be looking like this.
So the code above should work to sort the values. Here is the sample code that I did the testing with.
Note: Save your JSON string in your test.json and add it to your project. or download the file from here and add it to your project
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"json"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSDictionary *jsonDictionary=[NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableLeaves
error:nil];
NSLog(#"%#",jsonDictionary);
NSArray *array = [jsonDictionary objectForKey:#"title"];
NSSortDescriptor* marksDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"Marks"
ascending:YES];
NSArray* sortedObjects = [array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:marksDescriptor, nil]];
NSLog(#"%#",sortedObjects);
Example :
NSArray *originalArray = #[
#{#"page_no": #"27"},
#{#"page_no": #"1"},
#{#"page_no": #"12"},
#{#"page_no": #"23"},
#{#"page_no": #"3"},
#{#"page_no": #"2"},
#{#"page_no": #"17"},
];
NSSortDescriptor *alphaNumSD = [NSSortDescriptor sortDescriptorWithKey:#"page_no"
ascending:YES
comparator:^(NSString *string1, NSString *string2)
{
return [string1 compare:string2 options:NSNumericSearch];
}];
NSArray *sortedArray = [originalArray sortedArrayUsingDescriptors:#[alphaNumSD]];
NSLog(#"%#", sortedArray);
Output :
(
{
"page_no" = 1;
},
{
"page_no" = 2;
},
{
"page_no" = 3;
},
{
"page_no" = 12;
},
{
"page_no" = 17;
},
{
"page_no" = 23;
},
{
"page_no" = 27;
}
)
It definitely works as expected.
Related
I am working with Coredata. i have an relation with the entity when i fetch my relation it gives the NSSet and i Convert this to NSMutableArray with AllObjects. The issue it gives me __NSSingleObjectArrayI instead of NSMutableArray. Can any one please help me on this. Thanks
Tired Code :-
NSSet *arrPayment = [[[dictPaymentData allValues] valueForKey:#"paymentToInvoice"] valueForKey:#"invoiceToPeople"];
NSMutableArray *arrData = [NSMutableArray arrayWithArray:[arrPayment allObjects]];
This is my __NSSingleObjectArrayI Code :-
<__NSArrayM 0x6000018464f0>(
<__NSSingleObjectArrayI 0x600000a8bdd0>(
{(
<People: 0x600002b44370> (entity: People; id: 0x91782a4d7073f8e8 <x-coredata://0785667E-51C4-4C34-B696-C4374F6315D4/People/p630> ; data: {
archiveStatus = 0;
defaultTerms = 30;
email = nil;
extra1 = nil;
extra2 = nil;
extra3 = nil;
faxNo = nil;
firstName = nil;
from = nil;
fromid = 0;
homeNo = nil;
internalNotes = nil;
lastName = nil;
mobileNO = nil;
modificationDate = "2019-01-17 11:58:50 +0000";
organization = Yogesh;
peopleId = 0;
"sa_City" = nil;
"sa_Country" = nil;
"sa_State" = nil;
"sa_Street1" = nil;
"sa_Street2" = nil;
"sa_Zip" = nil;
selectedCurrency = "en_US";
signature = nil;
sortingString = nil;
status = Cactive;
syncID = nil;
uniqueIdentifier = "6A819F69-DCB0-4B6C-BE60-03151B23541D-3341-0000043F5397E622";
userId = nil;
vatNo = nil;
})
)}
)
)
,
valueForKey: has a special behavior. Applied to an array it returns always an array of all values for the given key.
You are better off using objectForKey:
Apologies if this is a novice question, but I've been struggling to figure this out with no luck. I'm trying to pull a users' twitter timeline using the AFOAuth2Manager framework. Everything makes sense except the returned JSON object is an array and the entire object is one giant object. I obviously want to break it up into different elements and store them in a dictionary, but have not been able to figure it out so far.
This is a VERY partial example of what the array object looks like. The complete object is this with about 20 or so more tweets with this format attached. I will post the entire json object if requested, but it seems pretty pointless to post the entire thing.
Heres my code:
[manager GET:#"/1.1/statuses/user_timeline.json?screen_name=jack"
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject ) {
self.object = responseObject;
NSLog(#"Success: %#", responseObject);
if ([responseObject isKindOfClass:[NSArray class]]) {
NSLog(#"object is a nsarray class");
} else if ([responseObject isKindOfClass:[NSDictionary class]]){
NSLog(#"object is a nsdictionary class");
} else {
NSLog(#"object is a different class");
}
NSArray *response = [NSArray arrayWithObject:responseObject];
NSLog(#"count %ld", [response count]);
NSData *dataFromTwitter = [NSKeyedArchiver archivedDataWithRootObject:self.object];
NSError *parseError = nil;
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:dataFromTwitter
options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers|NSJSONReadingAllowFragments
error:&parseError];
NSLog(#"response Dict: %#", responseDict);
NSError *e = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:dataFromTwitter
options:NSJSONReadingAllowFragments
error:&e];
NSLog(#"jsonArray: %#", jsonArray);
NSError *jsonError2 = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:dataFromTwitter
options:NSJSONReadingAllowFragments
error:&jsonError2];
if ([jsonObject isKindOfClass:[NSArray class]]) {
NSLog(#"its an array!");
NSArray *jsonArray = (NSArray *)jsonObject;
NSLog(#"jsonArray2 - %#",jsonArray);
}
else {
NSLog(#"its probably a dictionary");
NSDictionary *jsonDictionary = (NSDictionary *)jsonObject;
NSLog(#"jsonDictionary - %#",jsonDictionary);
NSLog(#"error %#", jsonError2);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Failure: %#", error);
}];
{
contributors = "<null>";
coordinates = "<null>";
"created_at" = "Mon Feb 29 01:18:05 +0000 2016";
entities = {
hashtags = (
);
symbols = (
);
urls = (
);
"user_mentions" = (
{
id = 14616957;
"id_str" = 14616957;
indices = (
0,
7
);
name = "Jason Del Rey";
"screen_name" = DelRey;
},
{
id = 19040598;
"id_str" = 19040598;
indices = (
8,
18
);
name = "\U0ca0_\U0ca0";
"screen_name" = MikeIsaac;
},
{
id = 46063;
"id_str" = 46063;
indices = (
19,
30
);
name = "Hunter Walk";
"screen_name" = hunterwalk;
}
);
};
"favorite_count" = 12;
favorited = 0;
geo = "<null>";
id = 704113377920978944;
"id_str" = 704113377920978944;
"in_reply_to_screen_name" = DelRey;
"in_reply_to_status_id" = 704112911116013568;
"in_reply_to_status_id_str" = 704112911116013568;
"in_reply_to_user_id" = 14616957;
"in_reply_to_user_id_str" = 14616957;
"is_quote_status" = 0;
lang = en;
place = "<null>";
"retweet_count" = 0;
retweeted = 0;
source = "Twitter for iPhone";
text = "#DelRey #MikeIsaac #hunterwalk this changes everything";
truncated = 0;
user = {
"contributors_enabled" = 0;
"created_at" = "Tue Mar 21 20:50:14 +0000 2006";
"default_profile" = 0;
"default_profile_image" = 0;
description = "#withMalala!";
entities = {
description = {
urls = (
);
};
};
"favourites_count" = 11433;
"follow_request_sent" = "<null>";
"followers_count" = 3458722;
following = "<null>";
"friends_count" = 1859;
"geo_enabled" = 1;
"has_extended_profile" = 1;
id = 12;
"id_str" = 12;
"is_translation_enabled" = 0;
"is_translator" = 0;
lang = en;
"listed_count" = 25944;
location = "California, USA";
name = Jack;
notifications = "<null>";
"profile_background_color" = EBEBEB;
"profile_background_image_url" = "http://abs.twimg.com/images/themes/theme7/bg.gif";
"profile_background_image_url_https" = "https://abs.twimg.com/images/themes/theme7/bg.gif";
"profile_background_tile" = 0;
"profile_image_url" = "http://pbs.twimg.com/profile_images/668328458519384064/FSAIjKRl_normal.jpg";
"profile_image_url_https" = "https://pbs.twimg.com/profile_images/668328458519384064/FSAIjKRl_normal.jpg";
"profile_link_color" = 990000;
"profile_sidebar_border_color" = DFDFDF;
"profile_sidebar_fill_color" = F3F3F3;
"profile_text_color" = 333333;
"profile_use_background_image" = 1;
protected = 0;
"screen_name" = jack;
"statuses_count" = 19066;
"time_zone" = "Pacific Time (US & Canada)";
url = "<null>";
"utc_offset" = "-28800";
verified = 1;
};
},
You show no code in your question, in a comment you state you have a responseObject but not how you came by it.
The JSON you are getting from somewhere will be text, you may have it as an NSString, NSData, an NSArray whose elements are these, etc.
You need to obtain that text and then parse it as JSON, the NSJSONSerialization class will handle the parsing for you, the first line of its description states:
You use the NSJSONSerialization class to convert JSON to Foundation objects and convert Foundation objects to JSON.
Once you have those Foundation objects - arrays, dictionaries, strings, numbers, etc. you can extract the fields you are interested in.
Once you've written some code to do this if it doesn't work and you are stuck ask a new question showing your code and someone will probably be able to help you further.
HTH
I have an array(seachResult) which contains dictionaries and I want to sort this array according to 'price' key in the dictionary that is a number in string format.
I tried this code but it doesn't work for sorting 'price' but it works for pid which is a number.
how can I sort it according 'price' (number in string format)?
NSSortDescriptor *sortByPrice = [NSSortDescriptor sortDescriptorWithKey:#"price" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortByPrice];
NSArray *sortedArray = [self.seachResult sortedArrayUsingDescriptors:sortDescriptors];
NSLog(#"%#",sortedArray );
here is sample data for self.searchResult:
2013-07-17 02:04:55.012 MyApp[57014:16a03] sorted array of dictionaries: (
{
cid = 2;
image = "http:///images/loginlogo.png";
latitude = "48.245565";
longitude = "16.342333";
manual = "";
movie = "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v";
pcode = 023942435228;
pid = 1;
pname = "example product";
price = "12.00";
qrcode = "";
rid = 1;
rname = "Example Retailer Name";
sale = 0;
"sale_percent" = 0;
"sale_price" = "0.00";
text = "here is text about sample product number 1...\nasdasdasda\nsdfsdfsd\nSdfsdf\nSDfsdfs\ndfsdfsdf\n\n\n";
},
{
cid = 2;
image = "http:///testImage.png";
latitude = "48.245565";
longitude = "16.342333";
manual = "";
movie = "";
pcode = 1;
pid = 2;
pname = "sample product 2";
price = "126.00";
qrcode = "";
rid = 1;
rname = "Example Retailer Name";
sale = 1;
"sale_percent" = 20;
"sale_price" = "99.99";
text = "here is text about sample product number 2...\nblah blah blah\nasdasdasd\nASdasdas\nASdasdasd";
},
{
cid = 1;
image = "";
latitude = "";
longitude = "";
manual = "";
movie = "";
pcode = 1;
pid = 3;
pname = "test product";
price = "46.00";
qrcode = "";
rid = 2;
rname = "";
sale = 0;
"sale_percent" = 0;
"sale_price" = "35.00";
text = "some text here...
\nasdasd
\nasd
\na
\nsd
\nas
\nd";
}
)
I also tried this code :
NSSortDescriptor *hopProfileDescriptor =
[[NSSortDescriptor alloc] initWithKey:#"price"
ascending:YES];
NSArray *descriptors = [NSArray arrayWithObjects:hopProfileDescriptor, nil];
NSArray *sortedArrayOfDictionaries = [self.seachResult
sortedArrayUsingDescriptors:descriptors];
NSLog(#"sorted array of dictionaries: %#", sortedArrayOfDictionaries);
but still doesn't work.
I think the issue is that in your self.searchResult array the price data is differently formatted for the objects in the array.
The first object in the array it's formatted like price = 12; (probably a NSDecimalNumber)
The second object in the array it's formatted like price = "125.99"; (proably a NSString)
NSArray *testSorted = [test sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
NSString *price1 = obj1[#"price"];
NSString *price2 = obj2[#"price"];
NSNumber *n1 = [NSNumber numberWithFloat:[price1 floatValue]];
NSNumber *n2 = [NSNumber numberWithFloat:[price2 floatValue]];
return [n1 compare:n2];
}];
Since your price data seems to vary (NSString vs NSNumber) you might try instead using sortedArrayUsingComparator:. eg:
NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
id value1 = [obj1 objectForKey:#"price"];
float float1 = [value1 floatValue];
id value2 = [obj2 objectForKey:#"price"];
float float2 = [value2 floatValue];
if (float1 < float2) {
return NSOrderedAscending;
}
else if (float1 > float2) {
return NSOrderedDescending;
}
return NSOrderedSame;
}];
This is kinda ugly but should get you started. It's also fragile--if you get something other than NSNumber or NSString for the price it'll likely crash.
I have an array self.seachResult which contains dictionary, I want to add an item to all dictionaries I tried a code like this but I got error, can you provide me by a code that add item 'distance' to it
[[self.seachResult objectAtIndex:0] setObject:#"distance" forKey:#"400"];
this is my array of dictionary:
2013-07-19 02:13:24.929 MyApp[59321:16a03] sorted array of dictionaries: (
{
cid = 2;
image = "http:///images/loginlogo.png";
latitude = "48.245565";
longitude = "16.342333";
manual = "";
movie = "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v";
pcode = 023942435228;
pid = 1;
pname = "example product";
price = "12.00";
qrcode = "";
rid = 1;
rname = "Example Retailer Name";
sale = 0;
"sale_percent" = 0;
"sale_price" = "0.00";
text = "here is text about sample product number 1...\nasdasdasda\nsdfsdfsd\nSdfsdf\nSDfsdfs\ndfsdfsdf\n\n\n";
},
{
cid = 1;
image = "";
latitude = "";
longitude = "";
manual = "";
movie = "";
pcode = 1;
pid = 3;
pname = "test product";
price = "46.00";
qrcode = "";
rid = 2;
rname = "";
sale = 0;
"sale_percent" = 0;
"sale_price" = "35.00";
text = "some text here...
},
{
cid = 2;
image = "http:///testImage.png";
latitude = "48.245565";
longitude = "16.342333";
manual = "";
movie = "";
pcode = 1;
pid = 2;
pname = "sample product 2";
price = "126.00";
qrcode = "";
rid = 1;
rname = "Example Retailer Name";
sale = 1;
"sale_percent" = 20;
"sale_price" = "99.99";
text = "here is text about sample product number 2...\nblah blah blah\nasdasdasd \nASdasdas\nASdasdasd";
}
)
here is the error:
2013-07-19 02:49:54.261 MyApp[59376:16a03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
*** First throw call stack:
(0x3367012 0x2d27e7e 0x3366deb 0x332d347 0x13472 0x1d4d1c7 0x1d4d232 0x1d4d4da 0x1d648e5 0x1d649cb 0x1d64c76 0x1d64d71 0x1d6589b 0x1d65e93 0x1d65a88 0x20c1e63 0x20b3b99 0x1d4ddd2 0x12b9f 0x2d3b705 0x1c6f2c0 0x1c6f258 0x1d30021 0x1d3057f 0x1d2f6e8 0x1c9ecef 0x1c9ef02 0x1c7cd4a 0x1c6e698 0x37a7df9 0x37a7ad0 0x32dcbf5 0x32dc962 0x330dbb6 0x330cf44 0x330ce1b 0x37a67e3 0x37a6668 0x1c6bffc 0x288d 0x27b5)
libc++abi.dylib: terminate called throwing an exception
here is where I create it:
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
self.seachResult =[NSJSONSerialization JSONObjectWithData:responseData options:nil error:nil];
// [self.productsTableView reloadData];
NSMutableArray *testSorted = [self.seachResult sortedArrayUsingComparator:^NSComparisonResult(NSMutableDictionary *obj1, NSMutableDictionary *obj2) {
NSString *price1 = obj1[#"price"];
NSString *price2 = obj2[#"price"];
NSNumber *n1 = [NSNumber numberWithFloat:[price1 floatValue]];
NSNumber *n2 = [NSNumber numberWithFloat:[price2 floatValue]];
return [n1 compare:n2];
}];
[[self.seachResult objectAtIndex:0] setObject:#"distance" forKey:#"400"];
NSLog(#"sorted array of dictionaries: %#", testSorted);
Your objects created from JSON are all immutable.
self.seachResult =[NSJSONSerialization JSONObjectWithData:responseData options:nil error:nil];
instead of passing nil for options pass NSJSONReadingMutableContainers:
self.seachResult =[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
From the NSJSONSerialization docs:
NSJSONReadingMutableContainers
Specifies that arrays and dictionaries are created as mutable objects.
I am working on a App that makes use of a JSON API and I am Using the JSON for Objective C Framework to get the Data into a NSDictionary. The dictionary loos like this:
({
admitted = "<null>";
agreed = "<null>";
"area_id" = 1;
"area_name" = "Digitales, Urheber-/Patentrecht, Datenschutz";
created = "2010-08-17 12:41:03";
"current_draft_content" = Bla Bla Bla";
"current_draft_created" = "2010-08-17 12:42:44";
"current_draft_formatting_engine" = compat;
"discussion_url" = "http://piratenpad.de/OpenSourceZwang";
id = 502;
"informed_supporter_count" = 11;
"issue_accepted" = "";
"issue_admission_time" = "8 days";
"issue_closed" = "";
"issue_created" = "2010-08-17 12:41:03";
"issue_discussion_time" = "15 days";
"issue_fully_frozen" = "";
"issue_half_frozen" = "";
"issue_id" = 242;
"issue_population" = 372;
"issue_ranks_available" = 0;
"issue_state" = new;
"issue_verification_time" = "8 days";
"issue_vote_later" = 0;
"issue_vote_now" = 0;
"issue_voter_count" = "<null>";
"issue_voting_time" = "8 days";
name = "Alte & unsupportete Software, muss Open Source werden";
"negative_votes" = "<null>";
"policy_initiative_quorum_den" = 100;
"policy_initiative_quorum_num" = 10;
"policy_issue_quorum_den" = 100;
"policy_issue_quorum_num" = 10;
"policy_majority_den" = 2;
"policy_majority_num" = 1;
"policy_majority_strict" = 1;
"positive_votes" = "<null>";
rank = "<null>";
revoked = "<null>";
"satisfied_informed_supporter_count" = 7;
"satisfied_supporter_count" = 7;
"suggested_initiative_id" = "<null>";
"supporter_count" = 11;
},
{
admitted = "<null>";
agreed = "<null>";
"area_id" = 1;
"area_name" = "Digitales, Urheber-/Patentrecht, Datenschutz";
created = "2010-08-17 14:09:14";
"current_draft_content" = "Bla Bla Bla";
"current_draft_created" = "2010-08-17 14:09:31";
"current_draft_formatting_engine" = compat;
"discussion_url" = "";
id = 508;
"informed_supporter_count" = 0;
"issue_accepted" = "2010-08-14 01:51:45";
"issue_admission_time" = "15 days";
"issue_closed" = "";
"issue_created" = "2010-08-14 01:34:26";
"issue_discussion_time" = "30 days";
"issue_fully_frozen" = "";
"issue_half_frozen" = "";
"issue_id" = 30;
"issue_population" = 603;
"issue_ranks_available" = 0;
"issue_state" = accepted;
"issue_verification_time" = "15 days";
"issue_vote_later" = 9;
"issue_vote_now" = 0;
"issue_voter_count" = "<null>";
"issue_voting_time" = "15 days";
name = "Grundrecht: Alternative Formulierung";
"negative_votes" = "<null>";
"policy_initiative_quorum_den" = 100;
"policy_initiative_quorum_num" = 10;
"policy_issue_quorum_den" = 100;
"policy_issue_quorum_num" = 10;
"policy_majority_den" = 3;
"policy_majority_num" = 2;
"policy_majority_strict" = 0;
"positive_votes" = "<null>";
rank = "<null>";
revoked = {
};
"satisfied_informed_supporter_count" = 0;
"satisfied_supporter_count" = 0;
"suggested_initiative_id" = "<null>";
"supporter_count" = 0;
}
)
What is a good way to get all object where the "issue_id" is 2 oder 6 or what ever to display a grouped UITabeView with all objects with the same "issue_id" in one group?
You can use NSSortDescriptor to sort:
NSSortDescriptor *dateSortDescriptor = [[[NSSortDescriptor alloc] initWithKey:#"issue_id" ascending:YES selector:#selector(compare:)] autorelease];
[youDictionaryArray sortUsingDescriptors:[NSArray arrayWithObjects:dateSortDescriptor, nil]];
And/or NSPredicate to get some ID only:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(issue_id == %i)", 2];
NSArray *newArray = [youDictionaryArray filteredArrayUsingPredicate:predicate];
There are several ways. You can use NSPredicate and use the NSArray method -filteredArrayUsingPredicate:. Or on iOS 4 you use a filtering block: -indexesOfObjectsPassingTest: then use -objectsAtIndexes: to get the objects.
Otherwise the best is you just iterate through the array and filter the items you want into a new one:
// Given a NSArray *items
NSMutableArray *filteredItems = [NSMutableArray array];
for (NSDictionary *item in items) {
if (/* your condition */) {
[filteredItems addObject: item];
}
}