I have an issue to generate plist file from NSMutableDictionnary. It seems to doesn't work. i have watched many example on Stackoverflow. I generate Dictionary from a plist but not plist from Dictionary.
Here is my code :
NSArray *keys = [NSArray arrayWithObjects:#"id", #"name", nil];
NSArray *object = [NSArray arrayWithObjects:#"Value id", #"Value name", nil];
NSMutableDictionary *projectData = [NSDictionary dictionaryWithObject:object forKey:keys];
NSFileManager* fileManager = [NSFileManager defaultManager];
for (id key in projectData)
{
NSLog(#"projectData = key : %#, value : %#", key, [projectData objectForKey:key]);
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:#"data.plist"];
NSLog(#"%#", plistPath);
//write
[projectData writeToFile:plistPath atomically:YES];
NSMutableDictionary *newDictionnary;
//read
if ((plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:#"data.plist"])) {
if ([fileManager isWritableFileAtPath:plistPath]) {
[projectData writeToFile:plistPath atomically:YES];
newDictionnary = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
//NSLog(#"%#", plistPath);
}
}
else {
NSLog(#"File not found");
}
for (id key in newDictionnary)
{
NSLog(#"New dictionnary = key : %#, value : %#", key, [newDictionnary objectForKey:key]);
}
Thanks for help :)
One note: your paths variable is never used.
You cannot write to your bundle directory directly. You have to write either to the documents folder, the caches folder or the temp folder.
Replace [[NSBundle mainBundle] bundlePath] with paths[0].
Related
I am creating a iPhone app in which i get all countries name, logo & player name. I want to save that data in .plist instead of sqlite server. I don't know how to create a plist file in DocumentDirectory and save the data.
Please somebody suggest me how to save data in plist file.
I am going through with screenshot and step by step. Please follow this and you will get your answer.
First you have to create Property List through your Xcode.
Step:1
Step:2
Step:3
Save data on your save button action :
// Take 3 array for save the data .....
-(IBAction)save_Action:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"manuallyData.plist"];
[self.nameArr addObject:self.nameField.text];
[self.countryArr addObject:self.countryField.text];
[self.imageArr addObject:#"image.png"];
NSDictionary *plistDict = [[NSDictionary alloc] initWithObjects: [NSArray arrayWithObjects: self.nameArr, self.countryArr, self.imageArr, nil] forKeys:[NSArray arrayWithObjects: #"Name", #"Country",#"Image", nil]];
NSError *error = nil;
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(plistData)
{
[plistData writeToFile:plistPath atomically:YES];
alertLbl.text = #"Data saved sucessfully";
}
else
{
alertLbl.text = #"Data not saved";
}
}
// Data is saved in your plist and plist is saved in DocumentDirectory
Step:4
Retrieve Data from plist File:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"manuallyData.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
plistPath = [[NSBundle mainBundle] pathForResource:#"manuallyData" ofType:#"plist"];
}
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.nameArr = [dict objectForKey:#"Name"];
self.countryArr = [dict objectForKey:#"Country"];
Step:5
Remove data from plist file:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"manuallyData.plist"];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:(NSString *)plistPath];
self.nameArr = [dictionary objectForKey:#"Name"];
self.countryArr = [dictionary objectForKey:#"Country"];
[self.nameArr removeObjectAtIndex:indexPath.row];
[self.countryArr removeObjectAtIndex:indexPath.row];
[dictionary writeToFile:plistPath atomically:YES];
Step:6
Update your data on Update click Action:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"manuallyData.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
plistPath = [[NSBundle mainBundle] pathForResource:#"manuallyData" ofType:#"plist"];
}
self.plistDic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
[[self.plistDic objectForKey:#"Name"] removeObjectAtIndex:self.indexPath];
[[self.plistDic objectForKey:#"Country"] removeObjectAtIndex:self.indexPath];
[[self.plistDic objectForKey:#"Image"] removeObjectAtIndex:self.indexPath];
[[self.plistDic objectForKey:#"Name"] insertObject:nameField.text atIndex:self.indexPath];
[[self.plistDic objectForKey:#"Country"] insertObject:countryField.text atIndex:self.indexPath];
[[self.plistDic objectForKey:#"Image"] insertObject:#"dhoni.jpg" atIndex:self.indexPath];
[self.plistDic writeToFile:plistPath atomically:YES];
SWIFT 3.0
Below is the code to read and write Data in .plist File.
Create a data.plist file.
Make sure that root object is of type Dictionary.
class PersistanceViewControllerA: UIViewController {
#IBOutlet weak var nationTextField: UITextField!
#IBOutlet weak var capitalTextField: UITextField!
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
displayNationAndCapitalCityNames()
//Get Path
func getPath() -> String {
let plistFileName = "data.plist"
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentPath = paths[0] as NSString
let plistPath = documentPath.appendingPathComponent(plistFileName)
return plistPath
}
//Display Nation and Capital
func displayNationAndCapitalCityNames() {
let plistPath = self.getPath()
self.textView.text = ""
if FileManager.default.fileExists(atPath: plistPath) {
if let nationAndCapitalCities = NSMutableDictionary(contentsOfFile: plistPath) {
for (_, element) in nationAndCapitalCities.enumerated() {
self.textView.text = self.textView.text + "\(element.key) --> \(element.value) \n"
}
}
}
}
//On Click OF Submit
#IBAction func onSubmit(_ sender: UIButton) {
let plistPath = self.getPath()
if FileManager.default.fileExists(atPath: plistPath) {
let nationAndCapitalCities = NSMutableDictionary(contentsOfFile: plistPath)!
nationAndCapitalCities.setValue(capitalTextField.text!, forKey: nationTextField.text!)
nationAndCapitalCities.write(toFile: plistPath, atomically: true)
}
nationTextField.text = ""
capitalTextField.text = ""
displayNationAndCapitalCityNames()
}
}
output:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Canada</key>
<string>Ottawa</string>
<key>China</key>
<string>Beijin</string>
<key>Germany</key>
<string>Berlin</string>
<key>United Kingdom</key>
<string>London</string>
<key>United States of America</key>
<string>Washington, D.C.</string>
</dict>
</plist>
Operation Read, Write, update and delete plist file Xcode 11.3 with Swift 5.0
Add new plist file to your project
then storage it to the folder
When you add ur plist file to your project then you need to copy to this file from your main bundle to document directory and perform the operation , here is the code of Write, update and delete plist file
//Operation Write, update and delete plist file
static func chipsOperationPropertyList(operation: chipsOperation) {
//chipOperation is enum for add, edit and update
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let path = paths.appending("/StoreData.plist")
let fileManager = FileManager.default
if (!(fileManager.fileExists(atPath: path)))
{
do {
let bundlePath : NSString = Bundle.main.path(forResource: "StoreData", ofType: "plist")! as NSString
try fileManager.copyItem(atPath: bundlePath as String, toPath: path)
}catch {
print(error)
}
}
var plistDict:NSMutableDictionary = NSMutableDictionary(contentsOfFile: path)!
switch operation {
case chipsOperation.add:
plistDict.setValue("Value", forKey: "Key")
break
case chipsOperation.edit:
plistDict["Key"] = "Value1"
break
case chipsOperation.delete:
plistDict.removeObject(forKey: "Key")
break
}
plistDict.write(toFile: path, atomically: true)
}
and finally here is read plist file here
static func readPropertyList() {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let path = paths.appending("/StoreData.plist")
let plistDict = NSDictionary(contentsOfFile: path)
print(plistDict)
}
Simple Example
NSString *filePath=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"country.plist"];
// ADD Plist File
NSMutableArray *arr=[[NSMutableArray alloc]initWithObjects:#"India",#"USA" ,nil];
[arr writeToFile:filePath atomically:YES];
//Update
NSFileManager *fm=[NSFileManager defaultManager];
[arr removeObjectIdenticalTo:#"India"];
[fm removeItemAtPath:filePath error:nil];
[arr writeToFile:filePath atomically:YES];
// Read
NSMutableArray *arr=[[NSMutableArray alloc]initWithContentsOfFile:filePath];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"plist.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]) {
path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: #"yourfilename.plist"]];
}
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *data;
if ([fileManager fileExistsAtPath: path]) {
data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
} else {
// If the file doesn’t exist, create an empty dictionary
data = [[NSMutableDictionary alloc] init];
}
//To insert the data into the plist
int value = 5;
[data setObject:[NSNumber numberWithInt:value] forKey:#"value"];
[data writeToFile: path atomically:YES];
//To retrieve the data from the plist
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
int savedvalue;
savedvalue = [[savedStock objectForKey:#"value"] intValue];
NSLog(#“%d”, savedvalue);
You have already created a plist. This plist will remain same in app. If you want to edit the data in this plist, add new data in plist or remove data from plist, you can’t make changes in this file.
For this purpose you will have to store your plist in Document Directory. You can edit your plist saved in document directory.
Save plist in document directory as:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#”Data” ofType:#”plist”]; NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath]; NSDictionary *plistDict = dict;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *error = nil;
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if (![fileManager fileExistsAtPath: plistPath]) {
if(plistData)
{
[plistData writeToFile:plistPath atomically:YES];
}
}
else
{ }
Retrieve data from Plist as:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask,
YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"Data.plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
NSArray *usersArray = [dict objectForKey:#"Object1"];
You can edit remove, add new data as per your requirement and save the plist again to Document Directory.
Ref:https://medium.com/#javedmultani16/save-and-edit-delete-data-from-plist-in-ios-debfc276a2c8
Got a little problem. I have a .plist file which contain next data
So i cant understand, how i need read first array, than in array read dictionary. Or maybe need rewrite file and change Root key to type dictionary, and read like this:
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSString *plistPath;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0];
plistPath = [rootPath stringByAppendingPathComponent:#"Data.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
}
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&errorDesc];
if (!temp) {
NSLog(#"Error reading plist: %#, format: %d", errorDesc, format);
}
self.personName = [temp objectForKey:#"Name"];
self.phoneNumbers = [NSMutableArray arrayWithArray:[temp objectForKey:#"Phones"]];
You can do it by the following code:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
NSArray *dataArray = [NSArray arrayWithContentsOfFile:plistPath];
This array will contain all the dictionaries, you can get them like:
NSDictionary *dict = [dataArray objectAtIndex:0];
SString *plistFile = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:plistFile];
for(NSDictionary *dictionary in array) {
NSLog(#"%#", dictionary);
}
NSDictionary *item0 = array[0];
NSString *imageName = item[0][#"name"];
In a method I need to read a NSArray or NSDictionary from a plist file.
Here is my problem : How can I create a NSArray OR a NSDictionary from a plist file when I don't know if it's an array or dictionary in it ?
I know I can make :
NSArray *myArray = [NSArray arrayWithContentsOfFile:filePath];
or
NSDictionary *myDico = [NSDictionary dictionaryWithContentsOfFile:filePath];
But I don't know if filePath contains a NSArray or a NSDictionary. I would like something like :
id myObject = [... ...WithContentOfFile:filePath];
Can somebody help me and give me the best way to do this ?
Here's how
NSData *data = [NSData dataWithContentsOfFile:filePath];
id obj = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:nil];
if([obj isKindOfClass:[NSDictionary class]]) {
//cast obj to NSDictionary
}
else if([obj isKindOfClass:[NSArray class]]) {
//cast obj to NSArray
}
You can use isKindOfClass method to detect which class is that.
You will have to load .plist file using:
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
plistPath = [rootPath stringByAppendingPathComponent:#"Data.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
}
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
id objectFromPlist = [NSPropertyListSerialization
propertyListWithData:plistXML
options:0
format:&format
errorDescription:&errorDesc];
And you can check here:
if([objectFromPlist isKindOfClass:[NSArray class]])
{
NSArray* plistArray = (NSArray*) classFromPlist;
}
I am trying to add and array to a Root array in my plist:
And is not working. Here's my code:
-(IBAction)addName:(id)sender{
NSArray *arrayValues = [NSArray arrayWithObjects: nameLabel.text, nameDate.text, nameValue.text, nil];
NSString *plistpath = [[NSBundle mainBundle] pathForResource:#"Names" ofType:#"plist"];
NSMutableArray *namesNew = [[NSMutableArray alloc] initWithContentsOfFile:plistpath];
[namesNew addObject:arrayValues];
[namesNew writeToFile:plistpath atomically:YES];
}
What am I doing wrong? Thanks!
You need to move the file to NSDocumentDirectory. Then edit the plist file.
For example:
Moving to NSDocumentDirectory:
-(NSDictionary *)copyBundleToDocuments
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:#"Names.plist"];
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *bundlePlistPath = [bundlePath stringByAppendingPathComponent:#"Names.plist"];
//if file exists in the documents directory, get it
if([fileManager fileExistsAtPath:documentPlistPath])
{
NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithContentsOfFile:documentPlistPath];
return documentDict;
}
//if file does not exist, create it from existing plist
else
{
NSError *error;
BOOL success = [fileManager copyItemAtPath:bundlePlistPath toPath:documentPlistPath error:&error];
if (success) {
NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithContentsOfFile:documentPlistPath];
return documentDict;
}
return nil;
}
}
Then get the plist:
-(void)plistArray:(NSArray*)array
{
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//getting the plist file name:
NSString *plistName = [NSString stringWithFormat:#"%#/Names.plist",
documentsDirectory];
NSMutableArray *namesNew = [[NSMutableArray alloc] initWithContentsOfFile:plistName];
[namesNew addObject:arrayValues];
[namesNew writeToFile:plistName atomically:YES];
return nil;
}
The plist should be a dictionary as the base object instead of an array.
NSMutableDictionary *namesNew = [NSMutableDictionary dictionaryWithContentsOfFile:plistpath];
[namesNew setObject: arrayValues forKey: #"Root"];
[namesNew writeToFile:plistpath atomically:YES];
You cant write your plist to the bundle you need to use NSDocumentDirectory or NSCachesDirectory
Just copy your plist to bundle the overwrite it.
Note: learn the difference between NSCachesDirectory and NSDocumentDirectory
https://developer.apple.com/icloud/documentation/data-storage/
Copy your plist from bundle to documents(in below code caches), you need to this only one time if your plist in your bundle, I prefer using this code in appdelegate.m when - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Names.plist"];
NSString *plistInDocuments=#"Names.plist";
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:plistInDocuments];
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){
[[NSFileManager defaultManager] copyItemAtPath:sourcePath
toPath:dataPath
error:&error];
}
NSLog(#"Error description-%# \n", [error localizedDescription]);
NSLog(#"Error reason-%#", [error localizedFailureReason]);
Get your file and overwrite it
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistInDocuments=#"Names.plist";
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:plistInDocuments];
//add object here
NSMutableArray *namesNew = [[NSMutableArray alloc] initWithContentsOfFile:dataPath];
[namesNew addObject:arrayValues];
NSError *error = nil;
if ([myFile writeToFile:dataPath options:NSDataWritingAtomic error:&error]) {
// file saved
} else {
// error writing file
NSLog(#"Unable to write plist to %#. Error: %#", dataPath, error);
}
Using this code I am trying to get the data from Templates.plist file but I am getting null value in array.
//from plist
NSString *path = [[NSBundle mainBundle] pathForResource:#"Templates" ofType:#"plist"];
NSMutableArray *arry=[[NSMutableArray alloc]initWithContentsOfFile:path];
NSLog(#"arrayArray:::: %#", arry);
This is my plist file:
Also I want to know how can I add more strings to this plist file and delete a string from this plist.
First off you cannot write to anything in you mainBundle. For you to write to you plist you need to copy it to the documents directory. This is done like so:
- (void)createEditableCopyOfIfNeeded
{
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:#"Template.plist"];
success = [fileManager fileExistsAtPath:writablePath];
if (success)
return;
// The writable file does not exist, so copy from the bundle to the appropriate location.
NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Template.plist"];
success = [fileManager copyItemAtPath:defaultPath toPath:writablePath error:&error];
if (!success)
NSAssert1(0, #"Failed to create writable file with message '%#'.", [error localizedDescription]);
}
So calling this function will check if the file exists in the documents directory. If it doesn't it copies the file to the documents directory. If the file exists it just returns. Next you just need to access the file to be able to read and write to it. To access you just need the path to the documents directory and to add your file name as a path component.
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:#"Template.plist"];
To get the data from the plist:
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
To write the file back to the documents directory.
[array writeToFile:filePath atomically: YES];
-(NSMutableArray *)start1
{
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:#"Templates.plist"];
if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]){
plistArr = [NSMutableArray arrayWithContentsOfFile: plistPath];
}
else {
plistArr = [[NSMutableArray alloc] init];
}
return plistArr;
}
- (void)createNewRecordWithName:(NSMutableDictionary *)dict
{
plistArr=[self start1];
[plistArr addObject: dict];
//[dict release];
[self writeProductsToFile:plistArr];
}
- (void)writeProductsToFile:(NSMutableArray *)array1 {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:#"Template.plist"];
[array1 writeToFile:plistPath atomically:YES];
}
To get a plist into a mutable array, use this class method:
NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:path];
Then, add/delete strings from the array. To save the array back to a plist, use:
[array writeToFile:path atomically:YES];