Warm tip: This article is reproduced from serverfault.com, please click

iOS documentDirectory different between Swift en Objective-C version of app

发布于 2020-11-28 15:42:59

I have an old app written in Objective-c that I am rewriting in SwiftUI.

I noticed that the path for the documents folder is different between the two versons, so I cannot use data from the older version in the newer version.

These are the paths:

~/Library/Developer/CoreSimulator/Devices/8E2C0F01-ABF3-4414-A01A-EE4FFEF8D187/data/Containers/Data/Application/8B5EB288-0240-4437-8F20-F58521D12EAD/Documents/
~/Library/Developer/CoreSimulator/Devices/8E2C0F01-ABF3-4414-A01A-EE4FFEF8D187/data/Containers/Data/Application/0C07AF64-EA56-473E-B30A-D1FCE22E2F65/Documents/

As you can see, the first part is identical, but the second part after Containers/Data/Application is different.

I set the path as follows in Objective-C:

NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;

And in Swift:

let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

Why are they not equal, what am I missing here?

Questioner
koen
Viewed
0
matt 2020-11-29 00:01:07

There is no problem. iOS apps are sandboxed. Every app has its own private folder hierarchy. In fact, the path to the hierarchy can change even between runs of the same app. So an expression like

Application/8B5EB288-0240-4437-8F20-F58521D12EAD

is just the current sandbox path for this particular run of the app. You can't save it or use it for anything, as the same sandbox can have a different path next time.