2011-08-05 19 views
2

私はplistを作成したアプリケーションを開発していますが、データを追加しています。しかし、データが上書きされるたびに前回のデータ失われます。私は、岩、1つの名前を追加すると仮定すると、次回は、岩を追加すると、岩が上書きされた岩、しかし、私が望むのは私のplistは両方の岩と岩などを含める必要があります...私はplistユーザーエントリは....ここplistにデータを動的に追加し、テーブルビューのセルを設定する

-(IBAction) myplist:(id) sender//the data is saved in a plist by clicking on this button 
{ 
NSLog(@"mylist Clicked");  

NSMutableArray *array = [[NSMutableArray alloc] init]; 

[array addObject:searchLabel.text]; 

    // get paths from root direcory 

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 

// get documents path 

NSString *documentsPath = [paths objectAtIndex:0]; 

// get the path to our Data/plist file 

NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"]; 



// This writes the array to a plist file. If this file does not already exist, it creates a new one. 

    [array writeToFile:plistPath atomically: TRUE]; 
} 

答えて

0

私はこれがあなたのコードを少し修正してあなたの目的に役立つと思います。

NSLog(@"mylist Clicked");  
NSMutableArray *array = [[NSMutableArray alloc] init]; 


// get paths from root direcory 

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 

// get documents path 

NSString *documentsPath = [paths objectAtIndex:0]; 

// get the path to our Data/plist file 

NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"]; 


//This copies objects of plist to array if there is one 
[array addObjectsFromArray:[NSArray arrayWithContentsOfFile:plistPath]]; 
[array addObject:searchLabel.text]; 
// This writes the array to a plist file. If this file does not already exist, it creates a new one. 

[array writeToFile:plistPath atomically: TRUE]; 
+0

ありがとうソニーそれは働いて..それまさに私が望んでいたものだった...しかし今、もう一つの問題はシミュレータで働いています。しかし、それをデバイス上で動かすにはどうすればいいですか? – Ranjit

+0

こんにちは、私はplistに3つの値を追加したい場合、私はもう1つ質問したいと思います。そして、ユーザーがそれ以上を追加すると上書きされます...どうすればいいですか? – Ranjit

0

はPLISTにデータを保存するためのシーケンスを使用するようにしてください。..以下の私のコードです。

+0

ありがとうございましたVanya..forあなたの返事..私は以前のコードで何を変更すればいいのですか? – Ranjit

+0

ソニーのソリューションを試しましたか?よく見えます。 – Vanya

+0

こんにちはvanya ...私は試してみました..ソニーのソリューションとそれはうまく動作します..シミュレータ..私はそれがiPhoneのデバイスでも実行することができるように何を変更すべきか知りたいです。 – Ranjit

0

あなたが追加カント

1. 、NSMutableDictionary/NSMutableArrayの

2にPLISTから古いデータを取得するNSMutableDictionary/NSMutableArrayの

3に新しいレコードを追加し、ファイルに書き込めデータをPlistに転送します。あなたは毎回writeToFileをやっているので、plistファイルは書き直されます。したがって、最初に保存されたデータはそこには保存されません。あなたの望みを達成する唯一の他の方法は、plistからデータの配列を取り出すことです。次に、新しいデータオブジェクトを配列に追加します。新たに追加されたアレイを使用してplistファイルをディスクに再度書き込みます。 これが役立つことを願っています。

+0

あなたの返信のためのbooleanBoyありがとう..あなたは私の新しいコードですので、私の上記のコードで行う必要がある変更を教えて、より良いでしょう.. – Ranjit

関連する問題