gpt4 book ai didi

ios - 如何在“RLMArray”中使用“removeAllObjects”

转载 作者:行者123 更新时间:2023-12-01 18:56:21 28 4
gpt4 key购买 nike

如何在removeAllObjects中使用RLMArray
我收到'RLMException', reason: 'Attempting to mutate a readOnly RLMArray'错误。

#import "ViewController.h"
#import <Realm/Realm.h>
#import "Person.h"

@interface ViewController ()
@property RLMArray *list;
@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
_list = [Person allObjects];
NSLog(@"%@", _list);

}

- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
}

- (IBAction)addPerson:(id)sender {
Person *human = [[Person alloc] init];
human.name = @"John";
human.title = @"Wizard";

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

}

- (IBAction)removePerson:(id)sender {
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[_list removeAllObjects];
[realm commitWriteTransaction];
}
@end

最佳答案

我不是100%积极,但我认为这应该有效。

- (IBAction)removePerson:(id)sender {
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[realm deleteObjects:_list];
[realm commitWriteTransaction];
}

关于ios - 如何在“RLMArray”中使用“removeAllObjects”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26535586/

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