Domanda

I have NSMutableArray as below.

(
        {
        "Carton_Apartment" = "";
        "Carton_AreaCode" = 111;
        "Carton_AreaGroupCode" = 1;
        "Carton_AreaGroupName" = Group1;
        "Carton_AreaName" = Bayan;
        "Carton_Avenue" = "";
        "Carton_Block" = 1;
        "Carton_CartonNumber" = 1;
        "Carton_CustomerCode" = 6880;
        "Carton_CustomerName" = "Name 1";
        "Carton_DeliveryNotes" = YES;
        "Carton_DeliveryShiftCode" = 1;
        "Carton_DietType" = "LBS-750";
        "Carton_Floor" = "";
        "Carton_HomeNo" = 45;
        "Carton_HomePhone" = 111111;
        "Carton_IsDelivered" = false;
        "Carton_Latitude" = "";
        "Carton_Longitude" = "";
        "Carton_Mobile" = 1111111;
        "Carton_OfficePhone" = "";
        "Carton_StreetName" = 2;
        "Carton_SubscriptionDate" = "2014-04-29T00:00:00";
        "Carton_Title" = "Mrs.";
    },
        {
        "Carton_Apartment" = "";
        "Carton_AreaCode" = 111;
        "Carton_AreaGroupCode" = 1;
        "Carton_AreaGroupName" = Group1;
        "Carton_AreaName" = Bayan;
        "Carton_Avenue" = "";
        "Carton_Block" = 1;
        "Carton_CartonNumber" = 2;
        "Carton_CustomerCode" = 8314;
        "Carton_CustomerName" = "Name 2";
        "Carton_DeliveryNotes" = YES;
        "Carton_DeliveryShiftCode" = 1;
        "Carton_DietType" = ND1350;
        "Carton_Floor" = "";
        "Carton_HomeNo" = 45;
        "Carton_HomePhone" = 222222;
        "Carton_IsDelivered" = false;
        "Carton_Latitude" = "";
        "Carton_Longitude" = "";
        "Carton_Mobile" = 111111;
        "Carton_OfficePhone" = "";
        "Carton_StreetName" = 2;
        "Carton_SubscriptionDate" = "2014-04-29T00:00:00";
        "Carton_Title" = "Mr.";
    }
)

Actually this array is of length of size more then 500.

Now I am trying to extract array based on Carton_CartonNumber. So I tried using NSPredicate as below.

NSPredicate *predFilter = [NSPredicate predicateWithFormat:
    [NSString stringWithFormat:@"Carton_CartonNumber = %d", 2];
                                                            ^ this is coming from another array as intValue

NSArray *predArray = [shiftsCartoonsArray filteredArrayUsingPredicate: predFilter];

When I try to print the data for predArray I get nothing.

Any idea why this is happening?


Answer

I noticed that number was string and not integer and because of that I had to enclose in single quotes..

Below did the trick...

NSPredicate *predFilter = [NSPredicate predicateWithFormat:
    [NSString stringWithFormat:@"Carton_CartonNumber = '%d'", 2];
                                                       ^  ^ 

NSArray *predArray = [shiftsCartoonsArray filteredArrayUsingPredicate: predFilter];
È stato utile?

Soluzione 3

I noticed that Carton_CartonNumber was string and not integer and because of that I had to enclose in single quotes..

Below did the trick...

NSPredicate *predFilter = [NSPredicate predicateWithFormat:
    [NSString stringWithFormat:@"Carton_CartonNumber = '%d'", 2];
                                                       ^  ^ 

NSArray *predArray = [shiftsCartoonsArray filteredArrayUsingPredicate: predFilter];

Altri suggerimenti

NSString *searchString = @"2"; 

NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"Carton_CartonNumber == %@",
                                    searchString];
NSArray *filteredArray = [shiftsCartoonsArray filteredArrayUsingPredicate:resultPredicate];

The following code works just fine:

NSArray *array = @[
                   @{
                       @"Carton_Apartment" : @"",
                       @"Carton_AreaCode" : @111,
                       @"Carton_AreaGroupCode" : @1,
                       @"Carton_AreaGroupName" : @"Group1",
                       @"Carton_AreaName" : @"Bayan",
                       @"Carton_Avenue" : @"",
                       @"Carton_Block" : @1,
                       @"Carton_CartonNumber" : @1,
                       @"Carton_CustomerCode" : @6880,
                       @"Carton_CustomerName" : @"Name 1",
                       @"Carton_DeliveryNotes" : @YES,
                       @"Carton_DeliveryShiftCode" : @1,
                       @"Carton_DietType" : @"LBS-750",
                       @"Carton_Floor" : @"",
                       @"Carton_HomeNo" : @45,
                       @"Carton_HomePhone" : @111111,
                       @"Carton_IsDelivered" : @NO,
                       @"Carton_Latitude" : @"",
                       @"Carton_Longitude" : @"",
                       @"Carton_Mobile" : @1111111,
                       @"Carton_OfficePhone" : @"",
                       @"Carton_StreetName" : @2,
                       @"Carton_SubscriptionDate" : @"2014-04-29T00:00:00",
                       @"Carton_Title" : @"Mrs."
                       },
                   @{
                       @"Carton_Apartment" : @"",
                       @"Carton_AreaCode" : @111,
                       @"Carton_AreaGroupCode" : @1,
                       @"Carton_AreaGroupName" : @"Group1",
                       @"Carton_AreaName" : @"Bayan",
                       @"Carton_Avenue" : @"",
                       @"Carton_Block" : @1,
                       @"Carton_CartonNumber" : @2,
                       @"Carton_CustomerCode" : @8314,
                       @"Carton_CustomerName" : @"Name 2",
                       @"Carton_DeliveryNotes" : @YES,
                       @"Carton_DeliveryShiftCode" : @1,
                       @"Carton_DietType" : @"ND1350",
                       @"Carton_Floor" : @"",
                       @"Carton_HomeNo" : @45,
                       @"Carton_HomePhone" : @222222,
                       @"Carton_IsDelivered" : @NO,
                       @"Carton_Latitude" : @"",
                       @"Carton_Longitude" : @"",
                       @"Carton_Mobile" : @111111,
                       @"Carton_OfficePhone" : @"",
                       @"Carton_StreetName" : @2,
                       @"Carton_SubscriptionDate" : @"2014-04-29T00:00:00",
                       @"Carton_Title" : @"Mr."
                       }
                   ];
NSPredicate *predFilter = [NSPredicate predicateWithFormat:
                           [NSString stringWithFormat:@"Carton_CartonNumber = %d", 2]];
NSLog(@"%@", [array filteredArrayUsingPredicate:predFilter]);

Your problem is not with the predicate you're using.

Update

I was wrong and it is the predicate that's the problem, as you've stated in the comments that Carton_CartonNumber is a string. I updated the above code to be:

NSArray *array = @[
                   @{
                       @"Carton_Apartment" : @"",
                       @"Carton_AreaCode" : @111,
                       @"Carton_AreaGroupCode" : @1,
                       @"Carton_AreaGroupName" : @"Group1",
                       @"Carton_AreaName" : @"Bayan",
                       @"Carton_Avenue" : @"",
                       @"Carton_Block" : @1,
                       @"Carton_CartonNumber" : @"1",
                       @"Carton_CustomerCode" : @6880,
                       @"Carton_CustomerName" : @"Name 1",
                       @"Carton_DeliveryNotes" : @YES,
                       @"Carton_DeliveryShiftCode" : @1,
                       @"Carton_DietType" : @"LBS-750",
                       @"Carton_Floor" : @"",
                       @"Carton_HomeNo" : @45,
                       @"Carton_HomePhone" : @111111,
                       @"Carton_IsDelivered" : @NO,
                       @"Carton_Latitude" : @"",
                       @"Carton_Longitude" : @"",
                       @"Carton_Mobile" : @1111111,
                       @"Carton_OfficePhone" : @"",
                       @"Carton_StreetName" : @2,
                       @"Carton_SubscriptionDate" : @"2014-04-29T00:00:00",
                       @"Carton_Title" : @"Mrs."
                       },
                   @{
                       @"Carton_Apartment" : @"",
                       @"Carton_AreaCode" : @111,
                       @"Carton_AreaGroupCode" : @1,
                       @"Carton_AreaGroupName" : @"Group1",
                       @"Carton_AreaName" : @"Bayan",
                       @"Carton_Avenue" : @"",
                       @"Carton_Block" : @1,
                       @"Carton_CartonNumber" : @"2",
                       @"Carton_CustomerCode" : @8314,
                       @"Carton_CustomerName" : @"Name 2",
                       @"Carton_DeliveryNotes" : @YES,
                       @"Carton_DeliveryShiftCode" : @1,
                       @"Carton_DietType" : @"ND1350",
                       @"Carton_Floor" : @"",
                       @"Carton_HomeNo" : @45,
                       @"Carton_HomePhone" : @222222,
                       @"Carton_IsDelivered" : @NO,
                       @"Carton_Latitude" : @"",
                       @"Carton_Longitude" : @"",
                       @"Carton_Mobile" : @111111,
                       @"Carton_OfficePhone" : @"",
                       @"Carton_StreetName" : @2,
                       @"Carton_SubscriptionDate" : @"2014-04-29T00:00:00",
                       @"Carton_Title" : @"Mr."
                       }
                   ];
NSPredicate *predFilter = [NSPredicate predicateWithFormat:
                           [NSString stringWithFormat:@"Carton_CartonNumber = \"%d\"", 2]];
NSLog(@"%@", [array filteredArrayUsingPredicate:predFilter]);

You'll find the proper predicate that you're looking for in the updated code above.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top