如何更新 Realm Objective-C 中的特定记录

How to update specific record in Realm Objective-C

我想更新 Realm Objective-C 中的特定记录。目前,我还没有找到任何提到更新特定记录的代码。请帮我看看怎么做。

下面我的代码是在Realm中添加记录。但是我想更新关于 cpID.

的记录
RLMCoachPadItem *RMLCoachPad = [[RLMCoachPadItem alloc]init];
RMLCoachPad.cpID = [NSString stringWithFormat:@"%d", results.count+1];
RMLCoachPad.cpDescription = _txtMessage.text;
RMLCoachPad.cpTagId = @"";


RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[realm addObject:RMLCoachPad];
[realm commitWriteTransaction];

您可以找到有关如何更新对象的文档 here

来自 documentation 的示例:

You can also partially update objects with primary keys by passing the subset of values you wish to update, along with the primary key:

// Assuming a "Book" with a primary key of `1` already exists.
[realm beginWriteTransaction];
[Book createOrUpdateInRealm:realm withValue:@{@"id": @1, @"price": @9000.0f}];
// the book's `title` property will remain unchanged.
[realm commitWriteTransaction];

假设 cpIDRLMCoachPadItem 的主键,并且您想更新描述 (cpDescription):

[realm beginWriteTransaction];
[RLMCoachPadItem createOrUpdateInRealm:realm withValue:@{@"cpID": @"idOfYourRMLCoachPad", @"cpDescription": @"new description"}];
[realm commitWriteTransaction];