【问题标题】:How to stop overwriting data如何停止覆盖数据
【发布时间】:2013-05-05 07:25:09
【问题描述】:

我正在尝试在我的 iOS 应用中保存一些数据。我使用以下代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"yourPlist.plist"];

//inserting data
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:categoryField.text forKey:@"Category"];
[dict setValue:nameField.text forKey:@"Name"];
[dict setValue:eventField.text forKey:@"Event"];

NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:dict];
[arr writeToFile: path atomically:YES];


//retrieving data
NSMutableArray *savedStock = [[NSMutableArray alloc] initWithContentsOfFile: path];
for (NSDictionary *dict in savedStock) {
     NSLog(@"my Note : %@",dict);
}

但是 NSLog 只向我显示了最后的数据...我想我正在覆盖这里..我不明白为什么!

如何在不覆盖的情况下继续将字典保存在数组中?有什么想法吗?

【问题讨论】:

  • 我已经给你之前的question 提供了一个answer,它负责覆盖。以防你没有检查。
  • 我知道并且我正在强烈重新考虑将已接受的答案更改为您的答案。那段代码似乎更容易,这就是我选择它的原因。
  • 我不是要你接受它,另一个很简单,因为它没有处理所有覆盖,易用性,存储封装等。我建议你尝试一下.您可以将 plist 保存为字典,它将模型对象转换为字典并在回调时执行相反的操作。

标签: ios objective-c cocoa-touch dictionary overwrite


【解决方案1】:

由于您正在制作模型对象,因此最好在其中包含 save、remove、findAll、findByUniqueId 类型的逻辑。将使模型对象的工作变得非常简单。

@interface Note : NSObject

@property (nonatomic, copy) NSString *category;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *event;

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

/*Find all saved notes*/
+ (NSArray *)savedNotes;

/*Saved current note*/
- (void)save;

/*Removes note from plist*/
- (void)remove;

保存笔记

Note *note = [Note new];
note.category = ...
note.name = ...
note.event = ...

[note save];

从保存的列表中删除

//Find the reference to the note you want to delete
Note *note = self.savedNotes[index];
[note remove];

查找所有已保存的笔记

NSArray *savedNotes = [Note savedNotes];

Source Code

【讨论】:

    【解决方案2】:

    替换:

    NSMutableArray *arr = [[NSMutableArray alloc] init];
    [arr addObject:dict];
    [arr writeToFile: path atomically:YES];
    

    与:

    NSMutableArray *arr = [[NSMutableArray alloc] initWithContentsOfFile: path];
    [arr addObject:dict];
    [arr writeToFile: path atomically:YES];
    

    【讨论】:

    • 我做到了。现在它不打印任何东西。连最后一个都没有。
    【解决方案3】:

    您需要先读入数据,然后将新字典追加到旧字典。所以先读取文件,然后追加新字典,然后保存。

    完整代码:

    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setValue:categoryField.text forKey:@"Category"];
    [dict setValue:nameField.text forKey:@"Name"];
    [dict setValue:eventField.text forKey:@"Event"];
    
    [self writeDictionary:dict];
    
    - (void)writeDictionary:(NSDictionary *)dict
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *documentsDirectory = [paths objectAtIndex:0]; 
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"yourPlist.plist"];
    
        NSMutableArray *savedStock = [[NSMutableArray alloc] initWithContentsOfFile: path];
        if(!savedStock) {
            savedStock = [[[NSMutableArray alloc] initiWithCapacity:1];
        }
        [savedStock addObject:dict];
    
        // see what';s there now
        for (NSDictionary *dict in savedStock) {
             NSLog(@"my Note : %@",dict);
        }
        // now save out
        [savedStock writeToFile:path atomically:YES];
    }
    

    【讨论】:

    • 谢谢大卫。我知道这就是我的问题背后的逻辑,但我这里有一个编码问题,我认为不是架构问题。
    猜你喜欢
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 2020-07-16
    • 1970-01-01
    • 2015-03-29
    • 2015-02-03
    相关资源
    最近更新 更多