让嵌套 CloudKit 查询中的警告使 var 永不改变

Let warning in nested CloudKit Query makes var never change

我在我的代码中收到警告“变量 'userArray' 从未发生变化;考虑更改为 'let' 常量”,而我的变量由于某种原因没有被更改?我包含了一些打印语句来尝试找出什么是 运行 什么不是。这是打印的内容:“用户附加 - []”,“找到 [“测试用户”]”

static func getBossSubs(bossID: String, completion: @escaping (Result<UserNames, Error>) ->
    ()) {
    let pred = NSPredicate(format: "uniqueID = %@", bossID)
    let sort = NSSortDescriptor(key: "creationDate", ascending: false)
    let query = CKQuery(recordType: RecordType.Users, predicate: pred)
    query.sortDescriptors = [sort]
    
    let operation = CKQueryOperation(query: query)
    operation.desiredKeys = ["subscribedBosses"]
    operation.resultsLimit = 50
    
    operation.recordFetchedBlock = { record in
        DispatchQueue.main.async {
            guard let subs = record["subscribedBosses"] as? [String] else {
                print("Error at screenName")
                
                completion(.failure(CloudKitHelperError.castFailure))
                return
            }
            var userArray = UserNames() //Error that it should be a LET is here. 
            for boss in subs{
                CloudKitHelper.getBossScreenName(bossID: boss) { (result) in
                    
                    switch result{
                        
                    case .success(let name):
                        userArray.names.append(name) //works fine
                        print("Found \(userArray.names)") //Prints a name
                    case .failure(let er):
                        completion(.failure(er))
                    }
                    
                }
                print("does this run?") // Only runs if No Name
                completion(.success(userArray)) // contains no name or doesn't run?
            }
            
        }
    }
    operation.queryCompletionBlock = { (_, err) in
        DispatchQueue.main.async {
            if let err = err {
                completion(.failure(err))
                return
            }
        }
        
    }
    CKContainer.default().publicCloudDatabase.add(operation)
}

我这样调用代码:

.onAppear {
            // MARK: - fetch from CloudKit
            self.userList.names = []
            let myUserID = UserDefaults.standard.string(forKey: self.signInWithAppleManager.userIdentifierKey)!
            // get my subs projects
            CloudKitHelper.getBossSubs(bossID: myUserID) { (results) in
                switch results{
                    
                case .success(let user):
                    print("Users appended")
                    self.userList.names = user.names
                case .failure(let er):
                    print(er.localizedDescription)
                }
            }
        }

如你所见,userArray 是一个对象:

var userArray = UserNames()

当您将内容附加到对象的 names 属性 时,您并不是在改变对象,而是在更改其属性之一的内容。如果你不改变对象(即改变它的属性),你应该使用 let.

如果像 Xcode 所建议的那样将其更改为 let,是否会出现任何问题?

let userArray = UserNames()