Dynamically populate pathForResource - ios

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!

Related

How to read .m file as NSString in 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..

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.

Read a plist from website

I am incorporating an image gallery into my project. It currently reads from a local plist file, and I need to change it to read from a web server. I've read a number of ways to do this, but can't make it work in my context. This seems maddeningly simple.
Below is my code:
NSString *plistPath =
[[NSBundle mainBundle] pathForResource:#"Images" ofType:#"plist"];
NSArray *imagePaths = [NSArray arrayWithContentsOfFile:plistPath];
You can view my plist here
This is what I've tried to no avail:
//get image URLs
NSURL *plistPath = [NSURL URLWithString:#"http://kpd.altervista.org/Images.plist"];
NSDictionary *imagePaths = [[NSDictionary alloc] initWithContentsOfURL:plistPath];
I'd greatly appreciate anyone's help.
Try this and it should be work :
NSURL *plistPath = [NSURL URLWithString:#"http://kpd.altervista.org/Images.plist"];
NSArray *imagePaths = [NSArray arrayWithContentsOfURL:plistPath];
NSLog(#"%#", imagePaths);
The URL you provide serves a plist that contains an array.
Your code tries to read it as a dictionary.
Just change your code to read it as an array and you'll be one step closer.
But as others have said in the comments, you can't download a file (plist or otherwise) to your bundles directory. You have to download it to your Documents directory or similar.

Get and update plist value

I created a plist titled "appData.plist" in my supporting files section of Xcode. I have a key called "keyone" and am trying to get the value of it. I'm trying to log the value to console with NSLog but its not working.
How do you do this? I've searched google and there are a lot of way but none work for me.
NSString *myFile = [[NSBundle mainBundle] pathForResource: #"appData" ofType: #"plist"];
NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfFile:myFile];
NSString *viewCover = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"keyone"];
This is what i've tried but the whole thing ends in errors, I can't log it and I get an 'unusable variable' error.
The objectForInfoDictionaryKey method retrieves a value from your app's Info.plist file, which isn't what you want -- you want to retreive it from your dictionary myDict. You mentioned that the value is actually a boolean, so replace the last line with
BOOL viewCover = [[myDict objectForKey:#"keyOne"] boolValue];

NSSring from plist created from NSArray

I create a NSArray and write to a file: a.plist.
I use NSString: initWithContentsOfFile, and I can see the content in xml.
Then, I add a.plist to another project
and then I use NSString: initWithContentsOfFile to get the xml string.
filePath = [[NSBundle mainBundle] pathForResource:#"a" ofType:#"plist"];
NSString *plistStr = [[NSString alloc]initWithContentsOfFile:filePath];
However, it failed to recreate the xml string.
I user NSArray to test:
NSArray *plist2Array = [[NSArray alloc]initWithContentsOfFile:filePath];
But it successfully.
I think it may result from "Text Encoding" when I add it to another project.
The problem is I tried UTF8,UTF16 and so on.
I still can't find solution.
Hope for your help,thanks!
I find it!
rename the file: a.plist ->a.txt(or any else),
then in another project you can get the xml string.

Resources