|
| 1 | +#import "StyleFilterExample.h" |
| 2 | + |
| 3 | +@implementation StyleFilterExample |
| 4 | + |
| 5 | +// This will filter the data passed in |
| 6 | +-(NSData *)filterData:(NSData *)data { |
| 7 | + // Don't call super |
| 8 | + |
| 9 | + // This example will remove any layer that has "metal-rendering-layer" in the id |
| 10 | + |
| 11 | + // Parse the JSON: Make the containers mutable |
| 12 | + NSError *error = nil; |
| 13 | + NSMutableDictionary *styleDictionary = [NSJSONSerialization JSONObjectWithData:data |
| 14 | + options:NSJSONReadingMutableContainers |
| 15 | + error:&error]; |
| 16 | + |
| 17 | + NSData *tempResult = data; |
| 18 | + if (styleDictionary) { |
| 19 | + |
| 20 | + // Get the layer array |
| 21 | + NSMutableArray *layerArray = [styleDictionary objectForKey:@"layers"]; |
| 22 | + |
| 23 | + // Create an array to hold which objects to remove since we can't remove them in the loop |
| 24 | + NSMutableArray *removedLayers = [NSMutableArray array]; |
| 25 | + |
| 26 | + // Loop the layers and look for any layers that have the search string in them |
| 27 | + for (NSMutableDictionary *layer in layerArray) { |
| 28 | + NSString *layerID = [layer objectForKey:@"id"]; |
| 29 | + |
| 30 | + // If we find the layers we're looking for, add them to the list of layers to remove |
| 31 | + if ([layerID containsString:@"metal-rendering-layer"]) { |
| 32 | + [removedLayers addObject:layer]; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // Go through and remove any layers that were found |
| 37 | + for (NSMutableDictionary *l in removedLayers) { |
| 38 | + [layerArray removeObject:l]; |
| 39 | + } |
| 40 | + |
| 41 | + // Re-create the JSON, this time the layers we filtered out won't be there |
| 42 | + NSData *filteredStyleJSON = [NSJSONSerialization dataWithJSONObject:styleDictionary |
| 43 | + options:0 |
| 44 | + error:&error]; |
| 45 | + |
| 46 | + // If the JSON write is successful, then set the output to the new json style |
| 47 | + if (filteredStyleJSON) { |
| 48 | + tempResult = filteredStyleJSON; |
| 49 | + } |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + // Return the data |
| 54 | + return tempResult; |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +@end |
0 commit comments