Problem with reading/writing plist - iphone

I have an MSMutableArray that contains many NSDictionaries. A dictionary is just an NSString with a key "symbol".
I am writing my array to a plist:
+ (void)writeObjectToPList:(id)myData {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"mobile-watchlist.plist"];
[myData writeToFile:path atomically:YES];
}
How can I read this back as an array?

You can read the file back like this --
NSArray *array = [NSArray arrayWithContentsOfFile:path];

Related

How to update the string inside the PLIST

I have Array of the 40 item and inside that dict.Dict contain the 2 string item, i want to write the string item to 0 to 1 when user click on uibutton
<array>
<dict>
<key>quote</key>
<string>Some Quotes</string>
<key>favorite</key>
<string>0</string>
</dict>
</array>
I am applying following code but it doesn`t work for me ...
-(void) addFavorite
{
NSLog(#"Hello");
[someButton setImage:[UIImage imageNamed:#"add_favoritegold.png"] forState:UIControlStateHighlighted];
NSURL *documents = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *plistUrl = [documents URLByAppendingPathComponent:#"quotes.plist"];
NSArray *plistContents = [NSArray arrayWithContentsOfURL:plistUrl];
NSMutableDictionary *editFavorite = [plistContents objectAtIndex:1];
}
it does not change the favorite 0 to 1 and my plist is in the supporting file do i have to make any changes in property list .
-(void)btnClicked
{
[arr_Data addObject:str1];
[arr_Data addObject:str2];
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsDirectory = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"Data.plist"];
// This writes the array to a plist file. If this file does not already exist, it creates a new one.
[arr_Data writeToFile:path atomically: TRUE];
}
Please refer below code for your concept
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *plistdocumentsDirectory = [paths objectAtIndex:0];
NsString * plistFilePath = [plistdocumentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"filename.plist"]];
NSMutableArray *savedStock = [[NSMutableArray alloc] initWithContentsOfFile:plistFilePath];
[[savedStock objectAtIndex:indexForWheretoStoreData] setObject:#"your value" forKey:#"your key"];
[savedStock writeToFile:filePath atomically:YES];
use [yourArray writeToURL:yourPath atomically:YES];to store data
It's a simple 4 Step Process :
1) Select the Dictionary (you want to update the "favorite" tag).
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *yourPath = [documentsDirectory stringByAppendingPathComponent:#"yourPlist.plist"];
2) Set New Value.
[yourDict setValue:#"1" forKey:#"favorite"];
3) Insert it to the Array.
NSMutableArray *yourArray = [[NSMutableArray alloc] init];
[yourArray addObject:yourDict];
4) Write that array to yourPath.
[yourArray writeToURL:yourPath atomically:YES];

can't creating plist file in document folder

I have write code like this in Viewdidload method. But it don't create file .plist.
I don't know whether code is wi8 or wrong. And where i have to write?
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; //2
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"data1.plist"]; //3
NSFileManager *fileManager = [NSFileManager defaultManager];
if ( ![fileManager fileExistsAtPath:path] ) {
NSString *pathToSettingsInBundle = [[NSBundle mainBundle]
pathForResource:#"data1" ofType:#"plist"];
}
You have just created a path to it, you should try loading it into a NSDictionary.
NSDictionary* mydictionary = [ NSDictionary dictionaryWithContentsOfFile: pathToSettingsInBundle];
Inside your if block add a second line
[fileManager copyItemAtPath:pathToSettingsInBundle toPath:path error:&error];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
plistpath = [[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:#"Product.plist"]];
And then
[array writeToFile:path atomically:YES];
[Whatever [Array or Dictionary] you want to Write in Plist]
If it is still not creating the plist then check whether your array or dictionary must be empty.

Problem writing NSData in my device

Can someone explain me why my code doesn't work?
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the
//documents directory:
NSString *fullFileName = [NSString stringWithFormat:#"%#/subscriptions", documentsDirectory];
[dataReply writeToFile:fullFileName atomically:NO];
NSData *getData=[[NSData alloc]initWithContentsOfFile:fullFileName];
infact if I try
NSString *string = [[NSString alloc] initWithData:getData encoding:NSASCIIStringEncoding];
while if I load myData without save it to my device it works! (So I'm sure that my data is not empty)
Try This,
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the
//documents directory:
NSString *fullFileName = [NSString stringWithFormat:#"%#/subscriptions", documentsDirectory];
[dataReply writeToFile:fullFileName atomically:NO];
Nsdata *getData=[fullFileName dataUsingEncoding:NSUTF8StringEncoding];

writeToFile path?

I'm wondering what is the default path when saving a file using the NSArray writeToFile:atomically: method...?
[array writeToFile:#"myFile.plist" atomically:YES];
10x
It will save your file at Document directory.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = [paths objectAtIndex:0];
this is the path where your file will be saved.
I executed the following code:
if ([#"asdasdasd" writeToFile:#"foo123456.txt" atomically:YES]) {
NSLog(#"+++");
} else {
NSLog(#"---");
}
On the simulator the file foo123456.txt popped up in my root of my HD. The console printed +++, so the operation was successful.
On the device it failed, printing ---.
Use This Code.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
plistpath = [[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:#"Product.plist"]];
And then
[array writeToFile:path atomically:YES];
[Whatever [Array or Dictionary] you want to Write in Plist]
If it is still not creating the plist then check whether your array or dictionary must be empty.

download file through coding

I would like to save a file in my application which I have created with code:
NSArray *arrayPathsForImg =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *pathForImg = [arrayPathsForImg objectAtIndex:0];
pathForImg = [pathForImg stringByAppendingPathComponent:#"a.txt"];
[self.importUrlData writeToFile:pathForImg atomically:YES];
Now I want to download it to my iPhone?
If you meant "read" the file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"a.txt"];
And now use a initWithContentsOfFile: method. For example I used the code above to read from a property list and used the following code to get the data into a NSDictionary:
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];