【发布时间】: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<RLMint>,但这也不起作用。谁能告诉我我做错了什么?
更新:
这是我尝试过的模型的另一个版本:
@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上使用像<RLMint>这样的泛型类型注释;仅支持NSArray和NSDictionary之类的集合。您可以尝试将您的属性直接声明为无类型的NSNumber:@property NSNumber *routineId;。 -
Property requires a protocol defining the contained type错误仅由 Realm 为NSNumber类型的属性生成,而没有协议注释预期的具体类型。这意味着它不能为您提到的任何一个模型类生成。您是否检查过您在其他地方是否有另一个可能触发此错误的routineId属性? -
@bdash 你是对的。我觉得自己好傻。我在另一个对象上引用了该属性。如果你想把它放在答案中,我会接受。感谢您的帮助!
标签: ios objective-c realm