【发布时间】:2011-11-09 13:41:51
【问题描述】:
我有一个 UIView 对象数组。我想在这个数组上调用- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate 来获取MyCustomView 对象的数组。
如何用“isKindOf:”编写谓词?
【问题讨论】:
标签: iphone objective-c ios nspredicate
我有一个 UIView 对象数组。我想在这个数组上调用- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate 来获取MyCustomView 对象的数组。
如何用“isKindOf:”编写谓词?
【问题讨论】:
标签: iphone objective-c ios nspredicate
尝试(已弃用)
[NSPredicate predicateWithFormat: @"className == %@", [someObject className]]
或者
[NSPredicate predicateWithFormat: @"class == %@", [someObject class]]
【讨论】:
className一起使用。虽然class 看起来更干净。
?和*可以作为通配符使用。 developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/…
isMemberOfClass:,而不是原始问题的isKindOfClass:?
我在使用 Jef 的方法时遇到了错误。不过,这对我有用。
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"self isKindOfClass: %@", class];
【讨论】:
对于任何感兴趣的人,这里是 Jef 方法的 Swift 版本:
NSPredicate(format: "className = %@", String(describing: MyCustomView.self))
【讨论】:
使用-className 作为您的密钥怎么样?
NSPredicate* foo = [NSPredicate predicateWithFormat: @"className == %@", @"MyCustomView"];
【讨论】: