【问题标题】:Store string type array in RealM objective c在 RealM 目标 c 中存储字符串类型数组
【发布时间】:2017-09-25 17:14:37
【问题描述】:

我想在Realm objective c中存储字符串类型的数组。

Ex 数组:- 的 [ “58575bc922e87bd14480132f”, “58575c5c22e87bd144801331”, “58575cc922e87bd144801333”, “58575d5b22e87bd144801335”, “58575bc922e87bd14480132f”, “58575c5c22e87bd144801331”, “58575cc922e87bd144801333”, “58575d5b22e87bd144801335”, “58575bc922e87bd14480132f”, “58575c5c22e87bd144801331”, “58575cc922e87bd144801333”,“58575d5b22e87bd144801335 "]

【问题讨论】:

  • 请展示你的尝试!
  • 我不知道如何为这种类型的数组创建对象类。

标签: ios objective-c iphone xcode realm


【解决方案1】:

您可以从 RLMObject 类继承并将 NSString 作为属性放入您的 RLMObject。 然后,您可以再次创建新的 RLMObject,现在使用以前创建的 RLMObject 的 RLMArray。

@interface StringObject: RLMObject
@property NSString *stringValue;
@end

@interface RealmObject: RLMObject
@property RLMArray<StringObject> *realmArray
@end

完成此操作后,请随意使用它。 F.e.使用快速枚举循环将字符串放入领域 RLMArray。

NSArray *arrayOfStrings = @[@"58575bc922e87bd14480132f",@"58575c5c22e87bd144801331",@"58575cc922e87bd144801333",@"58575d5b22e87bd144801335",@"58575bc922e87bd14480132f",@"58575c5c22e87bd144801331",@"58575cc922e87bd144801333",@"58575d5b22e87bd144801335",@"58575bc922e87bd14480132f",@"58575c5c22e87bd144801331",@"58575cc922e87bd144801333",@"58575d5b22e87bd144801335"];

RLMRealm *realm = [RLMRealm defaultRealm];

RealmObject *realmObject = [RealmObject new];
for (NSString *value in arrayOfStrings) {
    StringObject *string = [StringObject new];
    string.stringValue = value;

    [realmObject.realmArray addObject:string];
}

[realm beginWriteTransaction];
[realm addObject:realmObject];
[realm commitWriteTransaction];

感谢RLMObject with Array of NSStrings
https://github.com/realm/realm-cocoa/issues/3415

【讨论】:

  • 不工作,StringObject 值总是包含:"key" -> "value",我不知道为什么这个推荐被标记为正确答案。
  • 嗯,好久不见了,我稍后再检查一下,然后回复你。
  • 你好,我找到了类似的问题,还在阅读:stackoverflow.com/questions/31724595/…
  • 谢谢,我会检查的。只是现在有点忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多