How to add enums to realm model? RLMObject?

守給你的承諾、 提交于 2019-12-11 05:15:32

问题


This is my serviceModel.h

typedef NS_ENUM(NSInteger, OKServiceType) {
    OKServiceTypePending = 0,
    OKServiceTypeAccepted ,
    OKServiceTypeStarted,
    OKServiceTypeCompleted,
    OKServiceTypeClosed,
    OKServiceTypeCancelled
};
@interface serviceModel : RLMObject
@property NSString *job_id;
@property NSString *job_service_id;
@property NSString *service_id;
@property NSString *vendor_id;
@property NSString *timeslot;
@property NSString *points;
@property OKServiceType *status;
@property NSString *service_name;
@property NSString *image_url;
@property NSString *vendor_name;
@property NSString *subservice_desc;
@property NSString *subservice_id;
@property NSString *display_datetime;
@property NSString *status_text;
@end

RLM_ARRAY_TYPE(serviceModel)

I know we can add the property type as These. But I cann;t find the proper way to add the enum to my models as realm collects all models on start only so it do crash due to this

@property OKServiceType *status;

I am having crash as

Error Error Domain=NSCocoaErrorDomain Code=3010 "remote notifications are not supported in the simulator"

UserInfo={NSLocalizedDescription=remote notifications are not supported in the simulator} 2017-06-12 15:20:41.049 AppName[9974:157085]

*** Terminating app due to uncaught exception 'RLMException', reason: 'Can't persist property 'status' with incompatible type. Add to ignoredPropertyNames: method to ignore.'

Any Help would be appreciated alot..


回答1:


Maybe you want to store your property as an NSInteger which is a supported type and create a method which returns your integer value as an enum of type OKServiceType.

@property NSInteger statusInt;

- (OKServiceType)status {
    return (OKServiceType)statusInt
}

- (void)setStatus(OKServiceType)status {
    self.statusInt = status
}



回答2:


Another alternative is to set the enum property as (readonly). Read-only properties are automatically ignored by Realm and you can then use a custom setter. For example:

@property (readonly) OKServiceType *status;
@property NSInteger statusInt;

@implementation ServiceModel

- (OKServiceType)status {
    return (OKServiceType)_statusInt;
}

@end


来源:https://stackoverflow.com/questions/44496642/how-to-add-enums-to-realm-model-rlmobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!