gpt4 book ai didi

swift - 如何向 CloudKit 中的现有记录添加字段

转载 作者:行者123 更新时间:2023-11-28 06:13:29 24 4
gpt4 key购买 nike

我有一个功能可以将用户名输入到 CloudKit 中的新记录名称中(如下所示),但是我还需要将用户的“分数”添加到同一条记录中。

有没有办法通过引用其唯一的“recordName”值来插入另一个字段“usersScore”?

/// INSERT NEW ENTRY TO DB.
func insertName(nameEntry: String) -> CKRecordID {

// Connects to the user database table.
let userTable = CKRecord(recordType: "User")

// Connects the Database to the public container.
let publicData = CKContainer.default().publicCloudDatabase

// Adds the users entry into the "name" field.
userTable["name"] = nameEntry as NSString

// If the record has saved or an error has occurred.
publicData.save(userTable) { (record, error) in
if let saveError = error {
//Error.
print("An error occurred in \(saveError)")
}else {
// Success.
print("Saved Record!")
}
}
// RecordName (ID) of the current user.
recordNameID = userTable.recordID
return recordNameID!
}

最佳答案

要输入多条记录,最好使用CKModifyRecordsOperation。这是我如何做到的...

func insertItems(nameEntry: String)  {

// Creates a record name based on name passed into function.
let userNamesID = CKRecordID(recordName: "\(nameEntry)")

// Inserts name into the User table.
let userName = CKRecord.init(recordType: "User", recordID: userNamesID)

// Adds the users entry into the "name" field along with score and the amount of questions answered.
userName["Name"] = nameEntry as CKRecordValue
userName["Score"] = questionsCorr as CKRecordValue
userName["Questions_Answered"] = questionsAns as CKRecordValue

// Insert all records into the database.
publicDatabase.add(CKModifyRecordsOperation.init(recordsToSave: [userName], recordIDsToDelete: nil))

}

关于swift - 如何向 CloudKit 中的现有记录添加字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45800753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com