【问题标题】:How can I make folders in my custom directory and create create a folder in /myapp/documents in advance(when I build the app)?如何在我的自定义目录中创建文件夹并提前在 /myapp/documents 中创建一个文件夹(当我构建应用程序时)?
【发布时间】:2021-01-18 20:20:27
【问题描述】:

我正在为 iOS 和 ipadOS 制作一个词汇应用程序。结构是

文件夹(雅思、TOPFL 等)-> 词汇(剑桥雅思、牛津雅思等)-> 单词(玩耍、睡觉等)

Vocabularies 文件将是 .txt 文件,所以我可以把它改成这样

["word1" : "meaning1"], ["word1", "meaning2"], ....

我想在用户安装应用程序时制作文件夹文件。

所以这将是我的应用程序/文档/文件夹

但是当我安装应用程序时,它变成了我的应用程序/文档/,没有文件夹。

在 iOS 上有类似安装脚本的东西吗?

第二个问题是我什么时候创建文件夹

let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let docURL = URL(string: documentsDirectory)!
let dataPath = docURL.appendingPathComponent(String(AddNewFolder.text!)) // AddNewFolder is a Text Field
if !FileManager.default.fileExists(atPath: dataPath.absoluteString) {
    do {
        try FileManager.default.createDirectory(atPath: dataPath.absoluteString, withIntermediateDirectories: true, attributes: nil)
    } catch {
        print(error.localizedDescription);
    }
}

如何在 myapp/documents/folders 而不是 /myapp/documents 中创建新文件夹?

第三个问题是关于 UIKit 和 SwiftUI。什么更快,什么更容易使用?

而在 SwiftUI 中,有 SwiftUI App 和 UIKit 委托,什么是 UIKit 委托?(是不是说我在 SwiftUI 中使用了 UIKit?)

【问题讨论】:

  • 使用代码创建文件夹的一个选项。启动应用时检查该文件夹是否存在,否则创建它。
  • 如何查看整个目录?文件只显示 icloud 和用户目录,不显示应用程序,/etc、/usr、/var 等。是否有应用程序可以导航整个文件系统?

标签: ios swift xcode storyboard


【解决方案1】:

我用谷歌搜索了一整周。答案是

// make subdirectory "vocabFolder" if it doesn't exist
let VocabFolderPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("vocabFolder")

if !FileManager.default.fileExists(atPath: VocabFolderPath.absoluteString) {
    try! FileManager.default.createDirectory(at: VocabFolderPath, withIntermediateDirectories: true, attributes: nil)
}
    
// create custom vocab folder
let CustomVocabFolderPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("vocabFolder/\(String(AddNewFolder.text!))")
    
if !FileManager.default.fileExists(atPath: CustomVocabFolderPath.absoluteString) {
    try! FileManager.default.createDirectory(at: CustomVocabFolderPath, withIntermediateDirectories: true, attributes: nil)
}

要导航 Simualtor 设备,请执行 open 'xcrun simctl get_app_container booted BUNDLE_IDENTIFIER data' -a Finder

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    相关资源
    最近更新 更多