• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Where's examples about handling file?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Where's examples about handling file? (From: Youngjin Kim <email@hidden>)

  • Prev by Date: Re: Displaying jpeg images
  • Next by Date: Flaky URLAccessSample behavior?
  • Previous by thread: Where's examples about handling file?
  • Next by thread: Flaky URLAccessSample behavior?
  • Index(es):
    • Date
    • Thread