我有一個名爲SCPFLocation
的自定義對象的NSSet
,我想用每個位置的可讀格式(名爲interpretedForm
的屬性)對其進行過濾。以下是我如何做到這一點:無法使NSPredicate在自定義NSObject上工作
NSMutableSet *set = [[SCPFLocation allLocations] mutableCopy];
[set filterUsingPredicate:[NSPredicate predicateWithFormat:@"interpretedForm contains[c] '%@'", searchString]];
self.matches = [set.allObjects sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [((SCPFLocation *)obj1).interpretedForm compare:((SCPFLocation *)obj2).interpretedForm];
}];
但我不明白爲什麼這不起作用。在應用謂詞過濾器時,set
包含零個對象。我可能做錯了什麼?
SCPFLocation
是SCPFValue
的一個子類,其中SCPFLocation
繼承並覆蓋了interpretedForm
屬性。以下是SCPFLocation
的@interface
和@implementation
。
@interface SCPFLocation : SCPFValue
@property (strong, nonatomic) NSString *province;
@property (strong, nonatomic) NSString *city;
@end
@implementation SCPFLocation
- (NSString *)interpretedForm
{
if (self.city) {
return [NSString stringWithFormat:@"%@, %@", self.city, self.province];
} else {
return self.province;
}
}
@end
而且SCPFValue.h
:
@interface SCPFValue : NSObject
/*! The human-readable representation of this @c SCPFValue.
*/
@property (strong, nonatomic) NSString *interpretedForm;
/*! A representation of this @c SCPFValue when it is being passed from and to the API.
*/
@property (strong, nonatomic) NSString *originalForm;
- (id)initWithInterpretedForm:(NSString *)interpretedForm originalForm:(NSString *)originalForm;
@end
嗯......這本質上是一樣的東西格式/字符串的東西只是創建一個字符串,你剛剛推了一步你可以把predicateWithFormat放回三重檢查它的文本嗎? – Fogmeister
是的,我把它放回到沒有'NSString'的第一個表單,它停止工作。必須是某種iOS錯誤?我正在構建對於iOS 7. –
剛剛做的如何 - [NSPredicate predicateWithFormat:@「decodedForm contains [c]%@」,searchString] - 我不認爲你需要單引號%@ –