gpt4 book ai didi

ios - 在 Realm 中存储 "unsigned long long"数字的最佳实践解决方案

转载 作者:行者123 更新时间:2023-11-29 11:49:33 29 4
gpt4 key购买 nike

我必须在 Realm 存储中存储大量数字,例如 14000822124935161134。目前我通过将它们的类型更改为 string 来存储它们,如下所示,然后保存:

    NSMutableDictionary *itemInsert = [item mutableCopy];

if([item valueForKey:@"timestamp"]) {
unsigned long long timestamp = [[item valueForKey:@"timestamp"] unsignedLongLongValue];
[itemInsert setObject:[NSString stringWithFormat:@"%llu", timestamp] forKey:@"timestamp"];
}

RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[RMember createOrUpdateInRealm:realm withValue:itemInsert];
[realm commitWriteTransaction];

我的 RLMObjecttimestamp 属性定义如下:

@interface RMember : RLMObject
...
@property (nullable) NSString *timestamp;
...
@end

对于 Realm 中的这种类型的数据,是否有任何合适的类型而不是 string 或任何更好的解决方案?

最佳答案

Realm 支持long long;它只是不支持 unsigned 变体。

您可以简单地将值保存为 long long 并提供一个 getter 访问器,在从数据库中检索时将其显式转换回 unsigned long long

@interface RMember : RLMObject
@property long long timestamp;
@end

unsigned long long timestamp = [[item valueForKey:@"timestamp"] unsignedLongLongValue];

RLMRealm *realm = [RLMRealm defaultRealm];
RMember *myObject = ...;
[realm transactionWithBlock:^{
myObject.timestamp = (long long)timestamp;
}];

unsigned long long savedTimestamp = (unsigned long long)myObject.timestamp;
NSLog(@"Saved timestamp is %llu", savedTimestamp);

在我的 iPad Air 上测试,它似乎按预期工作:

enter image description here

关于ios - 在 Realm 中存储 "unsigned long long"数字的最佳实践解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41763199/

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