Re: Where's examples about handling file?
Re: Where's examples about handling file?
- Subject: Re: Where's examples about handling file?
- From: Vince DeMarco <email@hidden>
- Date: Sat, 9 Jun 2001 20:03:32 -0700
On Saturday, June 9, 2001, at 06:39 PM, Youngjin Kim wrote:
I think what's missing in examples in dev CD is about file handling.
What I need to do is 1) putting my resource file(in hex data) into my
application bundle then 2) retrieve it at runtime. Sounds simple.
After days of juggling with NSData and NSBundle classes, I found I
really need some (simple) example.
Could anyone show me direction?
Okay lets say your are looking for a file called "myfile.txt" and its in
the applications Resources directory.
so put this code in some class in your app
NSString *path
NSData *fileContents;
path = [[NSBundle bundleForClass:[self class]] pathForResource:@"myfile"
ofType:@"txt];
fileContents = [NSData dataWithContentsOfFile:path];
Be sure to retain fileContents if you need it past the method that the
above code is in.
You can also replace [NSBundle bundleForClass:[self class]] with
[NSBundle mainBundle]
the first form will try to find what bundle the current class is defined
in and then search there. That way if you move the above code into a
framework it will still work.
Hope this helps
vince