Xcode 11 Playgrounds似乎改变了playgroundSharedDataDirectory
的行为:
要复制,请将以下内容粘贴到操场上,并分别在Xcode 10和Xcode 11中执行:
import PlaygroundSupport
print(playgroundSharedDataDirectory)
let str = try String(contentsOf: playgroundSharedDataDirectory.appendingPathComponent("data.json")) // assuming you create ~/Documents/Shared Playground Data/data.json
playgroundSharedDataDirectory
解析为:
Xcode 11.2:
file:///Users/:username/Library/Developer/XCPGDevices/DB897D66-E542-44BD-B6CF-0176A3E480A9/data/Containers/Data/Application/2F952093-BD81-4B0C-AD45-8B5E45B4D7AB/Documents/Shared%20Playground%20Data/
❌file:///Users/:username/Documents/Shared%20Playground%20Data/
✅Xcode 10.2.1:
file:///var/folders/s7/x9ltynsn2zg_djfbtzszhn8d4zn6gb/T/com.apple.dt.Xcode.pg/containers/com.apple.dt.playground.stub.iOS_Simulator.MyPlayground-5985DF99-2BFE-45AC-9F34-70620F1DCD17/Documents/Shared%20Playground%20Data/
✅file:///Users/:username/Documents/Shared%20Playground%20Data/
✅在Xcode 10.2.1中,iOS游乐场符号链接到~/Documents/Shared Playground Data
,在Xcode 11+中不再起作用。
我想知道,这是否是有意更改?如果是这样,如何从iOS游乐场访问共享文件?
PS:我看到了一个相关问题的答案(请参阅https://stackoverflow.com/a/57981133/1324861,但是,如果我正在运行的代码需要iOS游乐场,则答案实际上并不适用。
iOS和macOS游乐场具有指向“共享游乐场数据”文件夹的不同路径。Xcode 11不再自动链接两个目录,但这不是一个大问题。
打开终端并输入open .
并从查找器中将文件夹拖到此处,或从游乐场ex复制并粘贴路径
open . /Users/:username/Library/Developer/XCPGDevices/DB897D66-E542-44BD-B6CF-0176A3E480A9/data/Containers/Data/Application/2F952093-BD81-4B0C-AD45-8B5E45B4D7AB/Documents/Shared%20Playground%20Data/
以上将在您的查找器中打开该文件夹。现在,您将其链接到/Users/:username/Documents/
中的一个它实际上与创建别名相同。请拖放或复制并粘贴路径。终端中的命令是`ln -s,其后是原始文件夹的路径,然后是别名的路径,中间有一个空格:
ln -s /Users/:username/Library/Developer/XCPGDevices/DB897D66-E542-44BD-B6CF-0176A3E480A9/data/Containers/Data/Application/2F952093-BD81-4B0C-AD45-8B5E45B4D7AB/Documents/Shared%20Playground%20Data/ /Users/:username/Documents/
我使用了上面提供的路径,当然,实际情况下它们会有所不同。
打开一个新的macOS游乐场和一个iOS游乐场。做这个测试:
import PlaygroundSupport let fileName = "text.txt" let fileText = "Hello World!" print(playgroundSharedDataDirectory) let fileUrl = playgroundSharedDataDirectory.appendingPathComponent(fileName) do { try fileText.write(to: fileUrl, atomically: true, encoding: .utf8) } catch { print(error) }
检查数据是否已正确写入。这是macOS
这是在iOS游乐场中:
您可以看到它们都指向同一个文件夹!