Creating and writing/reading from plist file not working - objective-c

I am trying to save multiple games and whenever I want to save one the first method is called. However, no matter what I do, [NSFileManager defaultManager] keeps telling me that there is no file at [SaveGameManager filePath] and [SaveGameManager savedGames] keeps giving me nil objects. Any advice/help?
#implementation SaveGameManager
+(void)saveGameWithDate:(NSDate*)date type:(NSInteger)type allMoves:(NSArray*)allMoves players:(NSArray*)players playerTimes:(NSArray*)playerTimes delayTime:(double)delayTime useTimer:(BOOL)useTimer delayOption:(NSInteger)delayOption drawOfferState:(NSInteger)drawOfferState resignationState:(NSInteger)resignationState current:(BOOL)current {
NSLog(#"saving game");
NSMutableArray* savedGames = [SaveGameManager savedGames];
if (!savedGames) {
savedGames = [NSMutableArray array];
}
NSMutableDictionary* dict = [#{#"date" : date, #"boardType" : #(type), #"allMoves" : allMoves, #"players" : players, #"delayTime" : #(delayTime), #"useTimer" : #(useTimer), #"delayOption" : #(delayOption), #"drawOfferState" : #(drawOfferState), #"resignationState" : #(resignationState), #"current" : #(current)} mutableCopy];
if (playerTimes) {
dict[#"playerTimes"] = playerTimes;
}
[savedGames addObject:dict];
[savedGames writeToFile:[SaveGameManager filePath] atomically:YES];
}
+(NSString*)filePath {
static NSString* path;
if (!path) {
path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:#"SavedGames.plist"];
}
return path;
}
+(NSMutableArray*)savedGames {
return [NSMutableArray arrayWithContentsOfFile:[SaveGameManager filePath]];
}
#end

Use this code to save a NSDictionary in Plist format in Application document.
+ (void)saveSessionToDisk:(NSDictionary *)session {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithDictionary:session];
NSError *error;
NSData *archiveData = [NSKeyedArchiver archivedDataWithRootObject:dictionary];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *fullPath = [documentsDir stringByAppendingPathComponent:#"SavedSession.plist"];
[archiveData writeToFile:fullPath options:NSDataWritingAtomic error:&error];
}
Use this code to Load NSDictionary from Plist that you saved earlier.
+ (NSDictionary *)loadSessionFromDisk
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *fullPath = [documentsDir stringByAppendingPathComponent:#"SavedSession.plist"];
DLog(#"%#",fullPath);
NSFileManager *fileManager =[NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:fullPath]) {
NSMutableDictionary *dict = nil;
#try {
NSData *archiveData = [NSData dataWithContentsOfFile:fullPath];
dict = (NSMutableDictionary*)[NSKeyedUnarchiver unarchiveObjectWithData:archiveData];
if ([dict count] > 0) {
return dict;
} else {
return nil;
}
} #catch (NSException *e) {
}
} else {
return nil;
}
}
Always use this condition before you try to load NSDictionary :
if ([[UtilityFunctions loadSessionFromDisk] isKindOfClass:[NSDictionary class]]) {
NSDictionary *myDictionary = [UtilityFunctions loadSessionFromDisk];
}

Related

How do I download an image from a URL and save it to my computer?

How would I download an image from a URL, and have that saved to the computer using Objective-C? This is what I got so far:
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
UIImage *imageFromURL = [self getImageFromURL:#"https://www.google.com/images/srpr/logo11w.png"];
[self saveImage:imageFromURL withFileName:#"Google Logo" ofType:#"png" inDirectory:documentsDirectoryPath];
UIImage *imageFromWeb = [self loadImage:#"Google Logo" ofType:#"png" inDirectory:documentsDirectoryPath];
Xcode complains about UIIMage, trying to replace with NSImage. It also complains about an undeclared identifier 'self'.
I need to make an HTTP call to perform this as well. Explain this to me like I'm 5.
Here is the code to Save the image into document Directory.
-(void)saveImagesInLocalDirectory
{
NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *imgName = #"image.png";
NSString *imgURL = #"www.example.com/image/image.png";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *writablePath = [documentsDirectoryPath stringByAppendingPathComponent:imgName];
if(![fileManager fileExistsAtPath:writablePath]){
// file doesn't exist
NSLog(#"file doesn't exist");
if (imgName) {
//save Image From URL
[self getImageFromURLAndSaveItToLocalData:imgName fileURL:imgURL inDirectory:documentsDirectoryPath];
}
}
else{
// file exist
NSLog(#"file exist");
}
}
-(void) getImageFromURLAndSaveItToLocalData:(NSString *)imageName fileURL:(NSString *)fileURL inDirectory:(NSString *)directoryPath {
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
NSError *error = nil;
[data writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#", imageName]] options:NSAtomicWrite error:&error];
if (error) {
NSLog(#"Error Writing File : %#",error);
}else{
NSLog(#"Image %# Saved SuccessFully",imageName);
}
}
And this is the one method code..
-(void)saveImagesInLocalDirectory
{
NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *imgName = #"image.png";
NSString *imgURL = #"www.example.com/image/image.png";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *writablePath = [documentsDirectoryPath stringByAppendingPathComponent:imgName];
if(![fileManager fileExistsAtPath:writablePath]){
// file doesn't exist
NSLog(#"file doesn't exist");
//save Image From URL
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString: imgURL]];
NSError *error = nil;
[data writeToFile:[documentsDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#", imgName]] options:NSAtomicWrite error:&error];
if (error) {
NSLog(#"Error Writing File : %#",error);
}else{
NSLog(#"Image %# Saved SuccessFully",imgName);
}
}
else{
// file exist
NSLog(#"file exist");
}
}
This is my solution!
+(BOOL)downloadMedia :(NSString*)url_ :(NSString*)name{
NSString *stringURL = url_;
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,name];
[urlData writeToFile:filePath atomically:YES];
return YES;
}
return NO;
}
+(UIImage*)loadMedia :(NSString*)name{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:name];
UIImage *img_ = [UIImage imageWithContentsOfFile:getImagePath];
return img_;
}

issues reading from plist file

I have created a simple plist file with some user preferences for a card game I'm writing.
I have also created a controller that reads and writes to this plist file which is a singelton.
everything works fine, but then after a couple of tries it stops working.
Logging the values to the console it shows the list returning a value of 0 which causes my app to crash
I have deleted the plist and created a new one and then the same story, works fine for 2 or three time and then boom zero.
here is a copy of the controller singelton code:
#implementation userOptionsController
static userOptionsController* _sharedOptionsController = nil;
#synthesize backgroundSound=_backgroundSound;
#synthesize soundEffects = _soundEffects;
#synthesize coach = _coach;
#synthesize numberOfDecks = _numberOfDecks ;
+(userOptionsController*)sharedOptionsController{
#synchronized([userOptionsController class])
{
if(!_sharedOptionsController)
[[self alloc]init];
return _sharedOptionsController;
}
return nil;
}
+(id)alloc
{
#synchronized ([userOptionsController class])
{
NSAssert(_sharedOptionsController == nil, #"Attempted to allocate a second instance of userOptionsController singleton");
_sharedOptionsController = [super alloc];
return _sharedOptionsController;
}
return nil;
}
- (id) init {
self = [super init];
if (self) {
}
return self;
}
-(void)readPlistFile
{
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"playerPrefOptions.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"playerPrefOptions" ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
NSMutableDictionary *temp = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
self.backgroundSound = [[temp objectForKey:#"backgroundSounds"]boolValue];
self.soundEffects = [[temp objectForKey:#"soundEffects"]boolValue];
self.coach =[[temp objectForKey:#"coach"]boolValue];
self.numberOfDecks = [[temp objectForKey:#"numberOfDecks"]intValue];
}
-(void)writeOptionsToFile
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"playerPrefOptions.plist"];
NSMutableDictionary *infoDict = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
NSNumber *moshe = [NSNumber numberWithInt:self.numberOfDecks];
[infoDict setObject: moshe forKey:#"numberOfDecks"];
[infoDict setObject:[NSNumber numberWithBool:self.coach] forKey:#"coach"];
[infoDict setObject:[NSNumber numberWithBool:self.backgroundSound] forKey:#"backgroundSounds"];
[infoDict setObject:[NSNumber numberWithBool:self.soundEffects] forKey:#"soundEffects"];
[infoDict writeToFile:path atomically:YES];
}
#end
so the property :
int numberOfDecks =[userOptionsController sharedOptionsController].numberOfDecks;
will return zero.
any ideas?
thanks.
Rather than use a plist for this content, it looks like NSUserDefaults is a more appropriate location.
Instead of shipping the app with a default plist file, instead just registerDefaults: with NSUserDefaults (often done in your app delegate application:didFinishLaunchingWithOptions:).
Then, whenever any changes are made just update NSUserDefaults and call synchronize to save the changes.
Try this and see what it does (what logs are output):
#implementation userOptionsController
+ (userOptionsController*)sharedOptionsController
{
static dispatch_once_t pred = 0;
__strong static id _sharedObject = nil;
dispatch_once(&pred, ^{
_sharedObject = [[self alloc] init];
});
return _sharedObject;
}
- (id) init {
self = [super init];
if (self) {
}
return self;
}
-(void)readPlistFile
{
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"playerPrefOptions.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"playerPrefOptions" ofType:#"plist"];
if (![fileManager copyItemAtPath:bundle toPath: path error:&error]) {
NSLog(#"ERROR - file couldn't be copied: %#", error);
}
}
NSMutableDictionary *temp = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
if (temp == nil) {
NSLog(#"ERROR - file couldn't be read");
}
self.backgroundSound = [[temp objectForKey:#"backgroundSounds"]boolValue];
self.soundEffects = [[temp objectForKey:#"soundEffects"]boolValue];
self.coach =[[temp objectForKey:#"coach"]boolValue];
self.numberOfDecks = [[temp objectForKey:#"numberOfDecks"]intValue];
}
-(void)writeOptionsToFile
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"playerPrefOptions.plist"];
NSMutableDictionary *infoDict = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
NSNumber *moshe = [NSNumber numberWithInt:self.numberOfDecks];
[infoDict setObject: moshe forKey:#"numberOfDecks"];
[infoDict setObject:[NSNumber numberWithBool:self.coach] forKey:#"coach"];
[infoDict setObject:[NSNumber numberWithBool:self.backgroundSound] forKey:#"backgroundSounds"];
[infoDict setObject:[NSNumber numberWithBool:self.soundEffects] forKey:#"soundEffects"];
if (![infoDict writeToFile:path atomically:YES]) {
NSLog(#"ERROR - failed to write the new file (%#)", path);
} else {
NSLog(#"Completed write of:\n%#", infoDict);
}
}
#end

UIImage gives nil

I'm quite new to iOS development. My app gets a file over a network, writes it as image.png and later on reads and displays the image. However, the display part is not working as my UIImage object is always set to nil (on the iOS simulator). I've tried implementing other answers from stackoverflow, but no luck.
Here's my code to save the file:
//inside utility class for model
NSFileHandle * handle = nil;
//For first packet of new file request
if(CountFileParts == 1)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"image.png"];
NSLog(#"%#",appFile);
handle = [NSFileHandle fileHandleForWritingAtPath:appFile];
if(handle == nil)
{
[[NSFileManager defaultManager] createFileAtPath:appFile contents:nil attributes:nil];
handle = [NSFileHandle fileHandleForWritingAtPath:appFile];
}
}
//For other incoming packets of the same request
else
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"image.png"];
//NSLog(#"%#",appFile);
handle = [NSFileHandle fileHandleForUpdatingAtPath:appFile];
[handle seekToEndOfFile];
//NSLog(#"Writing continue in new file");
}
if(handle == nil)
NSLog(#"handle nil");
NSData * data = [str dataUsingEncoding:NSUTF8StringEncoding];
[handle writeData:data];
[handle closeFile];
if(index != -1 && index!= NSNotFound)
{
NSLog(#"Inside Bool");
self.isPlotReady = YES;//kvo in view-controller as shown below
self.isPlotReady = NO;
}
Here's my code to load the image file:
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqualToString:#"isPlotReady"])
{
self.isReady = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];
[self updateUI];
}
}
-(void) updateUI
{
if(self.isReady)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[self lsOwnDirectory:documentsDirectory];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"image.png"];
//NSLog(#"%#",appFile);
UIImage *img = [UIImage imageWithContentsOfFile:appFile];
if(img == nil)
NSLog(#"Couldn't find image");
else
{
UIImageView *imageView = [[UIImageView alloc] initWithImage:img] ;
[self.view addSubview:imageView];
}
}
}
//Prints Directory contents of input directory
- (void) lsOwnDirectory:(NSString *) currentpath {
NSError * error = [[NSError alloc] init];
NSFileManager *filemgr;
filemgr = [[NSFileManager alloc] init];
//currentpath = [filemgr currentDirectoryPath];
NSLog(#"Current Directory Path : %#",currentpath);
NSArray * files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:currentpath error: &error];
for(NSString * file in files){
NSLog(#"%#", file);
}
}
It alway's prints "Couldn't find image" corresponding to the if statement, but I've seen the file is still there (lsOwnDirectory prints directory contents). Maybe I'm doing something basic wrong here. Thanks in advance.

Writing to .plist returns "Success!" but ends up blank

Here is my code:
-(IBAction)btnSaveInfo:(UIButton *)sender {
NSMutableArray *data = [[NSMutableArray alloc] init];
NSDictionary *appInfo = [NSDictionary dictionaryWithObjectsAndKeys: fieldAPI.text, #"App API", fieldID.text, #"App ID", fieldName.text, #"App Name", nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *newPath = [documentsDirectory stringByAppendingPathComponent:#"AppData.plist"];
NSMutableArray *appdata = [NSMutableArray arrayWithContentsOfFile:newPath];
[appdata addObject:appInfo];
NSLog(#"%#",appdata);
[appdata writeToFile:newPath atomically:YES];
if ([data writeToFile:newPath atomically:YES]) {
NSLog(#"Success!");
NSMutableArray *finalData = [NSMutableArray arrayWithContentsOfFile:newPath];
NSLog(#"Final array:\n\n%#",finalData);
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
else {
NSLog(#"Error...");
}
[data release];
}
My NSLogs return
2012-12-23 01:20:37.628 Parse Push[38052:c07] (
{
"App API" = asdf;
"App ID" = adsf;
"App Name" = asdf;
}
)
2012-12-23 01:20:37.932 Parse Push[38052:c07] Success!
2012-12-23 01:20:37.933 Parse Push[38052:c07] Final array:
(
)
Is there an error with my code, or something that I'm missing? I am new to interacting with .plists, so I would appreciate any help, thanks!
The problem is here
if ([data writeToFile:newPath atomically:YES]) {
You are writing again an empty array here
Use this
[appdata addObject:appInfo];
NSLog(#"%#",appdata);
if ([appdata writeToFile:newPath atomically:YES]){

Correcting a plist with dictionaries

Plist is copyed to documents directory if it doesn't exist.
If it already exists, I want to use the "Name" key from NSDictionary in bundleArray to find the matching NSDictionary in documentsArray.
When the match is found, I want to check for changes in the strings and replace them if there is a change.
If a match is not found it means this dictionary must be added to documents plist.
This is my code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self managePlist];
return YES;
}
- (void)managePlist {
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"Objects.plist"];
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"Objects" ofType:#"plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]) {
[fileManager copyItemAtPath:bundle toPath:path error:&error];
} else {
NSArray *bundleArray = [[NSArray alloc] initWithContentsOfFile:bundle];
NSMutableArray *documentArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
BOOL updateDictionary = NO;
for(int i=0;i<bundleArray.count;i++) {
NSDictionary *bundleDict=[bundleArray objectAtIndex:i];
BOOL matchInDocuments = NO;
for(int ii=0;ii<documentArray.count;ii++)
{
NSMutableDictionary *documentDict = [documentArray objectAtIndex:ii];
NSString *bundleObjectName = [bundleDict valueForKey:#"Name"];
NSString *documentsObjectName = [documentDict valueForKey:#"Name"];
NSRange range = [documentsObjectName rangeOfString:bundleObjectName options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
matchInDocuments = YES;
}
if (matchInDocuments) {
if ([bundleDict objectForKey:#"District"] != [documentDict objectForKey:#"District"]) {
[documentDict setObject:[bundleDict objectForKey:#"District"] forKey:#"District"];
updateDictionary=YES;
}
}
else {
[documentArray addObject:bundleDict];
updateDictionary=YES;
}
}
}
if(updateDictionary){
[documentArray writeToFile:path atomically:YES];
}
}
}
If I run my app now I get this message: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
How can I fix this?
When this is fixed, do you think my code will work?
If not, I would be happy for some suggestions on how to do this. I have struggled for a while and really need to publish the update with the corrections! Thanks a lot for your help.
Presumably the documentDict is actually immutable. Even though you assign it to an NSMutableDictionary, this doesn't accurately describe the underlying data.
NSMutableDictionary *documentDict = [documentArray objectAtIndex:ii];
should be:
NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithDictionary:[documentArray objectAtIndex:ii]];
and wherever you edit the documentDict, add:
[documentArray replaceObjectAtIndex:ii withObject:documentDict];
When you read a plist from file, an immutable dictionary/array with immutable children is created, so your
  NSMutableDictionary *documentDict = [documentArray objectAtIndex:ii];
line is erfectively a lie - you don't have a mutable dictionary. To create a mutable object from a plist, have a look at CFPropertyListCreateWithData and specify kCFPropertyListMutableContainersAndLeaves as the mutability option.
This is my final and working code using the answer from James Webster. Thanks to H2CO3 as well for contribution.
- (void)managePlist {
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"Object.plist"];
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"Object" ofType:#"plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:path]) {
[fileManager copyItemAtPath:bundle toPath:path error:&error];
}
else
{
NSArray *bundleArray = [[NSArray alloc] initWithContentsOfFile:bundle];
NSMutableArray *documentArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
BOOL updateDictionary = NO;
for(int i=0;i<bundleArray.count;i++) {
NSDictionary *bundleDict=[bundleArray objectAtIndex:i];
for(int ii=0;ii<documentArray.count;ii++)
{
NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithDictionary:[documentArray objectAtIndex:ii]];
NSString *bundleObjectName = [bundleDict valueForKey:#"Name"];
NSString *documentsObjectName = [documentDict valueForKey:#"Name"];
NSRange range = [documentsObjectName rangeOfString:bundleObjectName options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
NSString *districtString = [bundleDict objectForKey:#"District"];
if ([documentDict objectForKey:#"District"] != districtString) {
[documentDict setObject:districtString forKey:#"District"];
[documentArray replaceObjectAtIndex:ii withObject:documentDict];
updateDictionary=YES;
}
}
}
}
if(updateDictionary){
[documentArray writeToFile:path atomically:YES];
}
}
}