I want to populate one.plist files from a number of .plist files as per the user selection .
How to populate UIPickerView with a .plist file ?
I don't understand your question well but here is what I guess. You may want to load data from a .plist file and then fill in the UIPickerView.
Here is a sample code for loading a .plist file:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"money.plist"];
NSDictionary *plistDictionary = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
Related
I am building an app that has a pickerview, thus, I had created a .plist file to make the keys and values. The only issue is I want to be able to access the .plist values. The thing is, the values are name strings which when selected on the picker view i would like to create an if statement where i call a mobile number. I need guide on access the values if that is possible
Any idea on how to carry this out?
NSString *pathToFile = [[NSBundle mainBundle] pathForResource:#"myList" ofType:#"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:pathToFile];
NSString *path = [[NSBundle mainBundle] pathForResource: // here data is file nale of plist
#"data" ofType:#"plist"];
// Build the dictionary from the plist
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:path];
I have problem with adding and removing items in an array inside a plist file in XCODE.
I'm able to read the array by following code:
// Path to the plist (in the application bundle) ------>>>>>
NSString *path = [[NSBundle mainBundle] pathForResource:
#"Fav" ofType:#"plist"];
// Build the array from the plist ------>>>
NSDictionary *favs = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
Resepies = [favs objectForKey:#"Root"];
And this my plist structure
The senario is to let the user add and remove specific item from the array at a time.
try this code -
NSString *path = [[NSBundle mainBundle] pathForResource: #"Fav" ofType:#"plist"];
// Build the array from the plist ------>>>
NSDictionary *favs = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
Resepies = [favs objectForKey:#"Root"];
[Resepies addObject:addYourObjectHere];
//then add this array into dictonary ;
[favs setObject:Resepies forKey:#"Root"];
// now write dictionary to plist
[favs writeToFile:yourPlistName atomically:YES];
You can't modify files in the application bundle, which is what your code above is attempting to do.
If the file is supposed to be modifiable, you need to move it into the documents folder first (say, on first run) and then read / write to that one subsequently. There are plenty of questions dealing with how to do this, for example : create plist and copying plist to document directory
I got annoying question...
What if I have many image , and I want load it in to a table view
And show the file name as cell's text , and the preview image is also show in the cell.
When I select the cell , it will push to next view , show the big size image.
That's it .
I don't know how to load many folder to an array?
/*********** EDIT ***********/
This is the folder I set many images inside
You can see that's only one root folder ...
And this is my code to load the image inside
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Image"ofType:#""];
NSDirectoryEnumerator *direnum;
direnum = [fileManager enumeratorAtPath: filePath];
imageFolder = [NSMutableArray new];
for(NSString *filename in direnum){
if([[filename pathExtension] isEqualToString:#"png"]){
[imageFolder addObject:filename];
}
}
NSLog(#"Files in the folder %#",imageFolder);
I got result like this :
Files in the folder (
"macro1.png",
"macro10.png",
"macro11.png",
"macro12.png",
"macro13.png",
"macro14.png",
"macro15.png",
"macro16.png",
"macro17.png",
"macro18.png",
"macro19.png",
"macro2.png",
"macro20.png",
"macro21.png",
"macro22.png",
"macro23.png",
"macro24.png",
"macro25.png",
"macro26.png",
"macro27.png",
"macro4.png",
"macro5.png",
"macro6.png",
"macro7.png",
"macro8.png",
"macro9.png"
)
But what if I change the root folder like this
How to read the image files in the subfolders?
The App won't have issues finding files inside the bundle. The structure of your app development folders is irrelevant to the end product, for the most part. If you are storing the images in the apps bundle, the system can find it.
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"imageName" ofType:#"png"]];
When I've had to do this as a quicky solution, I created an NSDictionary entry for each photo and stored that inside of an array in userPrefs. I programmatically created thumbnails for each image to utilize in the cell.imageView.image property, and then used an NSDictionary with #"description", #"imageName", and #"imageNameThumbnail" as the keys. You could do the same thing with an NSArray and just call the objectAtIndex, but I prefer the plain text friendliness of dictionaries.
You can try testing the path information that is being returned. Here are a couple of lines to try:
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"imageName" ofType:#"png"];
NSString *path2 = [[NSBundle mainBundle] pathForResource:#"imageName" ofType:#"png" inDirectory:#"folderName"];
NSLog(#"Path 1: %#",path1);
NSLog(#"Path 2: %#",path2);
See what the output for these lines is, or if they return nothing.
Documents Directory as filePath
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
Then, enumerate the documentsDirectoryPath, and it should read subfolders recursively.
If it is local data you are using, you probably want to put the data in a .plist file. You would just be putting the names of the files in there, and then load the plist into an NSDictionary which you will only have one key for called "images" or something like that. You can then load all the objects under the "images" key into an array and use that array to populate your table.
Hope this makes sense.
in my resources directory i have a file called lsf.plist
i want to load this file (dictionary) but i always get null as content of the file. i am using the following code. i've verified that the file is in the app after the build.
self.path = [[NSBundle mainBundle] pathForResource:#"lsf" ofType:#"plist"];
NSLog(self.path);
self.lsf = [NSMutableArray arrayWithContentsOfFile:path];
NSLog(#"%#",lsf);
The first log-output shows the path and the second one gives me null....
it would be great if you can help me to solve this issue!
Br,
martin
Is root of your plist array or dictionary?
Also you dont need to use self.
Heres an example from a project of mine:
NSString* path = [[NSBundle mainBundle] pathForResource:#"property" ofType:#"plist"];
NSDictionary *newDict = [NSDictionary dictionaryWithContentsOfFile:path];
In my application one is mainviewcontroller which is subclass of uiTableviewcontroller holds data from plist when clicked on a particular cell its displays detailviewcontroller(dvc) which is subclass of uiviewcontroller. dvc holds textfield & button .actually when i clicked on button i write textfield's text on tableviewcell through plist but it is actually write data temporary but i want to write data permanently on tableview what can i do for that .its a navigation based application?
Try this:
Loading the plist to dictionary:
//Get the path to documents directory
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:#"listData.plist"];
//if the file does not exist in documents directory, then load from the bundle.
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:#"listData" ofType:#"plist"];
}
//load plist into NSMutableDictionary.
nes.plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
Saving dictionary to plist:
//Get the path to documents directory
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:#"listData.plist"];
//Write to documents directory! NOT bundle!!
[nes.plistDict writeToFile:plistPath atomically: YES];
Also use:
[nes.plistDict setObject:txtName.text forKey:#"col1"];
Not:
[nes.plistDict setValue:txtName.text forKey:#"col1"];
If this does not help can I see youre cellForRowAtIndexPath in mainviewcontroller?
could you show us some code?
What is the code for the action button? How do you reload the table view?
I'm guessing you are reading a plist into a NSDictionary? Then loading the tableviews cell.text from the values in the dictionary? When you push the button do you write the textfield text to the NSDIctionary? Do you remember to write the NSDictionary back to the plist?
Okay.
How do you load youre tableview cells?
Are you sure the value for col1 is of NSString type?
You should probably use setObject: forKey: instead of SetValue: forKey:..
Does the tableview not update at all or is it just that it is not saved after you quit?
Are you testing on simulator or device?
The application bundle is a readonly directory on device but not in simulator. you should use:
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) objectAtIndex:0];
NSString *plistPath = [rootPath
stringByAppendingPathComponent:#"listData.plist"];