-4

I have NSMutableArray as below.

products = (
        {
        fullName = Fahim;
        id = 1;
        photoName = "family-1.png";
    },
        {
        fullName = Nikhat;
        id = 2;
        photoName = "family-2.png";
    },
        {
        fullName = Naju;
        id = 3;
        photoName = "family-3.png";
    },
        {
        fullName = Dasd;
        id = 7;
        photoName = "family-7.png";
    },
        {
        fullName = Galib;
        id = 8;
        photoName = "family-8.png";
    }
)

Now what I want is pull data based on id.

e.x. if I say I need data for id 8, I should get fullName as Galib.

Any idea how to get this done? I am week in arrays.

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276

2 Answers2

2

In general you can replace value by %d as well.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id == %d",YOUR_ID_VALUE];

For now you can use below,

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id == 8"];
NSArray *filter = [array filteredArrayUsingPredicate:predicate];
NSLog(@"%@",filter);
Apurv
  • 17,116
  • 8
  • 51
  • 67
1

If you need to pull by id, use NSDictionary instead.

NSDictionary * products = @{
    @1: @{ @"fullName": @"Fahim", @"photoName":@"family-1.png"}, 
    @2: @{ @"fullName": @"Nikhat" @"photoName":@"family-2.png"}
};

That's still not the optimum way of doing it... hard to devise that without knowing your full requirements.

Sanjay Chaudhry
  • 3,181
  • 1
  • 22
  • 31