2

こんにちは私はNSdictionaryからPlistに値(ブックID、ページ番号、メモ)を追加しようとしましたが、新しい値が前の値を置き換えるたびに?しかし、plistに辞書を追加するためのコードはplistのすべての値が必要です

NSString *bid=@"95";
NSString *pnum=@"12";
userNotes=[[NSMutableDictionary alloc]init];
[userNotes setValue:userNotesTextview.text forKey:@"notes"];
[userNotes setValue:bid forKey:@"bookid"];
[userNotes setValue:pnum forKey:@"pagenumber"];
userNotesView.hidden=YES;
_background.hidden = YES;
userNotesTextview.text=@"";
[self savingMetaData];
  NSMutableArray *notes=[[NSMutableArray alloc]init];
  [notes addObject:userNotes];

  NSMutableDictionary *final=[[NSMutableDictionary alloc]init];
  [final setValue:notes forKey:@"usernotes"];
  [final writeToFile:metaDataPath atomically:YES];

そして私のplistは次のようになります

ここに画像の説明を入力してください

どうすればこの問題を解決できますか

ここに画像の説明を入力してください

4

3 に答える 3

4

以下のようにplistから既存の配列をフェッチしますが、最初にplistをDocumentsディレクトリ、または以下のように書き込み可能なフォルダにコピーしたことを確認してください。

NSFileManager *fileManager=[NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *docPath=[[paths objectAtIndex:0] stringByAppendingString:@"yourplist.plist"];
BOOL fileExists = [fileManager fileExistsAtPath: docPath];
NSError *error = nil;
if(!fileExists) 
{
    NSString *strSourcePath = [[NSBundle mainBundle] pathForResource:@"yourplist" ofType:@"plist"];

    [fileManager copyItemAtPath:strSourcePath toPath:docPath error:&error];         

}

NSString *path = docPath;
NSMutableDictionary *plistdictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:path];
NSMutableArray *notes=[plistdictionary objectForKey:@"usernotes"];
if(notes==nil){ 
   notes=[[NSMutableArray alloc] init]; 
}
NSString *bid=@"95";
NSString *pnum=@"12";
userNotes=[[NSMutableDictionary alloc]init];
[userNotes setValue:userNotesTextview.text forKey:@"notes"];
[userNotes setValue:bid forKey:@"bookid"];
[userNotes setValue:pnum forKey:@"pagenumber"];
[notes addObject:userNotes];

そしてついに

NSMutableDictionary *final=[[NSMutableDictionary alloc]init];
[final setValue:notes forKey:@"usernotes"];
[final writeToFile:docPath atomically:YES];

注:MainBundleには何も書き込むことができないため、plistをDocumentsディレクトリにコピーして、そこから使用することをお勧めします。

于 2013-03-25T04:43:50.120 に答える
1

plistは一意のキーでのみ値を保存できるためです。同じキーで値を保存しようとすると、古い値が新しい値に置き換えられます。したがって、常に新しいキーで新しい値を保存してください(例:item0、item1、item3など)

次の行には、それぞれキー@"usernotes1"と@"usernotes2"を持つ2つのユーザーノートが格納されます。

[final setValue:notes forKey:@"usernotes1"];
[final setValue:notes forKey:@"usernotes2"];
于 2013-03-25T04:38:52.903 に答える
1

Plistの構造は次のようになります

ここに画像の説明を入力してください

UserNoteモデルクラスを作成できます。

#define kBookID @"bookid"
#define kPageNumber @"pageNumber"
#define kNotes @"notes"

@interface UserNote : NSObject

@property (nonatomic, copy) NSString *bookID;
@property (nonatomic, copy) NSString *pageNumber;
@property (nonatomic, copy) NSString *notes;

- (id)initWithDictionary:(NSDictionary *)dictionary;

+ (NSArray *)savedUserNotes;
- (void)save;

@end

初期化

- (id)initWithDictionary:(NSDictionary *)dictionary
{
    self = [super init];
    if (self)
    {
        self.bookID = dictionary[kBookID];
        self.pageNumber = dictionary[kPageNumber];
        self.notes = dictionary[kNotes];
    }

    return self;
}

ドキュメントディレクトリでplistファイルのドキュメントパスを見つけます。plistファイルがない場合は、新しいファイルを作成してパスを返します。

+ (NSString *)userNotesDocumentPath
{
    NSString *documentsPath  = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"UserNotes.plist"];

    NSFileManager *fileManger = [NSFileManager defaultManager];
    if (![fileManger fileExistsAtPath:documentsPath])
    {
        NSString *bundleResourcePath = [[NSBundle mainBundle]pathForResource:@"UserNotes" ofType:@"plist"];
        NSArray *userNotes = [NSArray arrayWithContentsOfFile:bundleResourcePath];
        [userNotes writeToFile:documentsPath atomically:YES];
    }

    return documentsPath;

}

保存されているすべてのユーザーノートをplistファイルから取得します。

+ (NSArray *)savedUserNotes
{
    NSString *documentsPath = [self userNotesDocumentPath];
    NSArray *savedNotes = [NSArray arrayWithContentsOfFile:documentsPath];
    NSMutableArray *savedUserNotes = [@[] mutableCopy];
    for (NSDictionary *dict in savedNotes) {
        UserNote *note = [[UserNote alloc]initWithDictionary:dict];
        [savedUserNotes addObject:note];
    }

    return savedUserNotes;

}

ユースノートをplistに保存します

- (NSDictionary *)userNoteDictionary
{
    return @{kBookID:self.bookID,kPageNumber:self.pageNumber,kNotes:self.notes};
}

- (void)saveUserNotesToPlist:(NSArray *)userNotes
{
    NSMutableArray *mutableUserNotes = [@[] mutableCopy];
    for (UserNote *note in userNotes) {
        NSDictionary *dict = [note userNoteDictionary];
        [mutableUserNotes addObject:dict];
    }
    NSString *documentsPath  = [UserNote userNotesDocumentPath];
    [mutableUserNotes writeToFile:documentsPath atomically:YES];
}

#pragma mark - Save

- (void)save
{
    NSMutableArray *savedNotes = [[UserNote savedUserNotes] mutableCopy];
    [savedNotes addObject:self];
    [self saveUserNotesToPlist:savedNotes];
}

ユーザーがメモをとるviewControllerで

- (IBAction)saveUserNoteButtonPressed:(UIButton *)button
{
    UserNote *note = [UserNote new];

    note.bookID = @"95";
    note.pageNumber = @"12";
    note.notes = self.userNotesTextview.text;

    [note save];
}

デモソースコード

于 2013-03-25T05:21:40.590 に答える