2012-03-12 6 views
0

私はplistにデータをコピーするのにこのコードを試しましたが、追加する場合には機能しません。いずれにしてもデータを追加するには良い方法がありますか?
NSMutableDictionary * nameDictionary = [NSMutableDictionary dictionary];plistファイルにデータを追加するのに手伝ってもらえますか?

NSString *plistPath=[[NSBundle mainBundle] pathForResource:@"listNew" ofType:@"plist"]; 
[nameDictionary setValue:nameEntered.text forKey:@"name"]; 
[nameDictionary setValue:company.text forKey:@"company"]; 
[nameDictionary setValue:designation.text forKey:@"designation"]; 

// create dictionary with values in UITextFields 
NSMutableArray *plist = [NSMutableArray arrayWithContentsOfFile:plistPath]; 
if (plist == nil) plist = [NSMutableArray array]; 
[plist addObject:nameDictionary]; 
[plist writeToFile:plistPath atomically:YES]; 
UIAlertView *tellErr = [[UIAlertView alloc] initWithTitle:title message:@"succsefully created" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
[tellErr show]; 
[email protected]""; 
[email protected]""; 
[email protected]""; 
+0

あなたはそれが機能していないと言ったら、クラッシュしていますか、アレイには最後に追加する予定の辞書がありませんか?私が見ていることから、上記のコードは上手く見えます。 – Sam

+0

初めてplistからデータ全体を削除しようとしたとき、またはリストを表示してから挿入しようとするとクラッシュする – ashwin19

+0

質問に答えがある場合は緑色のチェック –

答えて

0

私はのために行くだろう:...等...

NSString *plistPath=[[NSBundle mainBundle] pathForResource:@"listNew" ofType:@"plist"]; 
NSMutableDictionary *plist = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath]; 
if (plist == nil) plist = [[NSMutableDictionary alloc] init]; 
[plist setObject:nameEntered.text forKey:@"name"]; 
[plist setObject:company.text forKey:@"company"]; 
[plist setObject:designation.text forKey:@"designation"]; 
[plist writeToFile:plistPath atomically:YES]; 

もちろん

を、これはあなたが達成したい正確に何に依存します。

+0

これで古いデータが上書きされます。新しく入力したデータを古いデータに追加したい – ashwin19

+0

これは古いデータを上書きしません。しかし、1分ほどで修正するエラーがあります。 –

+0

いいえ、エラーは修正されています。 dictionayWithContentsOfFileをallocして辞書を初期化し、ファイルの内容を辞書に読み込みます。次にplist値は辞書にあります。次に、3つの新しい値が追加され、その後、完全な辞書が同じファイル名に書き込まれます。トリックをする必要があります。 –

0

iOSのアプリケーションメインバンドルに書き込むことはできません。プロパティリストファイルを書き込み可能なディレクトリにコピーする必要があります。それを読んだり、変更したり、書き換えたりすることができます。

+0

あなたは、ドキュメントディレクトリまたはそのようなフォルダの中からplistファイルを取得するように指示していますか? – ashwin19

+0

上記のコードセグメントはplistを読み込みます。おそらくメインバンドルからのデータがあらかじめ格納されています。しかし、変更されたバージョンのファイルをメインバンドルに保存することはできません。これをドキュメントディレクトリなどに保存する必要があります。アプリは、アプリのサポートディレクトリまたはdocsディレクトリに、修正が必要なリソースを最初の起動時にコピーするのが一般的です。 – FluffulousChimp

0

あなたが必要とするコードはここにあります。私は、既存のiOSプログラムに

static BOOL dicIsNULL = YES; 

.... 

//initialize the variable 
dicIsNULL = YES; 


//READ FILE 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *stringsPlistPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"listNew.plist"]; 

     dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:stringsPlistPath]; 

     //Check if this dic has values 
     for (id key in dictionary) 
     { 
      //NSLog(@"listNew.plist file has values"); 
      dicIsNULL = NO; 
      break; 
     } 

今すぐdictonaryが既に作成またはないされていたに応じて、次の操作を行いそもそも辞書にどんな情報があるかどうかを確認するために

最初のチェックを、これを使用しています。辞書に値を追加してください。これは単なるサンプルコードです。

if (dicIsNULL == YES) 
     { 
      //NSLog(@"dictionary is NULL"); 

      dictionary = [[NSMutableDictionary alloc] init]; 
      dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:tempArray, infoKeyStr, textTitleField1.text, titleKeyStr, userSelectedCatStr, catKeyStr, nil]; 

     } 

     if (dicIsNULL == NO) 
     { 
      //NSLog(@"dictionary is NOT null"); 

      dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:stringsPlistPath]; 
      [dictionary setObject: tempArray forKey: infoKeyStr]; 
      [dictionary setObject: textTitleField1.text forKey: titleKeyStr]; 
      [dictionary setObject: userSelectedCatStr forKey: catKeyStr]; 
     } 

これがあなたを助けてくれることを願っています。

関連する問題