I am collecting Ids in an array from an array of Dictionaries like this
NSArray *places= #[
{place_id = #"3dsfdDfGDH";
place_url = #"/Peru/Lambayeque/Chiclayo";
place_name = #"Peru";}
,
{place_id = #"HUHiKcZVU7ItMyQ";
place_url = #"/Peru/La+Libertad/Huanchaco";
place_name = #"Peru";}
,
{place_id = #"7JL1K5FVUbg0vg";
place_url = #"/United+Kingdom/England/Buckley+Hill";
place_name = #"United Kingdom";}
];
NSArray *placeIds= [places valueForKeyPath:place_id];
It is working perfectly but What I want is another Array with place_id as well as place_url but NOT Name.
something Like
I want NSArray *PlaceIdAndURL to have dictionaries like below
#[
{place_id = #"3dsfdDfGDH";
place_url = #"/Peru/Lambayeque/Chiclayo";}
,
{place_id = #"HUHiKcZVU7ItMyQ";
place_url = #"/Peru/La+Libertad/Huanchaco";}
,
{place_id = #"7JL1K5FVUbg0vg";
place_url = #"/United+Kingdom/England/Buckley+Hill";}
];
How can I get without looping whole array just like the one i got first one with ValueForKeyPath above
I don't think it is possible in one liner that won't iterate....but here is the code that will do the job. Actually, you can make this logic into a method and add as your category to NSArray and NSMutableArray...
places_dup will contain the dictionaries you are looking for.
NSArray *places= #[
#{#"place_id" : #"NFxmX.VVU7LtQwQ",
#"place_url" : #"/Peru/Lambayeque/Chiclayo",
#"place_name" : #"dadfasdf"}
,
#{#"place_id" : #"HUHiKcZVU7ItMyQ",
#"place_url" : #"/Peru/La+Libertad/Huanchaco",
#"place_name" : #"dadfasdf"}
,
#{#"place_id" : #"7JL1K5FVUbg0vg",
#"place_url" : #"/United+Kingdom/England/Buckley+Hill",
#"place_name" : #"dadfasdf"}
];
NSMutableArray *places_dup = [#[] mutableCopy];
[places enumerateObjectsUsingBlock:^(id item, NSUInteger idx, BOOL *stop) {
NSDictionary *dictionary = (NSDictionary *) item;
NSMutableDictionary *filteredDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionary];
[filteredDictionary removeObjectForKey:#"place_name"];
[places_dup addObject:filteredDictionary];
}];
Related
Hi I have prepared static dictionary with data like Like
{ Age = 25, Name = Ajay;}
My requirement is I want to add this dictionary into My Array, I need a format like
({ Age = 25, Name = Ajay;})
My Code:
mainArray = [[NSMutableArray alloc] init];
dictMain = [[NSMutableDictionary alloc] init];
// dictMain = #{ #"Name" : #"Ajay", #"Age" : #"25" };
[dictMain setValue:#"Ajay" forKey:#"Name"];
[dictMain setValue:#"25" forKey:#"Age"];
[self dictMain];
I would do this like so:
NSArray *myArray = #[ #{#"Name": #"Ajay", #"Age": #"25"} ];
This will give you an immutable array with the data you want. Note though, that I would use #25 for your age (which will give you an NSNumber), instead of #"25", which will give you a string.
And if you really need to return a dictionary of an array of a dictionary, then you can do:
NSDictionary *myDict = #{ #[ #{#"Name": #"Ajay", #"Age": #"25"} ] };
Add NSDictionary to NSMutableArray.
NSMutableArray *mainArray = [[NSMutableArray alloc] init];
NSDictionary *dictMain = #{ #"Name" : #"Ajay", #"Age" : #"25" };
[mainArray addObject: dictMain];
Update:
NSDictionary *dictMain = #{ #"Name" : #"Ajay", #"Age" : #"25" };
NSArray *mainArry = #[dictMain];
NSDictionary *dict = #{#"key" : mainArry};
I have 3 MutableArray's Named:
tvShows
tvNetworks
tvdbID
I need to sort them by the name of the tvShows.
But the need to stay linked.
So e.g.:
tvShows = Breaking Bad, House, Community;
tvNetworks = AMC, FOX, NBC;
tvdbID = 81189, 73255, 94571;
Needs To Become:
tvShows = Breaking Bad, Community, House;
tvNetworks = AMC, NBC, FOX;
tvdbID = 81189, 94571, 73255;
How would I do this? It's my first app so sorry if it's a realy easy question.
store them in an array of dictionaries then sort with an NSArray sort function: (below)
NSDictionary * dict1 = #{#"title":#"breaking bad",#"network":#"AMC",#"tvbdID":#(81189)};
NSDictionary * dict2 = #{#"title":#"house",#"network":#"FOX",#"tvbdID":#(73255)};
NSDictionary * dict3 = #{#"title":#"Community",#"network":#"NBC",#"tvbdID":#(94571)};
NSArray * array = #[dict1,dict2,dict3];
NSSortDescriptor * desc = [NSSortDescriptor sortDescriptorWithKey:#"title"ascending:YES selector:#selector(caseInsensitiveCompare:)];
NSArray * sortedArray = [array sortedArrayUsingDescriptors:#[desc]];
I would personally create a custom NSObject called TVShow, that has properties of showName, network, and tvbdID. This way, you only have one array of each show. Assuming your array is called myShows, you could do something like this:
[allShows sortUsingComparitor:^NSComparisonResult(id a, id b) {
NSString *firstName = [(TVShow*)a showName];
NSString *secondName = [(TVShow*)b showName];
return [firstName compare: secondName];
}];
That is, if you wanted to sort by show name. You can swap network for showName if you wanted to sort by network!
No idea what your end goal is, but you should probably create a TVShow class that has properties (i.e., instance variables) for "title," "network", and "dbid." Then you can instantiate three TVShow objects with their appropriate properties, put them in a mutable array, and use one of the sorting methods on NSMutableArray -- I'd probably choose sortUsingComparator:.
you can't do it with 3 independent arrays but maybe with 1 dictionary where the keys are tv shows and the value is a dictionary with 2 keys: tvNetworks & tvdbIDs
sample:
NSDictionary *data = #{#"Breaking Bad":#{#"tv" : #"AMC", #"tvdb": #(81189)},
#"House":#{#"tv" : #"FOX", #"tvdb": #(73255)},
#"Community":#{#"tv" : #"NBC", #"tvdb": #(94571)}};
NSArray *sortedShows = [data.allKeys sortedArrayUsingSelector:#selector(compare:)];
for (id show in sortedShows) {
NSLog(#"%# = %#", show, data[show]);
}
One of the easiest and most straightforward ways to do this would be to create one array of dictionaries, like this:
NSMutableArray *tvShowInfos = [NSMutableArray array];
for (NSInteger i = 0; i < tvShows.count; i++) {
NSDictionary *info = #{#"show": [tvShows objectAtIndex:i],
#"network": [tvNetworks objectAtIndex:i],
#"id": [tvdbIDs objectAtIndex:i]};
[tvShowInfos addObject:info];
}
You can then sort that array easily:
[tvShowInfos sortUsingDescriptors:#[ [[NSSortDescriptor alloc] initWithKey:#"show" ascending:YES] ]];
If you need an array that contains all networks, sorted by show title, you can then use valueForKey: on the array of dictionaries:
NSArray *networksSortedByShow = [tvShowInfos valueForKey:#"network"];
I want to sort the Dictionary value following is my dictionary response that have four key value pair like- "artist_id", "artworks_count","first_name","last_name". I want to sort it according to "first_name" how to do. please help me.
2012-03-02 16:10:41.299 Paddle8[4928:207] The currentArtistNameDict is-- >{
"artist_id" = 370;
"artworks_count" = 1;
"first_name" = Gerri;
"last_name" = Davis;
}
2012-03-02 16:10:41.300 Paddle8[4928:207] The currentArtistNameDict is-- >{
"artist_id" = 369;
"artworks_count" = 1;
"first_name" = Stephen;
"last_name" = Cimini;
}
2012-03-02 16:10:41.302 Paddle8[4928:207] The currentArtistNameDict is-- >{
"artist_id" = 367;
"artworks_count" = 1;
"first_name" = Melinda;
"last_name" = Buie;
}
2012-03-02 16:10:41.305 Paddle8[4928:207] The currentArtistNameDict is-- >{
"artist_id" = 358;
"artworks_count" = 7;
"first_name" = Kcho;
"last_name" = "<null>";
}
Any help is highly Appreciated..
NSDictionary objects are by definition unsorted, so you'd be best off iterating through your dictionary and putting the items into an NSMutableArray, and then using the sortUsingDescriptors method to sort the array.
Here's an example that might help:
scores = [NSMutableArray arrayWithArray:[defaults
objectForKey:#"HighScores"]];
[scores addObject:[NSDictionary dictionaryWithObjectsAndKeys:#"Andrew", #"Name", [NSNumber numberWithUnsignedInt:45], #"Score", nil]];
[scores addObject:[NSDictionary dictionaryWithObjectsAndKeys:#"Andrew2", #"Name", [NSNumber numberWithUnsignedInt:55], #"Score", nil]];
[scores sortUsingDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:#"Score" ascending:NO] autorelease]]];
(taken from http://www.iphonedevsdk.com/forum/iphone-sdk-development/6146-sort-array-dictionary-objects.html)
I have an array of NSDictionary.
NSDictionary* dictionary = [NSDictionary dictionaryWithObjects:bothUserName forKeys:bothUID]; // here array "bothUserName" and "bothUID" is an NSArray type
[dictionary keysSortedByValueUsingSelector:#selector(compare:)];
NSLog(#" dictionary objects %#",dictionary);
I am getting an output like this.
dictionary objects {
14172368 = webtickle;
271882407 = electrodealio;
314125883 = Coral5mz;
316212228 = ajaysinghHF2;
316348693 = Caroline99a;
43944597 = WorldStuffer;
}
but I want to have output like this.
dictionary objects {
316212228 = ajaysinghHF2;
316348693 = Caroline99a;
314125883 = Coral5mz;
271882407 = electrodealio;
14172368 = webtickle;
43944597 = WorldStuffer;
}
Thanks in advance.
keysSortedByValueUsingSelector returns a sorted array containing dictionary's keys, you have to use this returned array to retrieve the associated objects:
NSDictionary* dictionary = [NSDictionary dictionaryWithObjects:bothUserName forKeys:bothUID];
NSArray *sortedKeys = [dictionary keysSortedByValueUsingSelector:#selector(compare:)];
for (NSString *key in sortedKeys) {
NSLog(#"%#: %#", key, [dictionary objectForKey:key]);
}
I'm trying to pull two values from this Dictionary, But the values I'm getting have "()" around them. Any Ideas what is causing this?
Here is the ServerOutput:
{"Rows":[{"userid":"1","location":"beach"}]}
Dictionary after JSON:
{
Rows = (
{
location = beach;
userid = 1;
}
);
}
This is what I'm getting:
location : (
beach
)
user Id : (
1
)
Both the userid and the location key values have the "()". Here is the code. Thanks a lot.
NSString *serverOutput= [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
if(serverOutput > 1){
SBJSON *jsonFF = [[SBJSON new] autorelease];
NSError *error3 = nil;
NSDictionary *useridDict= [jsonFF objectWithString:serverOutput error:&error3];
NSLog(#"useridDict: %#",useridDict);
idreturn = [[useridDict valueForKey:#"Rows"] valueForKey:#"userid"];
locationreturn = [[useridDict valueForKey:#"Rows"] valueForKey:#"location"];
NSLog(#" user Id : %#", idreturn);
NSLog(#" location : %#", locationreturn);
Just to clarify what is going on. When parsing JSON {} gets returned as a dictionary and [] gets retured as an array. So we have useridDict an NSDictionary containing the parsed data.
'useridDict' has one key Rows which returns an NSArray.
NSArray *useridArray = [useridDict objectForKey:#"Rows"];
Our useridArray has one element, an NSDictionary
NSDictionary *dict = [useridArray objectAtIndex:0];
This dict contains the two keys: location and userid
NSString *location = [dict objectForKey:#"location"];
NSInteger userid = [[dict objectForKey:#"userid"] intValue];
You can use like this.
idreturn = [[[useridDict valueForKey:#"Rows"] objectAtIndex:0]valueForKey:#"userid"];
locationreturn = [[[useridDict valueForKey:#"Rows"] objectAtIndex:0] valueForKey:#"location"];