Goal C: Reading Text Files

I have done this before, but now it does not work for me. I do:

NSString* path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"]; NSString* content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL]; NSLog(@"%@",path); 

and it returns (null) every time I have an NSLog path and content. Can anyone see what I'm doing wrong?

+63
ios text objective-c bundle
Feb 18 '11 at 16:27
source share
1 answer

the contents will be nil (which is registered as "(null)") if you pass it a path that it cannot open. Therefore, the only problem is that the corresponding instance of NSBundle cannot find test.txt in terms of the resources of your application package.

You should:

  • check that the file is in your Xcode project; and if so
  • make sure that it is included in the โ€œCopy Bundle resourcesโ€ phase under the target of your choice (in the project tree on the left in the normal layout of the Xcode window), and if so,
  • look inside the generated application package (find your product, right-click, select "Reveal in Finder", from the Finder, right-click on the application and select "Show package contents", and then find your file there) to make sure that it there.

If it is copied, but the corresponding instance of NSBundle cannot find it, then something very strange happens.

+49
Feb 18 2018-11-18T00:
source share



All Articles