【问题标题】:Compare two plist files or two NSDictionary in iOS在 iOS 中比较两个 plist 文件或两个 NSDictionary
【发布时间】:2015-10-05 22:30:50
【问题描述】:

我从服务器下载了一个 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 文件或 NS 字典的内容? 哪个功能最适合执行上述操作我必须在应用程序创建/恢复/重新启动时执行此操作? 应该兼容ios7和ios8 SDK。

【问题讨论】:

标签: ios objective-c ios7 ios8 nsdictionary


【解决方案1】:

如果您想要所有更改的键,请按照以下步骤操作:- 这将返回所有更改的键的数组。

为 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);   

【讨论】:

    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多