NSPredicate 未按预期给出结果
Posted
技术标签:
【中文标题】NSPredicate 未按预期给出结果【英文标题】:NSPredicate not giving result as expected 【发布时间】:2014-04-29 09:05:52 【问题描述】:我有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];
【问题讨论】:
在您的代码中,predArray
使用 mmfilter
,而不是 predFilter
。这可能是问题所在。我刚刚运行了你的代码,它对我有用。
【参考方案1】:
NSString *searchString = @"2";
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"Carton_CartonNumber == %@",
searchString];
NSArray *filteredArray = [shiftsCartoonsArray filteredArrayUsingPredicate:resultPredicate];
【讨论】:
@"Carton_CartonNumber == %d", [searchString intValue]
和@"Carton_CartonNumber == %@", searchString]
有什么区别。 毕竟它们只是 NSString
第一个是如果你将它存储为一个整数然后将它转换为 int 值,否则它是一个字符串值。
@FahimParkar,这个答案的不同之处在于他使用的是[predicateWithFormat:@"..."]
而不是[predicateWithFormat:[NSString stringWithFormat@"..."]]
。这意味着@Chetan 的版本实际上让NSPredicate
处理格式,而不是仅仅提供一个简单的字符串,这就是你的答案。
有趣。我试过了,效果很好。如果我使用[NSPredicate predicateWithFormat:@"Carton_CartonNumber == %@", @"2"]
我会得到正确的答案,但如果我使用[NSPredicate predicateWithFormat:@"Carton_CartonNumber == %d", 2]
它会失败。
@GuyKogus :查看编辑后的答案。这对我有用。 %d 部分是错误的.. 抱歉【参考方案2】:
以下代码可以正常工作:
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
谓词是问题所在,正如您在 cmets 中所说,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]);
您会在上面更新的代码中找到您正在寻找的正确谓词。
【讨论】:
Chetan 上面的回答更简洁。我建议改用那个。【参考方案3】:我注意到Carton_CartonNumber
是字符串而不是整数,因此我必须用单引号括起来..
下面的伎俩...
NSPredicate *predFilter = [NSPredicate predicateWithFormat:
[NSString stringWithFormat:@"Carton_CartonNumber = '%d'", 2];
^ ^
NSArray *predArray = [shiftsCartoonsArray filteredArrayUsingPredicate: predFilter];
【讨论】:
以上是关于NSPredicate 未按预期给出结果的主要内容,如果未能解决你的问题,请参考以下文章