I have an NSArray full of NSDictionary object. This structure is built from a database. I’m building a verification function that is only included and executed when debugging. The idea is to make sure all the files need are in fact included and any extra files are not included.
Based on this question I tried to use a NSPredicate but its flagging every file in thr directory as “File not in Database”.
What am I doing wrong?
KEY_DFILE is #defined as @"dFilename".
#ifdef DEBUG
- (void)checkItems
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSLog(@"-------- Database / File Check --------");
for (NSDictionary* item in self.allItmes)
{
// ansi items
if ([[item objectForKey:KEY_TYPE] isEqualToString:@"ansi"])
{
// Make sure the datafile exists
NSString* path = [[NSBundle mainBundle] pathForResource:[item objectForKey: KEY_DFILE] ofType:nil inDirectory:@"ansi"];
if (![fileManager fileExistsAtPath:path])
NSLog(@"Can't find file: %@", path);
}
}
NSString *dir = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ansi"];
NSDirectoryEnumerator* en = [fileManager enumeratorAtPath:dir];
NSString* file;
while (file = [en nextObject])
{
NSPredicate *p = [NSPredicate predicateWithFormat:@"%@ == %@", KEY_DFILE, file];
NSArray *matchedDicts = [self.allItmes filteredArrayUsingPredicate:p];
if ([matchedDicts count] == 0)
{
NSLog(@"File not in Database: %@", file);
}
else if ([matchedDicts count] > 1)
{
NSLog(@"File in Database more than once: %@", file);
}
}
NSLog(@"---------------------------------------");
}
#endif
Change this line:
To this:
The %K says that the argument is a dynamic property name and not a string (so it doesn’t quote it).