我有NSMutableArray
如下。
(
{
"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.";
}
)
实际上这个数组的长度大于500。
现在我尝试根据Carton_CartonNumber
提取数组。所以我尝试使用NSPredicate
,如下所示。
NSPredicate *predFilter = [NSPredicate predicateWithFormat:
[NSString stringWithFormat:@"Carton_CartonNumber = %d", 2];
^ this is coming from another array as intValue
NSArray *predArray = [shiftsCartoonsArray filteredArrayUsingPredicate: predFilter];
当我尝试打印predArray
的数据时,我什么都没得到。
知道为什么会这样吗?
我注意到这个数字是字符串而不是整数,因此我必须用单引号括起来..
以下是诀窍......
NSPredicate *predFilter = [NSPredicate predicateWithFormat:
[NSString stringWithFormat:@"Carton_CartonNumber = '%d'", 2];
^ ^
NSArray *predArray = [shiftsCartoonsArray filteredArrayUsingPredicate: predFilter];
答案 0 :(得分:1)
NSString *searchString = @"2";
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"Carton_CartonNumber == %@",
searchString];
NSArray *filteredArray = [shiftsCartoonsArray filteredArrayUsingPredicate:resultPredicate];
答案 1 :(得分:0)
以下代码可以正常使用:
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]);
您的问题不在于您使用的谓词。
<强>更新强>
我错了,is
谓词就是问题所在,正如你在Carton_CartonNumber
是一个字符串的评论中所说的那样。我将上面的代码更新为:
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]);
您将在上面的更新代码中找到您正在寻找的正确谓词。
答案 2 :(得分:0)
我注意到Carton_CartonNumber
是字符串而不是整数,因此我必须用单引号括起来。
以下是诀窍......
NSPredicate *predFilter = [NSPredicate predicateWithFormat:
[NSString stringWithFormat:@"Carton_CartonNumber = '%d'", 2];
^ ^
NSArray *predArray = [shiftsCartoonsArray filteredArrayUsingPredicate: predFilter];