我希望能够在我的原生macOS应用程序中 Select Xcode项目,我使用.xcodeproj表示allowedContentTypes,但很明显Xcode或UniformTypeIdentifiers并不知道这一点!有没有办法让代码发挥作用?

import SwiftUI
import UniformTypeIdentifiers

struct ContentView: View {
    @State private var fileImporterIsPresented: Bool = false
    var body: some View {
        
        Button("Select your Xcode projext") { fileImporterIsPresented = true }
            .fileImporter(isPresented: $fileImporterIsPresented, allowedContentTypes: [.xcodeproj], allowsMultipleSelection: false, onCompletion: { result in
                
                switch result {
                case .success(let urls):
                    
                    if let unwrappedURL: URL = urls.first {
                        print(unwrappedURL.path)
                    }
                    
                case .failure(let error):
                    print("Error selecting file \(error.localizedDescription)")
                }
                
            })
        
    }
}

推荐答案

理论上,它应该适用于以下UTType种情况(即使Info.plist中没有其他配置)

UTType(tag: "xcodeproj", tagClass: .filenameExtension, conformingTo: .compositeContent)

但我想这并不是由于fileImporter中的一些错误,因为使用本机AppKit open panel可以正常工作

    Button("Open") {
        let panel = NSOpenPanel()
        panel.allowsMultipleSelection = false
        panel.allowedContentTypes = [xcodeproj]
        panel.canChooseDirectories = false
        if panel.runModal() == .OK {
            print(">> ", panel.urls)
        }
    }

Test module on GitHub

Swift相关问答推荐

JSON如何解码空对象

SWIFT Vapor控制台应用程序-操作无法完成.权限被拒绝

Variadic泛型与Swift中的值和类型参数包

如何为任务扩展的泛型静态函数获得自动类型推理

在`NavigationStack`中使用`SafeAreaInset`修饰符时出现SwiftUI异常行为

仅当单击UIButton时才调用计时器函数

MacOS 13-如何使用SwiftUI创建Message.App设置工具栏?

对实例方法appendInterpolation的调用没有完全匹配

Swift ui 转换无法按预期工作

使用View()和List()无法显示影子

暂停我的 AR 会话并清除变量和锚点的功能

我如何遵守与演员的协议?

如何在 UITableView 中点击图片和标题

在 Swift 中为(递归)扩展提供默认实例参数

响应 UITextField (UIViewRepresentable) 中的特定按键

在 Swift 5.5 中编写同步和异步函数

在 Swift 中计算两个日期之间的差异

我可以在 Swift 中为泛型类型 T 分配默认类型吗?

Swift中switch 盒的详尽条件

UISearchbar TextField 的高度可以修改吗?