How to read .m file as NSString in iOS - ios

I am trying to display an implementation file(Code inside the .m file) in UITextView. Below is the code snippet i used. But it returns nil.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"TextCheck.m" ofType:#"m"];

NSString *filePath = [[NSBundle mainBundle] pathForResource:#"TextCheck.m" ofType:#"m"];
Replace the code as below line :
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"TextCheck" ofType:#"m"];

I really doubt you can do this. The source code is not a 'resource'..When you execute the app they are in binary form.. there is no code in there..
If you need to do display something that you know already - try storing the text/code you intend to display in a plain file and display..

Related

Can I get markdown file's contents in Objective-C?

I can get the contents of a .txt file in this way:
NSString *articlePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#".txt"];
return [NSString stringWithContentsOfFile:articlePath encoding:NSUTF8StringEncoding error:nil];
However,it is no use when the file is a markdown file:
NSString *articlePath = [[NSBundle mainBundle] pathForResource:#"test2" ofType:#".md"];
return [NSString stringWithContentsOfFile:articlePath encoding:NSUTF8StringEncoding error:nil];
The articlePath is "nil",but the file is exist actually.
I wonder if there any method I can use to get the contents of a .md file.
Thanks in advance.
Go to : Target -> "Build Phases" -> "copy bundle Resources" Then add that particular file here.
Make sure that test2 file is there

How can I get my own bundle's infomation?

I want to get some information of "myBundle.bundle" such as version,create time whitch identify the bundle. Thest information may wirite by myself localed in myBundle.bundle.
Are there any recommend method for doing this ?
I try like this, but not success:
I put a plist named Info.plist into the root of myBundle.bundle.
read the info like below,but the info is nil:
self.bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:#"myBundle" ofType:#"bundle"]];
NSDictionary * info = [self.bundle infoDictionary];
You can Use the following line of code to get Bundle Version
NSString * bundleVersionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleVersion"];
NSDictionary *infoDictionary = [[NSBundle mainBundle]infoDictionary];
NSString build = infoDictionary[(NSString)kCFBundleVersionKey];
This will give all the info. that is stored in plist like version,bundle version, display name, etc.

NSBundle pathForResource: returns nil

I am attempting to access a .txt file from my supporting files folder in Xcode on iOS using the following piece of code:
NSString* filePath = #"filename.txt";
NSLog(#"File path: %#", filePath);
NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:#"txt"];
NSLog(#"File root: %#", fileRoot);
The first NSLog prints just what I expect it to print, but the last NSLog always simply prints
File root: (null)
Accessing text from the file after (attempting to) reading it into memory also simply gives me a (null) printout.
The solution for this problem is probably right under my nose, I just can't seem to find it. Any help is very appreciated! Thanks in advance.
filePath already contains the suffix ".txt", therefore you must not specify it
again when locating the resource. Either
NSString* filePath = #"filename.txt";
NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:nil];
or
NSString* filePath = #"filename";
NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:#"txt"];

Ios file handling and location access

I am having an ios app that reads contents of a csv file and produces output.When i run the program on a simulator ,i can directly specify the file path(/Users/abc/file.csv).Where to keep these files and access them in a n iphone.We cant specify the static path here?
Just drag & drop them to your project, and access them like this:
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *myFile = [mainBundle pathForResource:#"myFile" ofType:#"csv"]
If you place the file in your application directory, you can access it by:
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:#"yourfilename" ofType:#"csv"];
For reading static files, use the other people's answers.
If you need to write out a file, you need to access the Documents directory.
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"file.csv"];
And then use NSFileManager for writing the file. Classes such as NSDictionary have helper functions for writing their values to a file, too.

Dynamically populate pathForResource

I'm having serious brain issues today. I am passing data into a ViewController from a UITableView. Basically what I want to achieve is dynamically populate the pathForResource#"dynamicallypopulated" with what ever is coming from the UITableView (it will then go on to get the txt file associated). When I step through the code the value is NULL even though it's set just above it and I can output it.
The code:
NSString *location = self.animalTitle;
NSLog(#"path : %#",animalTitle);
NSLog(#"path : %#",location);
//initWithFormat:#"Your favorite color is %#", favoriteColorTextField.text
NSString *path = [[NSBundle mainBundle] pathForResource:[location to be dynamic!!! what should I put here?] ofType:#"txt"];
Thanks in advance!
Jeremy
Ug.
I worked it out. There was nothing wrong with the code. It had to do with the file I was bringing in which provided the content was cached and the project needed a good clean...Ironic really as that is what I do first. Nevertheless, below is the code completed and working.
NSString *path = [[NSBundle mainBundle] pathForResource:animalTitle ofType:#"txt"];
Brian fart resolved.
Thanks!

Resources