PDA

View Full Version : How to load the content from the file in resource to NSString?




xingworld
Jan 27, 2011, 08:25 AM
I store an article in resource as file, I want to load it to a NSString.
How should I do?
Thank you.



j-a-x
Jan 27, 2011, 09:11 AM
This is from a Mac App I wrote but it should be similar.

This is for a resource in the preferences folder on my mac, so you'd have to change the path to be your resource instead, but this should give you a start. Also the file I am loading is a Plist and all of the key object combinations are loaded into a dictionary.

NSString *path;
path = @"~/Library/Preferences/file.plist";
NSString *absolutePath = [path stringByExpandingTildeInPath];
NSDictionary *contentDictionary = [[NSDictionary alloc] initWithContentsOfFile:absolutePath];

In your case, the path would be to a local resource and I guess you wouldn't have to expand the tilde for an iOS app, and you'd have to convert the dictionary to a string (which is easy). In your case maybe the plist could contain a key for the title of the article, and a key for the body of the article, etc.

Sorry this isn't exactly the same but I thought it might help...

xingworld
Jan 27, 2011, 09:24 AM
This is from a Mac App I wrote but it should be similar.

This is for a resource in the preferences folder on my mac, so you'd have to change the path to be your resource instead, but this should give you a start. Also the file I am loading is a Plist and all of the key object combinations are loaded into a dictionary.

NSString *path;
path = @"~/Library/Preferences/file.plist";
NSString *absolutePath = [path stringByExpandingTildeInPath];
NSDictionary *contentDictionary = [[NSDictionary alloc] initWithContentsOfFile:absolutePath];

In your case, the path would be to a local resource and I guess you wouldn't have to expand the tilde for an iOS app, and you'd have to convert the dictionary to a string (which is easy). In your case maybe the plist could contain a key for the title of the article, and a key for the body of the article, etc.

Sorry this isn't exactly the same but I thought it might help...

Thank you very for you reply.
In code which gives from you I see the way to continue.