LoginSignup
4
3

More than 5 years have passed since last update.

Plistの書き換え

Last updated at Posted at 2013-09-23

PlistはほぼXMLのような構造なので、最初はPythonで適当にスクリプト書いたが、書き換え後に初めの2行がうまく出力されなかったり、色々と脆そうな感じだった。

初めはPlistのことがよく分かっていなかったので、上記の方法を取ったが、結局Objective-Cのオブジェクトをシリアライズしたものだと分かった。もちろんシリアライズ・デシリアライズはObjective-Cで簡単に書けるので、そちらの形式で改めて書いてみたら、やはりこちらの方がずっと簡単で堅牢な感じになった。

// 引数として与えられる想定
NSString *filePath = [NSString stringWithUTF8String:argv[1]];
NSString* key = [NSString stringWithUTF8String:argv[2]];
NSString* value = [NSString stringWithUTF8String:argv[3]];
// plistファイルをデシリアライズしてさらに変更可能なDictionaryに
NSMutableDictionary* plistDict = [[NSDictionary alloc] initWithContentsOfFile:filePath].mutableCopy;
NSString* originalValue = plistDict[key];
NSLog(@"filePath: %@. key: %@ ( %@ + %@ )", filePath, key, originalValue, value);
// Suffixを付与
plistDict[key] = [originalValue stringByAppendingString:value];
// 元のファイル名で上書き
[plistDict writeToFile:filePath atomically:YES];

コマンドラインプロジェクトとして作成した、一式はこちら。
PlistLib
~Libというわりにすごくスペシフィックな感じだけど、他に用途増えたらもう少し拡充させていくかも。

4
3
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3