views:

4706

answers:

4

Hi to all,

I have created save.plist in resource folder. I have written some data within that directly (without using coding). I am able to read that data but I cant able write through coding into the same save.plist. By using following code I am trying to write the data but it get stored within my .app plist. The code is here

NSString *errorDesc = nil;

NSPropertyListFormat format;

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"save" ofType:@"plist"];

NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];

NSMutableDictionary *temp = (NSMutableDictionary *)[NSPropertyListSerialization
            propertyListFromData:plistXML
                           mutabilityOption:NSPropertyListMutableContainersAndLeaves
         format:&format errorDescription:&errorDesc];

if (!temp) {

    NSLog(errorDesc);

    [errorDesc release];  
    }
    // [temp setValue:@"123" forKey:@"line1"];
    // [temp writeToFile:plistPath atomically: YES];

    //Reading data from save.plist
    NSLog([temp objectForKey:@"name"]);
    NSLog([temp objectForKey:@"wish"]);
    NSNumber *num=[temp valueForKey:@"roll"];
    int i=[num intValue];
    printf("%d",i);
        //writitng the data in save.plist

    [temp setValue:@"green" forKey:@"color"];
    [temp writeToFile:plistPath atomically: NO];
    NSMutableDictionary *temp1 = (NSMutableDictionary *)[NSPropertyListSerialization
       propertyListFromData:plistXML                    
                                mutabilityOption:NSPropertyListMutableContainersAndLeaves
       format:&format errorDescription:&errorDesc];

    NSLog([temp objectForKey:@"color"]);

I want that, the data which I want to write should get written into save.plist only which is stored in references. I am new with this concept. So if anyone knows it please help me. Thanks in advance. :-)

+2  A: 

I don't know if I understand your question, but if you want to write into a .plist within your .app bundle you are probably doing something wrong. If you want to store preferences, you should consider using NSUserDefaults.
If you really want to modify a bundled .plist - here is some code:

NSString* plistPath = nil;
NSFileManager* manager = [NSFileManager defaultManager];
if (plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Contents/Info.plist"]) 
{
    if ([manager isWritableFileAtPath:plistPath]) 
    {
     NSMutableDictionary* infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
     [infoDict setObject:[NSNumber numberWithBool:hidden] forKey:@"LSUIElement"];
     [infoDict writeToFile:plistPath atomically:NO];
     [manager changeFileAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] atPath: [[NSBundle mainBundle] bundlePath]];
    }
}
weichsel
I don't want to use NSUserDefault and also I don't want to store data into save.plist within our .app bundle. I want to stored it into save.plist which I created in resource folder. Because whatever changes I got by above code is for temporary basis and also it saved in save.plist within app. So I cant able to see the changes directly without coding.
Jyotsna
+3  A: 
iPhoney
+1  A: 

To summarize some of the other answers:

You're problem is that you're trying to write the file back into the folder that contains your application. That folder is not writable at runtime. Everything you're doing is fine, you just need to pick a different location to write your file to.

You can use the NSSearchPathForDirectoriesInDomains function to find a more suitable folder for this data.

Jon Hess
A: 

Hi Jyotsna, Please accept the answers, your accept rate is just 5%.

Harsh