在Core Data中将NSURL存储为字符串 [英] Storing an NSURL as a string in Core Data

查看:164
本文介绍了在Core Data中将NSURL存储为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始玩Core Data,只是有一个关于存储类似网址的问题。

I'm beginning to play around with Core Data and just have a question around storing something like a URL.

我目前已将其配置为可转换这是工作正常,但我觉得它可能存储过多的数据不必要的,因为它可以很容易地用文本表示我想知道是否应该将绝对URL存储为字符串和实例化NSURL对象加载时(我怀疑

I've currently got it configured as a transformable which is working fine however I feel that it's probably storing excess data unnecessarily and since it can be easily represented with text I'm wondering whether I should just be storing the absolute URL as a string and instantiating an NSURL object when it's loaded (I suspect this is the case) and if so how should this be implemented?

我的研究到目前为止,让我想到也许使用mogenerator,然后只是重写属性访问器来处理在字符串和NSURL之间的转换。

My research so far leads me to think maybe use mogenerator and then just override the property accessors to deal with the conversion between the string and NSURL. Would just like to get a sense of how this or normally done or whether I am just making things difficult for myself by even bothering.

干杯
Nick

推荐答案

根据Monogenerator不同,我将使用第二种方法。覆盖getters和setters,使用

Depending on Monogenerator or not, I will use the second approach. Override "getters" and "setters", perform the mapping from the URL to the string with

NSString *urlString = [url absoluteString];

并使用 setPrimitiveValue:forKey:对应的像

- (void)setYourURL:(NSURL*)newURL {
    [self willChangeValueForKey:@"yourURL"]; // call to notify the world you are modifying that property
    NSString* yourURLAsString = // perform the conversion here...
    [self setPrimitiveValue:yourURLAsString forKey:@"yourURL"];
    [self didChangeValueForKey:@"yourURL"]; // call to notify the world you've modified that property
}

有空格,您也可以考虑用 stringByReplacingPercentEscapesUsingEncoding:替换它们。

If your URL has spaces, you may also think to replace them with stringByReplacingPercentEscapesUsingEncoding:.

这篇关于在Core Data中将NSURL存储为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆