首页 > 解决方案 > 使用公共 CloudKit 数据库 (NSPersistentCloudkitContainer) - macOS

问题描述

我在 SwiftUI 中编写了一个 iOS 应用程序(使用 Xcode 12 中的 SwiftUI 生命周期),它使用 CloudKit 容器的公共数据库和以下代码行:

 let container = NSPersistentCloudKitContainer(name: "my_ios_container_name")
    
 guard let description = container.persistentStoreDescriptions.first else {
     print("Can't set description")
     fatalError("Error")
 }
    
 description.cloudKitContainerOptions?.databaseScope = .public

我正在用 SwiftUI 编写一个访问同一个 CloudKit 容器的公共数据库的 macOS 应用程序。但是当我尝试在我的 mac 应用程序AppDelegate.swift文件中构建下面的代码时,Xcode 给我一个错误,说它无法找到“databaseScope”成员。此成员变量是否仅适用于 iOS 应用程序?如果是这种情况,您如何在 macOS 应用程序中访问 Cloudkit 容器的公共数据库?

在此处输入图像描述

标签: core-dataswiftuicloudkitxcode12

解决方案


尝试将“import CloudKit”添加到文件顶部。

为我解决了这个问题。


推荐阅读