Storing NSURL as a string in master data

I start playing with Core Data and just ask a question about storing something like a URL.

I am currently set up as convertible, which works fine, but I feel that it is probably without extra extra extra extra data, and since it can be easily represented by text, I am wondering if I just need to store the absolute URL as a string and create an instance of an NSURL object when it is loaded (I suspect it is), and if so, how to implement it?

My research still makes me think, maybe use a mogenerator, and then just override property accessors to handle the conversion between string and NSURL. It would be easy to get an idea of ​​how this is either usually done, or I just make it difficult for myself even to worry.

Cheers Nick

+5
source share
3 answers

Depending on the Monogenerator or not, I will use the second approach. Override "getters" and "seters", match from URL to string using

NSString *urlString = [url absoluteString];

and use setPrimitiveValue:forKey:its analogue, for example

- (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
}

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

+4
source

, URL-.

file URL-

NSURL . .

:

  • -absoluteString
  • CFURLGetBytes()
  • , NSCoder

, , , URL-, -baseURL, .

+9

In Xcode 9 and iOS 11+, you can simply use the new type of URI for storage URL.

+1
source

All Articles