ios - get the specific record from nsarray -
hello in nsarray data
( { trip = { "trip_id" = 41; "trip_post_date" = "2016-03-28 07:52:19"; "user_id" = 65; }; user = { "first_name" = irfan; "last_name" = sheikh; "user_id" = 65; }; "arrival_country" = { "city_name" = "feldkirchen in karnten"; "country_name" = austria; id = 272221; }; "departure_country" = { "city_name" = "colonia la tordilla"; "country_name" = argentina; id = 1234057; }; }, { trip = { "trip_id" = 40; "trip_post_date" = "2016-03-28 07:50:48"; "user_id" = 65; }; user = { "first_name" = irfan; "last_name" = sheikh; }; "arrival_country" = { "city_name" = karachi; "country_name" = pakistan; id = 67008; }; "departure_country" = { "city_name" = lahore; "country_name" = pakistan; id = 112623; }; } )
how can record has trip id give. code how can record has trip_id
41
you need use nspredicate
filter object nsarray
. following snippet find object nested array.
json
nsarray *tripaarray = @[ @{@"trip":@{ @"trip_id" : @"41", @"trip_post_date" : @"2016-03-28 07:52:19", @"user_id" : @65 }, @"user" :@{ @"first_name" : @"irfan", @"last_name" : @"sheikh", @"user_id" : @65 }, @"arrival_country" :@{ @"city_name" : @"feldkirchen in karnten", @"country_name" : @"austria", @"id" : @272221 }, @"departure_country" :@{ @"city_name" : @"colonia la tordilla", @"country_name" : @"argentina", @"id" : @1234057 } }, @{@"trip":@{ @"trip_id" : @"42", @"trip_post_date" : @"2016-03-28 07:52:19", @"user_id" : @65 }, @"user" :@{ @"first_name" : @"irfan", @"last_name" : @"sheikh", @"user_id" : @65 }, @"arrival_country" :@{ @"city_name" : @"feldkirchen in karnten", @"country_name" : @"austria", @"id" : @272221 }, @"departure_country" :@{ @"city_name" : @"colonia la tordilla", @"country_name" : @"argentina", @"id" : @1234057 } }];
objective-c
// create predicate mapping trip_id nspredicate *filterbytrip = [nspredicate predicatewithformat:@"%k.%k contains[c] %@", @"trip",@"trip_id",@"41"]; // filter main array predicate, resulting array have filtered objects nsarray *filteredarray = [tripaarray filteredarrayusingpredicate:filterbytrip]; nslog(@"%@",filteredarray);
swift
// create predicate mapping trip_id var filterbytrip: nspredicate = nspredicate(format: "%k.%k contains[c] %@", "trip", "trip_id", @"41") // filter main array predicate, resulting array have filtered objects var filteredarray: [anyobject] = tripaarray.filteredarrayusingpredicate(filterbytrip) nslog("%@", filteredarray)
Comments
Post a Comment