I have two xml files (a.xml and b.xml) in my file system (iPhone). Now I want to know if these files contain exactly the same data. Most of the time, this comparison would be true, because b.xml is the result of a copyItemAtPath: operation. Unless it is overwritten by newer information. What would be the most efficient way to compare these files?
Any suggestion is very welcome.
Thanks ahead
Sjakelien
Update:
I ended up doing this:
oldData = [NSData dataWithContentsOfFile:PathToAXML];
newData = [NSData dataWithContentsOfFile:PathToBXML];
and then compare it with:
[newData isEqualToData:oldData];
The question is still: is that more efficient than:
oldData = [NSString dataWithContentsOfFile:PathToAXML];
newData = [NSString dataWithContentsOfFile:PathToBXML];
[newData isEqualToString:oldData];
You could also use:
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr contentsEqualAtPath:PathToAXML andPath:PathToBXML])
NSLog (@"File contents match");
else
NSLog (@"File contents do not match");
One alternative to comparing the file contents is to track the file modification date -- if it's later than the date you created the copy, you might assume that it has been updated.
See fileAttributesAtPath:traverseLink: in the NSFileManager
class.
BOOL filesAreEqual = [[NSData dataWithContentsOfMappedFile:file1] isEqual:[NSData dataWithContentsOfMappedFile:file2]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With