NSDictionary值替换而不是在Plist中添加

时间:2013-03-25 04:36:33

标签: objective-c nsarray plist nsdictionary

嗨我试图将值从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看起来像

enter image description here

我该如何解决这个问题

enter image description here

3 个答案:

答案 0 :(得分: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目录并从那里使用..

答案 1 :(得分:1)

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

以下行将分别存储两个带有键@“usernotes1”和@“usernotes2”的usernote

[final setValue:notes forKey:@"usernotes1"];
[final setValue:notes forKey:@"usernotes2"];

答案 2 :(得分:1)

Plist结构如下所示

enter image description here

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

Demo Source Code