swift 正在终止应用程序,原因:“containerIdentifier can not be nil”以CKException类型的未捕获异常终止

0mkxixxg  于 2023-10-15  发布在  Swift
关注(0)|答案(1)|浏览(138)

我是Swift的新手。我的第一个项目是使用CloudKit。但不幸的是,我从CloudKit获取数据时出错:
我得到这个错误:

*** Terminating app due to uncaught exception 'CKException', reason: 'containerIdentifier can not be nil'
terminating with uncaught exception of type CKException

代码如下:

import Foundation
import CloudKit

class CKRelation {
    static let database = CKContainer.default().publicCloudDatabase

    class func fetch(completion: @escaping (Result<[Relation], Error>) -> ()) {
        let predicate = NSPredicate(value: true)
        let name = NSSortDescriptor(key: "LastName", ascending: true)
        let query = CKQuery(recordType: "Clients", predicate: predicate)
        query.sortDescriptors = [name]

        let operation = CKQueryOperation(query: query)
        operation.desiredKeys = ["FirstName", "Initials", "LastName", "MiddleName", "RelationId"]
        operation.resultsLimit = 50

        var newRelations = [Relation]()

        operation.recordFetchedBlock = { record in
            var relation = Relation()
            relation.recordID = record.recordID
            relation.FirstName = record["FirstName"] as! String
            relation.Initials = record["Initials"] as! String
            relation.LastName = record["LastName"] as! String
            relation.MiddleName = record["MiddleName"] as! String
            relation.RelationId = record["RelationId"] as! Int
            
            newRelations.append(relation)
        }

        operation.queryCompletionBlock = { (cursor, error) in
            DispatchQueue.main.async {
                if let error = error {
                    completion(.failure(error))
                } else {
                    completion(.success(newRelations))
                    print("data")
                }
            }
        }
        database.add(operation)
    }

我在网上找不到任何东西。希望有人能帮我。

xqkwcwgp

xqkwcwgp1#

我也遇到了这个问题,因为我的项目没有为CloudKit建立数据库。按照苹果的演示,可以解决enter image description here的问题

相关问题