iOS diff_match_patch 使用

谷歌,diff_match_patch,在 React Native 开发时增量更新使用

从文件里读出内容
NSString *aContent = [NSString stringWithContentsOfFile:afile encoding:NSUTF8StringEncoding error:nil];
NSString *bContent = [NSString stringWithContentsOfFile:bfile encoding:NSUTF8StringEncoding error:nil];

实例化对比对象
DiffMatchPatch *dmp = [[DiffMatchPatch alloc]init];

//比对差异

NSMutableArray *diffs = [NSMutableArray array];
diffs = [dmp diff_mainOfOldString:bContent andNewString:aContent];

//生成补丁
NSMutableArray *patches = [dmp patch_makeFromDiffs:diffs];

//应用补丁
NSArray *results = [dmp patch_apply:patches toString:bContent];
results 是个数组,数组第一个字段为应用补丁后的内容。(字符串)
然后将应用后的内容回写到原文件
NSString *string = results.firstObject;
[string writeToFile:bfile atomically:YES];

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容