I am trying to manage subviews in my application. I searched web and recently meet NSPredicate class. I checked examples but I stuck at somewhere here. if someone can help or correct me I ll be glad.
I got an array of subviews in UIView class. I can get this array with this command as all you know : “self.subviews”. I want to get an array of subviews which x position is greatr then 2100, and code is like below:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"frame.origin.x >= 2100.0"];
NSLog(@"%d", [[self.subviews filteredArrayUsingPredicate:predicate] count]);
but it gives me an error like this:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSConcreteValue 0xef2a2f0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key origin.'
can u give a hand?
This won’t work because
frameis astructand not an object andoriginis a member of this struct. You cannot access struct members by using key-value coding.You could try adding a category on
UIViewto add a-(CGFloat)xmethod to UIView that returns the view’s x position. The predicate format would then simply be@"x >= 2100".