更新: いくつかのことを考え出し、コードを変更しました。
my を配列に追加するとNSDictionary
、前回追加した以前の辞書が突然置き換えられます。なぜこれが起こっているのかわかりません。plist をデータ ストレージとして使用しています。
次のようなエラー メッセージが表示されます。
スレッド 1: プログラム受信シグナル: "EXC_BAD_ACCESS"。
初期化
-(id)init{
self=[super init];
if(self){
dbArray = [[NSMutableArray alloc] init];
}
return self;
}
新しいアイテムの追加。
-(void)addNewItem:(NSString *)aString
{
// Creates a mutable dictionary with a anonymous string under the NAME key.
NSDictionary *newString = [[NSDictionary alloc] initWithObjectsAndKeys:aString,@"name", nil];
// Adds the new string to empty dbArray.
[dbArray addObject:(newString)];
NSLog(@"[add]:Added anonymous string to dbArray, under name key.");
// Writes the current dbArray (with the dict) to plist and releases retain counts.
[self writeItem];
[newString release];
}
私のデータを表示する方法。
-(void)viewData
{
// View data from the created plist file in the Documents directory.
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:finalPath]) {
self.dbArray = [NSMutableArray arrayWithContentsOfFile:finalPath];
}
else {
self.dbArray = [NSMutableArray array];
}
}