开发者

Need to load data from Bundle into my iphone app

I'm looking for some guidance/code-snippets/tutorial on passing some simple data into my app. I have some data in a text file at the moment.

Name,Age,Weight Name,Age,Weight etc, etc,etc

There is not much. It will be read only since i wont need to change it.

How can i go about reading this into my app? What do you recommend? Nothing to difficult please, as I am still learning and advanced topics are still scary. Doesnt need to be fast or awesome etc. Just work and simple to understand.

Can iphone apps parse text files that are stored in the app bundle?

开发者_C百科

Many Thanks, Code


You can get a path to the file like this:

NSString* path = [[NSBundle mainBundle] pathForResource: @"data" ofType: @"txt"];

And then load it into a string like this:

NSString* data = [NSString stringWithContentsOfFile: path
    encoding: NSUTF8StringEncoding error: NULL];

You can then use the split functions of NSString to split the data in lines and then in individual items.

Give it a try, if it does not work, post some code here.


Or.. you can introduce your data in a plist file. You can create it in XCode directly.. and read it either like a dictionary or an array, depends on how you edited the plist file in the first place.

A plist is a key/value type of file (XML) so this helps a lot. Reading from a plist is very easy. Google that.

The best thing about this is that you retrieve your data very easily in the form you wanted in the first place.

Cheers

PS. Another idea to store large amounts of text is sqlite3..


Take a look at chcsvparser.


Swift:

    var data: NSData?
    if let path = NSBundle.mainBundle().pathForResource("MyFile", ofType: "txt") {
        data = NSData(contentsOfFile: path)
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜