简体   繁体   English

如何使用领域保存NSURL数组?

[英]how to use realm to save an array of NSURL?

Now, I have a realm model , including images, audios and others. 现在,我有了一个领域模型,包括图像,音频和其他。 So I have to get the URLs of the media files and give the model value of the URLs. 因此,我必须获取媒体文件的URL,并提供URL的模型值。 But I meet a problem that I cannot save them into the realm. 但是我遇到一个问题,我无法将它们保存到领域中。 Because I can not save NSArray , also when I use the RLMArray to contain them ,it also don't be solved. 因为我无法保存NSArray,所以当我使用RLMArray包含它们时,也无法解决。 So how can I give my model the URLs? 那么如何为模型提供URL?

Its not possilbe to directly add the NSURL type in Realm but you can use workaround on your own like :- 它不是可以直接在Realm中添加NSURL类型的方法,但是您可以自己使用替代方法,例如:

@interface MyModel : JOBIBaseModel

@property (nonatomic, strong) NSString * urlString;

-(NSURL *)getUrl;

@end

@implementation MyModel

-(NSURL *)getUrl{
    return [NSURL URLWithString:self.urlString];
}

@end

Then access it from your object like:- 然后从您的对象访问它,例如:

NSURL *url=Obj.getUrl;

Update for array 更新数组

RLM_ARRAY_TYPE(MyArrayModel);
@interface MyArrayModel : JOBIBaseModel
@property (nonatomic, strong) NSString * urlString;
-(NSURL *)getUrl;
@end

@implementation MyArrayModel
-(NSURL *)getUrl{
    return [NSURL URLWithString:self.urlString];
}
@end

@interface MyModel : JOBIBaseModel
@property (nonatomic, strong) RLMArray <MyArrayModel> * strings;
@end

@implementation MyModel

@end

And use it like:- 并像这样使用它:

NSURL *url=Obj.strings[0].getUrl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM