【问题标题】:Realm Error: Property requires a protocol defining the contained type领域错误:属性需要定义包含类型的协议
【发布时间】:2016-04-25 21:50:54
【问题描述】:

我有以下型号,我正在使用Realm

@interface GUIRoutineModel : GUIModel # GUIModel is a subclass of RLMObject

@property (nonatomic, retain) NSString *dateCreated;
@property (nonatomic, retain) NSString *dateModified;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *type;

@property NSInteger userId;
@property int routineId; #also have same issue using NSInteger


@end

当我打电话时:

   // Persist to Realm DB
    RLMRealm *realm = [RLMRealm defaultRealm];
    [realm transactionWithBlock:^{
        [realm addObject:routineModel];
    }];

我收到以下错误:

'Property 'routineId' requires a protocol defining the contained type - example: NSNumber<RLMInt>.'

我尝试将routineId 属性更改为NSNumber&lt;RLMint&gt;,但这也不起作用。谁能告诉我我做错了什么?

更新:

这是我尝试过的模型的另一个版本:

@interface GUIRoutineModel : GUIModel

@property (nonatomic, retain) NSString *dateCreated;
@property (nonatomic, retain) NSString *dateModified;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *type;

@property NSInteger userId;
@property NSNumber<RLMInt> *routineId;

@end

【问题讨论】:

  • 你试过这个:@property NSNumber *routineId;? NSNumber 是一个对象指针,所以你需要 c 风格的 * 对象解引用。
  • @fullofsquirrels 是的,我试过了。
  • 再仔细看一下,我认为您不能在objective-c 中的NSNumber 上使用像&lt;RLMint&gt; 这样的泛型类型注释;仅支持 NSArrayNSDictionary 之类的集合。您可以尝试将您的属性直接声明为无类型的NSNumber:@property NSNumber *routineId;
  • Property requires a protocol defining the contained type 错误仅由 Realm 为 NSNumber 类型的属性生成,而没有协议注释预期的具体类型。这意味着它不能为您提到的任何一个模型类生成。您是否检查过您在其他地方是否有另一个可能触发此错误的 routineId 属性?
  • @bdash 你是对的。我觉得自己好傻。我在另一个对象上引用了该属性。如果你想把它放在答案中,我会接受。感谢您的帮助!

标签: ios objective-c realm


【解决方案1】:

Property requires a protocol defining the contained type error 仅由 Realm 为 NSNumber 类型的属性生成,而没有协议注释预期的具体类型。这意味着它不能为您提到的任何一个模型类生成。您的应用中的其他位置可能有另一个 routineId 属性触发了错误。

【讨论】:

  • 这是不正确的。我检查了应用程序是否在应用程序的其他地方具有相同的属性或其他属性。我没有其他财产,但我仍然面临错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-22
  • 1970-01-01
  • 1970-01-01
  • 2018-05-21
  • 2021-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多