Skip to content

Instantly share code, notes, and snippets.

@keitaito
Last active April 28, 2021 14:51
Show Gist options
  • Save keitaito/933dc147b7136400b0b2 to your computer and use it in GitHub Desktop.
Save keitaito/933dc147b7136400b0b2 to your computer and use it in GitHub Desktop.
Read and Write Plist File in Objective-C, converted from Swift version written by Rebeloper http://rebeloper.com/read-write-plist-file-swift/
//
// ViewController.m
// ReadAndWritePlistFile
//
// Created by Keita on 3/3/15.
// Copyright (c) 2015 Keita Ito. All rights reserved.
// converted from Swift version written by Rebeloper http://rebeloper.com/read-write-plist-file-swift/
#import "ViewController.h"
static NSString *const BedroomFloorKey = @"BedroomFloor";
static NSString *const BedbroomWallkey = @"BedroomWall";
@interface ViewController ()
@property (nonatomic, assign) NSInteger bedroomFloorID;
@property (nonatomic, assign) NSInteger bedroomWallID;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.bedroomFloorID = 101;
self.bedroomWallID = 101;
// Do any additional setup after loading the view, typically from a nib.
[self loadGameData];
}
- (void)loadGameData {
// getting path to GameDAta.plist
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths firstObject];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"GameData.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
//check if file exists
if (![fileManager fileExistsAtPath:path]) {
NSLog(@"path doesn't exist. plist file will be copied to the path.");
// If it doesn't, copy it from the default file in the Bundle
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"GameData" ofType:@"plist"];
if (bundlePath) {
NSLog(@"file exists in the main bundle.");
// NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:bundlePath];
NSDictionary *resultDictionary = [NSDictionary dictionaryWithContentsOfFile:bundlePath];
NSLog(@"Bundle GameData.plist file is --> %@", [resultDictionary description]);
// copy dictionary from main bundle to document directory path
[fileManager copyItemAtPath:bundlePath toPath:path error:nil];
NSLog(@"plist file is copied from main bundle to document directory");
} else {
NSLog(@"GameData.plist not found in main bundle. Please, make sure it is part of the bundle.");
}
// use this to delete file from documents directory
// [fileManager removeItemAtPath:path error:nil];
}
// store plist file, at documents directory, in dictionary
NSDictionary *resultDictionary = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"Loaded GameData.plist file at Documents Directory is --> %@", [resultDictionary description]);
// NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfFile:path];
if (resultDictionary) {
// load items from plist file
self.bedroomFloorID = [[resultDictionary objectForKey:BedroomFloorKey] integerValue];
self.bedroomWallID = [[resultDictionary objectForKey:BedbroomWallkey] integerValue];
} else {
NSLog(@"WARNING: Couldn't create dictionary from GameData.plist at Documents Dicretory! Default values will be used!");
}
}
- (void)saveGameData {
// getting path to GameDAta.plist
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths firstObject];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"GameData.plist"];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:@"XInitializerItem" forKey:@"DoNotEverChangeMe"];
// saving values
[dict setObject:[NSNumber numberWithInteger:self.bedroomFloorID] forKey:BedroomFloorKey];
[dict setObject:[NSNumber numberWithInteger:self.bedroomWallID] forKey:BedbroomWallkey];
// writing to GameData.plist
[dict writeToFile:path atomically:YES];
NSDictionary *resultDictionary = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"Saved GameData.plist file in Documents Direcotry is --> %@", [resultDictionary description]);
}
- (IBAction)SaveButtonTapped:(id)sender {
//change bedroomFloorID variable value
self.bedroomFloorID = 999;
//save the plist with the changes
[self saveGameData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment