NSPredicate查询UIView的子视图

Sar*_*aya 2 iphone nspredicate

我正在尝试在我的应用程序中管理子视图.我搜索了网络,最近遇到了NSPredicate课程.我检查了一些例子,但我在这里停留了一些地方.如果有人可以帮助或纠正我,我会很高兴.

我在UIView类中得到了一个子视图数组.我可以通过这个命令得到这个数组,就像你所知道的那样:"self.subviews".我想获得一个子视图数组,其中x位置是greatr然后2100,代码如下所示:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"frame.origin.x >= 2100.0"];
NSLog(@"%d", [[self.subviews filteredArrayUsingPredicate:predicate] count]);
Run Code Online (Sandbox Code Playgroud)

但它给我一个这样的错误:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSConcreteValue 0xef2a2f0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key origin.'
Run Code Online (Sandbox Code Playgroud)

你能帮忙吗?

Ole*_*ann 5

这不起作用,因为它frame是一个struct而不是一个对象,并且origin是这个结构的成员.您不能使用键值编码来访问struct成员.

您可以尝试添加类别UIView-(CGFloat)x向UIView 添加返回视图x位置的方法.那么谓词格式就是@"x >= 2100".