Davidin073
Davidin073

Reputation: 1021

Edit plist in objective c

I have a plist file and i want edit this file, but setObject() in NSDictionary dont operate, my code is:

NSString *documentsDirectoryConf = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSString *pathConf = [documentsDirectoryConf stringByAppendingPathComponent: @"Test.plist"];
NSMutableDictionary *infoConf = [[NSMutableDictionary alloc] initWithContentsOfFile:pathConf];
NSMutableArray* defaultIm = [infoConf objectForKey:@"DatosDescargas"];
NSMutableDictionary *banners = [defaultIm objectAtIndex:0];

NSInteger numI = (int)[banners objectForKey:@"NumImages"]; 
NSInteger numD = (int)[banners objectForKey:@"DownloadImges"]; 

NSLog(@"Numero total de Imagenes: %i",numI);
NSLog(@"Numero de Imagenes descargadas: %i",numD);

**numI = 7206;
numD = 5;**

[banners setObject:(id)numI forKey:@"NumImages"];
[banners setObject:(id)numD forKey:@"DownloadImges"];

[defaultIm replaceObjectAtIndex:0 withObject:banners];

[infoConf setObject:defaultIm forKey:@"DatosDescargas"];

pathConf = [documentsDirectoryConf stringByAppendingPathComponent: @"Test.plist"];

//save content to the documents directory
[infoConf writeToFile:[pathConf stringByExpandingTildeInPath] 
           atomically:TRUE];

defaultIm = [infoConf objectForKey:@"DatosDescargas"];
banners = [defaultIm objectAtIndex:0];

numI = (int)[banners objectForKey:@"NumImages"]; 
numD = (int)[banners objectForKey:@"DownloadImges"]; 

NSLog(@"Numero total de Imagenes: %i",numI);
NSLog(@"Numero de Imagenes descargadas: %i",numD);

In my console its possible see that dont edit any value:

2011-11-30 17:55:07.879 Catalogo-V1[5905:207] Numero total de Imagenes: 0
2011-11-30 17:55:07.880 Catalogo-V1[5905:207] Numero de Imagenes descargadas: 0
2011-11-30 17:55:07.880 Catalogo-V1[5905:207] Numero total de Imagenes: 0
2011-11-30 17:55:07.881 Catalogo-V1[5905:207] Numero de Imagenes descargadas: 0

And my file have this format, its necessary modify the second dictionary, after that modify a array and the first dictionary:

<?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>DatosDescargas</key>
    <array>
        <<dict>
            <key>NumImages</key>
            <integer>0</integer>
            <key>DownloadImges</key>
            <integer>0</integer>
        </dict>
    </array>
</dict>
</plist>

Upvotes: 1

Views: 587

Answers (1)

TigerCoding
TigerCoding

Reputation: 8720

You need to convert it to an NSNumber

[NSNumber numberWithInt:numI];

Then you can retrieve it:

numI = [[banners objectForKey:@"NumImages"] intValue]; 

Upvotes: 2

Related Questions