我正在尝试在我的应用程序中管理子视图。我搜索了网络,最近遇到了NSPredicate课程。我检查了一些例子,但我在这里停留了一些地方。如果有人可以帮助或纠正我,我会很高兴。
我在UIView类中获得了一组子视图。我可以通过这个命令得到这个数组,就像你所知道的那样:“self.subviews”。我想获得一个子视图数组,其中x位置是greatr然后2100,代码如下:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"frame.origin.x >= 2100.0"];
NSLog(@"%d", [[self.subviews filteredArrayUsingPredicate:predicate] count]);
但它给我一个这样的错误:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSConcreteValue 0xef2a2f0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key origin.'
你能帮忙吗?
答案 0 :(得分:5)
这不起作用,因为frame
是struct
而不是对象而origin
是此结构的成员。您无法使用键值编码来访问struct成员。
您可以尝试在UIView
上添加类别,向UIView添加-(CGFloat)x
方法,返回视图的x位置。那么谓词格式就是@"x >= 2100"
。
答案 1 :(得分:0)
另一种方法是使用UIView
类别添加只返回-(CGFloat) frameOriginX
的{{1}}方法,然后使用[self frame].origin.x
作为谓词中的keyPath。 / p>