2

嗨,我尝试将 NSdictionary 中的值(书号、页码、注释)添加到 Plist,但每次新值替换前一个值时?但我需要 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结构看起来像这样

在此处输入图像描述

您可以创建一个 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;
}

在documents目录中找到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;

}

将 usenote 保存到 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];
}

在用户做笔记的视图控制器中

- (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 回答
1

因为 plist 只能使用唯一键存储值。如果您尝试使用相同的键保存值,它将用新值替换旧值。所以总是用新键保存新值(例如 item0、item1、item3 等)

以下行将分别存储两个带有键 @"usernotes1" 和 @"usernotes2" 的用户笔记

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