hellosheikh hellosheikh - 7 months ago 33
Swift Question

get the specific record from nsarray

hello In nsarray the data is like this

(
{
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 I get only that record which has trip id which I give. like from the code how can I get that record which has
trip_id
41

Answer

You need to use NSPredicate to filter your object from NSArray. Following snippet will find object from 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 a Predicate with mapping to trip_id 
NSPredicate *filterByTrip = [NSPredicate predicateWithFormat:@"%K.%K CONTAINS[c] %@", @"Trip",@"trip_id",@"41"];   

// Filter your main array with predicate, resulting array will have filtered objects 
NSArray *filteredArray = [tripAarray filteredArrayUsingPredicate:filterByTrip];

NSLog(@"%@",filteredArray);

Swift

// Create a Predicate with mapping to trip_id
var filterByTrip: NSPredicate = NSPredicate(format: "%K.%K CONTAINS[c] %@", "Trip", "trip_id", @"41")

// Filter your main array with predicate, resulting array will have filtered objects
var filteredArray: [AnyObject] = tripAarray.filteredArrayUsingPredicate(filterByTrip)
NSLog("%@", filteredArray)