2011-12-09 7 views
3

私はiPhone開発の新人です。実行時にplistを作成するサンプルObjective-Cコードがあるかどうかを知りたいのですが、実行時にplistを簡単に作成できるように、データの形式を知りたいと思います。iOS SDKを使用して実行時にplistを作成しますか?

+0

plistはxmlファイルです。任意のテキストエディタで表示できます – progrmr

答えて

2

のplistファイルは、主キー/値の方法でシリアル化されたオブジェクトを格納するためのMac OS Xで使用されています。プロパティリストファイルには複数の種類があります。 ASCII、XMLおよびバイナリです。あなたのケースでは、あなたのサーバーはxml形式でデータを送信する必要があります。サーバーからデータを受け取った後、実行時にplistを生成できます。以下のコードを使用して.plistファイルにデータを書き込むことができます。

- (void)writeToPlist{ 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"WebData.plist"]; 
NSArray *dataToSave = [dataToSave writeToFile:filePath atomically:YES]; 
} 

link1link2link3link4thisとサーバ側のためにこれらのリンクを参照してください。

+1

これは理論的に質問に答えることができますが、リンクされた記事の重要な部分を回答に含め、[参考用リンク](http://meta.stackexchange.com/q/8259)を提供してください。そうしないと、リンク腐敗の危険があります。 – Kev

9

NSDictionaryと非常にシンプル:

#define DOCUMENTS_PATH [NSSearchPathForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES) objectAtIndex:0] 

NSDictionary *myDictionary = [self parseWebResult]; 

// note that myDictionary must only contain values of string, 
// int, bool, array (once again containing only the same types), 
// and other primitive types (I believe NSDates are valid too). 
[myDictionary writeToFile:[DOCUMENTS_PATH stringByAppendingPathComponent:@"myDict.plist"] atomically:YES]; 

// reading in the dictionary 
myDictionary = [NSDictionary dictionaryWithContentsOfFile:[DOCUMENTS_PATH stringByAppendingPathComponent:@"myDict.plist"]]; 
1
// Get the full path of file in NSDocuments 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"MyData.plist"]; 

// Save data to file 
NSArray *dataToSave = /* put data in it */; 
[dataToSave writeToFile:filePath atomically:YES]; 
+0

NSDictionaryオブジェクトでも動作します。 – Niko

関連する問題