hello all
I am new to objective c and i want to insert data dynamically/programmatically into plist.please help me.Here is my plist structure
root
|_Client1
|_report1
|_application1
|_application2
|_report2
|_application3
|_Client2
|_report1
Now i want to add and retrieve data dynamically to application1,application2 in my plist
please help me
Reading content of .plist file ..
NSString* plistPath = [[NSBundle mainBundle] pathForResource:#"league" ofType:#"plist"];
contentArray = [NSArray arrayWithContentsOfFile:plistPath];
Writing to .plist file.
NSMutableDictionary * myDictionary;
NSString* plistPath = [[NSBundle mainBundle] pathForResource:#"league" ofType:#"plist"];
[myDictionary writeToFile:plistPath atomically:YES];
Note that:
Dictionary must contain plist objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary). And dictionary keys must be NSString objects
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'm working on an iPhone app and I need to add a new key-value pair to an existing plist file using objective-c. This is what I've tried so far:
NSString *myFile=[[NSBundle mainBundle] pathForResource:#"Favourites" ofType:#"plist"];
dict = [[NSMutableDictionary alloc] initWithContentsOfFile:myFile];
[dict setObject:textContent forKey:keyName];
[dict writeToFile:myFile atomically:YES];
However, when I do this it doesn't write it to the file. I've only seen resources based on either changing the value of a key or adding to another key. Is there another way to accomplish this?
Thank you for your time
You cannot make any changes in the bundle. So what you have to do is copy the .plist file into your documents directory and do the manipulation there.
Something along these lines:
//check for plist
//Get documents directory's location
NSArray*docDir=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString*filePath=[docDir objectAtIndex:0];
NSString*plistPath=[filePath stringByAppendingPathComponent:#"Favourites.plist"];
//Check plist's existance using FileManager
NSError*err=nil;
NSFileManager*fManager=[NSFileManager defaultManager];
if(![fManager fileExistsAtPath:plistPath])
{
//file doesn't exist, copy file from bundle to documents directory
NSString*bundlePath=[[NSBundle mainBundle] pathForResource:#"Favourites" ofType:#"plist"];
[fManager copyItemAtPath:bundlePath toPath:plistPath error:&err];
}
//Get the dictionary from the plist's path
NSMutableDictionary*plistDict=[[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
//Manipulate the dictionary
[plistDict setObject:textContent forKey:keyName];
//Again save in doc directory.
[plistDict writeToFile:myFile atomically:YES];
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 have been following this Tutorial on how to read from a plist. However i cannot read the string with key name. i tried putting NSLog("%#", [temp objectForKey:#"Name"]); but it returns null. i had a similar issue before which involved opening the xml file and manually changing dict to array. How can i fix this?
the hierarchy in the xml file is what confuses me. the dictionary i created named root with string John Doe is part of a dictionary itself? Clarification appreciated!
From the tutorial linked above:
// check to see if Data.plist exists in documents
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
// if not in documents, get property list from main bundle
plistPath = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
}
// read property list into memory as an NSData object
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
// convert static property liost into dictionary object
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
if (!temp)
{
NSLog(#"Error reading plist: %#, format: %d", errorDesc, format);
}
NSLog(#"%#", [temp objectForKey:#"Name"];
Most likely this is because there is no object with key #"Name" (remember that Xcode is case sensitive!) in the (presumably) NSDictionary named temp.
For a more specific answer, you'll need to post the code where you define temp.
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];