UPDATE: Figured out some stuff and changed code.

When I add my NSDictionary to my array it suddenly replaces the previous dictionary I added last time. I don't know why this is happening. I am using a plist as data storage.

I get a error message like this:

Thread 1:Program received signal: "EXC_BAD_ACCESS".

Init

-(id)init{
    self=[super init];
    if(self){
        dbArray = [[NSMutableArray alloc] init];
    }
    return self;
}

Adding a new item.

-(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];
}

My method to view my data.

-(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];
    }
}
有帮助吗?

解决方案

instead this

self.dbArray = [[NSMutableArray alloc] init];

use this

if( nil == self.dbArray ) {
  self.dbArray = [[NSMutableArray alloc] init];
}

UPDATE: (based on provided code)

  • you're using different instances of DataObject class for displaying & saving data. Your content is over-written, because you don't load data from file during initialization of each instance; to fix that fast, you need to implement init method of your DataObject class as below:
- (id)init{
    self = [super init];
    if(self){
      [self viewData];
    }
    return self;
}

the following code from viewDidLoad of ViewController class will crash your application very often:

db = [[DataObject alloc] init];
[db viewData];
[db release];

array = [[NSMutableArray alloc] initWithArray:[db dbArray]];

replace it with

db = [[DataObject alloc] init];
[db viewData];

array = [[NSMutableArray alloc] initWithArray:[db dbArray]];

call [db release] only in dealloc implementation

  • another problem, that you'll probably arise - is updated data is not displayed when you're back to the main screen; to fix that add the following method implementation to your ViewController.m file:
- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];

  [db viewData];
  self.array = [NSMutableArray arrayWithArray: db.dbArray];
  [self.tableView reloadData];
}

also in AddView.m replace the following code

// Dismiss view and reload tableview.
ViewController *vc = [[ViewController alloc] init];
[self dismissModalViewControllerAnimated:YES];
[vc release];

with

// Dismiss view and reload tableview.
[self dismissModalViewControllerAnimated:YES];

Just as advise: see more information about using delegates and passing object instances & copies between objects.

其他提示

I think you are creating a new Array:

self.dbArray = [[NSMutableArray alloc] init];

You should create the dbArray on the viewDidLoad or on the init of your UIViewController (I am assuming you are using this on an UIViewController)

inside your DataObject do the following:

  -(id)init{
    self=[super init];
    if(self){
      self.dbArray = [[NSMutableArray alloc] init];
   }
   return self;
  }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top