In swift i am using this code to get particular object for a particular value
if let layer = self.layers.first(where: {$0.id == id}) { }
I want to use this same in objective-c. How should i get a object from array of objects for particular value
The predicateWithFormat solution is short, but not as type-safe as Swift.
To make it a bit more type-safe you could use indexOfObjectPassingTest.
Assuming you have:
@interface MyLayer
@property int layerID;
@end
NSArray<MyLayer *> *layers = @[...];
int layerIDToFind = 123;
You can write:
NSUInteger index = [layers indexOfObjectPassingTest:^BOOL(MyLayer *layer, NSUInteger idx, BOOL *stop) {
return layer.layerID == layerIDToFind;
}];
if (index != NSNotFound) {
MyLayer *layer = layers[index];
// ... act on the layer ...
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id == %@", id];
NSArray *result = [self.layers filteredArrayUsingPredicate:predicate];
if result.count > 0
{
id layer = [result firstObject].id
}
layerhere? Is it some kind of array of modal ?