Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSMutableArray添加和保存对象_Ios_Nsmutablearray_Plist - Fatal编程技术网

Ios NSMutableArray添加和保存对象

Ios NSMutableArray添加和保存对象,ios,nsmutablearray,plist,Ios,Nsmutablearray,Plist,iOS新手,关于在NSMutable数组中添加对象以及在应用程序内的另一个视图中显示数组,我遇到了一个问题。数据在TableView的其他视图上显示良好,但当我向数组中添加另一项(使用下面的代码)时,它只是替换了其中的内容,而不是添加到数组中 - (void) postArray { tableData = [[NSMutableArray alloc] initWithCapacity:10]; tableData = [[NSMutableArray alloc] initWithObjec

iOS新手,关于在NSMutable数组中添加对象以及在应用程序内的另一个视图中显示数组,我遇到了一个问题。数据在TableView的其他视图上显示良好,但当我向数组中添加另一项(使用下面的代码)时,它只是替换了其中的内容,而不是添加到数组中

- (void) postArray {
tableData = [[NSMutableArray alloc] initWithCapacity:10];
tableData = [[NSMutableArray alloc] initWithObjects: nil];
[tableData addObject:favShot]; }

-(NSString *) saveFilePath {

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, TRUE);
return [[path objectAtIndex:0] stringByAppendingPathComponent:@"savefile.plist"]; }

- (void) viewWillDisappear:(BOOL)animated: (UIApplication *) application  {
NSArray *values = [[NSArray alloc] initWithObjects:tableData, nil];
[values writeToFile:[self saveFilePath] atomically: TRUE]; }

- (void)viewDidLoad  {

tableData = [[NSMutableArray alloc] initWithObjects: nil];

NSString *myPath = [self saveFilePath];

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath];

if (fileExists)
{

    NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
    tableData = [values mutable copy];
}

UIApplication *myApp = [UIApplication sharedApplication];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(applicationDidEnterBackground:)
                                             name:UIApplicationDidEnterBackgroundNotification
                                           object:myApp];

[super viewDidLoad];  }

谢谢。

每次调用
postArray
时,您都在创建
NSMutableArray
的实例,然后将其丢弃(如果不使用ARC,则会泄漏)。然后您将创建另一个
NSMutableArray
实例。然后将对象(favShot)添加到第二个实例中

下次调用
postArray
时,它将丢弃旧阵列并创建两个新阵列

您要做的是在创建控制器实例或加载视图时创建
tableData
实例。然后,不要设置
tableData=…
,因为这样会丢弃旧实例。只需添加和删除对象

编辑

- (void) postArray {
[tableData addObject:favShot];
}

- (NSString *)saveFilePath {
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, TRUE);
return [[path objectAtIndex:0] stringByAppendingPathComponent:@"savefile.plist"];
}

- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];

[tableData writeToFile:[self saveFilePath] atomically: TRUE];
}

- (void)viewDidLoad  {
NSString *myPath = [self saveFilePath];

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath];

if (fileExists)
{
    tableData = [[NSMutableArray alloc] initWithContentsOfFile:myPath];
} else {
    tableData = [[NSMutableArray alloc] initWithObjects: nil];
}

UIApplication *myApp = [UIApplication sharedApplication];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(applicationDidEnterBackground:)
                                             name:UIApplicationDidEnterBackgroundNotification
                                           object:myApp];

[super viewDidLoad];
}
试一试


所以我把它移到了viewDidLoad,我可以添加多个,这是一个巨大的改进。但是,当我返回到以前的视图并重新输入再次添加到阵列的视图时,以前保存的阵列将消失。我在这里遗漏了什么?通过移动到上一个视图(可能在导航控制器中显示),您将丢弃该视图(和控制器),然后在返回时重新创建它们。您可能需要执行一些操作,例如将数组内容写入viewDidLoad中的磁盘将消失,然后在viewDidLoad中重新加载。如果我将其保存到.plist中,为什么当我返回该视图时它不可用?我使用了.plist在视图之间保存字符串,这似乎是可行的。如果您只是在ApplicationIdentinterBackground上触发保存,那么在视图之间移动时它不会保存。您的saveFilePath方法也不是很安全,如果数组中的对象比您预期的少,则可能会崩溃。我实现了保存到ViewWillInviser。然而,我被困在如何把它放在viewDidLoad中。我使用了与viewDidUnload中相同的代码,但它不起作用。谢谢。谢谢回复,当我从一个视图移动到另一个视图时,它会保存,但不会解决我的保存问题
- (void) viewWillDisappear:(BOOL)animated: (UIApplication *) application 
{
    NSString *filePath = [self saveFilePath];

    NSMutableArray *savedArray = [NSMutableArray arrayWithContentsOfFile:filePath];
    if (!savedArray) savedArray = [NSMutableArray array];

    [savedArray addObject:tableData];

    [savedArray writeToFile:filePath atomically:YES];
}