【问题标题】:How do I compare 2 files with the same name in iOS to check if they are copies of the same file or different files如何在iOS中比较两个同名文件以检查它们是相同文件的副本还是不同文件的副本
【发布时间】:2018-09-15 09:18:06
【问题描述】:

我正在使用 UIDocument 将文档文件保存到 Documents 目录中的本地存储沙箱中。我在 iCloud 中有另一个同名文件。如何检查这两个文件是同一个文档文件的副本还是两个同名的不同文档文件?

使用 NSFileVersion 并不能告诉我它们是否是同一个文件,只告诉我它是什么版本的文件。

我尝试获取文件的资源值,但本地存储沙箱中的文件给我 nil 作为文档标识符。

下面是使用 documentIdentifier 资源值的代码:

    let localFileResourceValues = try? self.document!.fileURL.resourceValues(forKeys: [URLResourceKey.documentIdentifierKey])

    let iCloudFileResourceValues = try? destinationURL.resourceValues(forKeys: [URLResourceKey.documentIdentifierKey])

    print("documentIdentifier", localFileResourceValues?.documentIdentifier as Any, iCloudFileResourceValues?.documentIdentifier as Any, localFileResourceValues?.documentIdentifier == iCloudFileResourceValues?.documentIdentifier)

打印结果:

documentIdentifier nil 可选(133819) false

是否可以从本地存储沙箱中的文件中获取 NSMetadataItem?

我将不胜感激。

【问题讨论】:

    标签: ios filesystems icloud


    【解决方案1】:

    要判断两个文件路径是否指向同一个文件对象,需要使用NSString stringByStandardizingPath规范化文件路径,然后使用NSString stringByResolvingSymlinksInPath:解析符号链接,然后比较路径。 This web page 有很好的描述和现成的代码。

    要确定两个文件是否具有相同的内容,即使它们可能是不同的文件对象,NSFileManager contentsEqualAtPath:andPath: 很有用。

    【讨论】:

      猜你喜欢
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-02
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      • 2022-08-14
      相关资源
      最近更新 更多