比较iOS中的两个plist文件或两个NSDictionary



我从服务器下载了一个plist文件,其中包含键值对。一旦应用程序恢复/重启,那么我必须再次下载文件并检查文件是否已更改。

下面是代码下载…我将键值存储在NSDictionary中。

 task1 = [session dataTaskWithURL:[NSURL URLWithString:[S3_SERVER_URL stringByAppendingString:propFile]] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    propfilePath = [documentsDirectory stringByAppendingString:propFile];
    NSLog(@"DestPath : %@", propfilePath);
    [receivedData appendData:data];
    NSLog(@"Succeeded! Received %lu bytes of data",(unsigned long)[data length]);
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [data writeToFile:propfilePath atomically:YES];
    if(error == nil){
        plistDictionary = [[NSDictionary dictionaryWithContentsOfFile:propfilePath] retain];
        [task2 resume];
    } else {
    }
}];

如何比较两个plist文件或nsdictionary的内容?哪个功能最适合做上面的,因为我必须这样做的应用程序创建/恢复/重启?它应该兼容ios7和ios8 SDK

如果您想要所有更改的键,请遵循以下命令:-这将返回所有更改的键的数组

为NSDictionary创建一个类别

NSDictionary + newDict.h

#import <Foundation/Foundation.h>
@interface NSDictionary (newDict)
- (NSArray*)changedKeysIn:(NSDictionary*)d;
@end

NSDictionary + newDict.m

#import "NSDictionary+newDict.h"
@implementation NSDictionary (newDict)
- (NSArray*)changedKeysIn:(NSDictionary*)d {
    NSMutableArray *changedKs = [NSMutableArray array];
    for(id k in self) {
        if(![[self objectForKey:k] isEqual:[d objectForKey:k]])
            [changedKs addObject:k];
    }
    return changedKs;
}
@end

调用:-

#import "NSDictionary+newDict.h"

: -

NSArray *keys = [dict1 changedKeysIn:dict2];
NSLog(@"%@", keys);   

你可以使用NSDictionary类中的isEqualToDictionary:方法。

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/index.html//apple_ref/occ instm/NSDictionary/isEqualToDictionary:

最新更新