Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.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_Objective C - Fatal编程技术网

Ios 保存和加载NSMutableArray

Ios 保存和加载NSMutableArray,ios,objective-c,Ios,Objective C,我希望我的应用程序能够正常工作,这样当用户按下“保存”按钮时,将保存从文本字段输入的NSMutableArray字符串(该数组称为“名称”)。当然,我也希望能够在关闭/重新打开应用程序时加载NSMutableArray 现在我的保存按钮是IBAction“保存”。因此,在我的实施文件中,我有: - (IBAction)save:(id)sender { NSArray *paths = NSSearchPathforDirectoriesInDomains(NSDocumentDir

我希望我的应用程序能够正常工作,这样当用户按下“保存”按钮时,将保存从文本字段输入的NSMutableArray字符串(该数组称为“名称”)。当然,我也希望能够在关闭/重新打开应用程序时加载NSMutableArray

现在我的保存按钮是IBAction“保存”。因此,在我的实施文件中,我有:

- (IBAction)save:(id)sender
{ 
    NSArray *paths =  NSSearchPathforDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0]; 
    NSString *fileName = [NSString stringWithFormat:@"%@/myArray", docDir]; 
    [NSKeyedArchiver archiveRootObject:names toFile:fileName];
}
首先,这看起来应该有效吗?因为当我试图按下保存按钮时,我的应用程序多次崩溃。第二,我在iAction中创建文件路径对吗?或者我应该在其他地方创建它(例如在viewDidLoad下)

第二,如何以及在何处加载保存的NSMutableArray(“名称”)


非常感谢

首先,正如Zaph所说,您缺少了一些概念,您确定
名称
对象符合NSCoding协议吗?
如果是,则这些代码行应该起作用:

- (IBAction)save:(id)sender {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = paths.firstObject;
    // Add to the path a new directory just to keep things ordered
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"AppData"];
    // If the directory doesn't exist it creates one
    if (![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory]) {
        NSError *error;
        [[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:&error];
        if (error) {
            NSLog(@"Error creating directory %@",error.localizedDescription);
        }
    }
    NSString * path = [documentsDirectory stringByAppendingPathComponent:@"names.arc"] ;
    //Check if the file alredy exist, if does we remove it or the file manager will trigger an error (it doesn't overwrite automatically)
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        [[NSFileManager defaultManager] removeItemAtPath:path error:nil] ;
    }
    // Pass the object that you want to archive, if it is a collection the object inside must support NSCoding protocol
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:<#ObjectsConformToNSCoding#>];
    // Saving to the path
    [data writeToFile:path atomically:YES];

}
-(iAction)保存:(id)发送方{
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*documentsDirectory=path.firstObject;
//向路径中添加一个新目录,以保持事物有序
documentsDirectory=[documentsDirectory stringByAppendingPathComponent:@“AppData”];
//如果目录不存在,它将创建一个
如果(![[NSFileManager defaultManager]文件存在路径:documentsDirectory]){
n错误*错误;
[[NSFileManager defaultManager]createDirectoryAtPath:DocumentsDirectorywithIntermediateDirectory:YES属性:无错误:&error];
如果(错误){
NSLog(@“创建目录时出错%@”,错误.localizedDescription);
}
}
NSString*path=[DocumentsDirectoryStringByAppendingPathComponent:@“names.arc”];
//检查文件是否存在,我们是否删除它,或者文件管理器将触发错误(它不会自动覆盖)
if([[NSFileManager defaultManager]fileExistsAtPath:path]){
[[NSFileManager defaultManager]removeItemAtPath:路径错误:nil];
}
//传递要存档的对象,如果该对象是集合,则其中的对象必须支持NSCoding协议
NSData*data=[NSKeyedArchivedDataWithRootObject:];
//保存到路径
[数据写入文件:原子路径:是];
}

[更新]
正如Zaph所指出的,所有错误都应该正确处理,否则可能会发生不好的事情。

消息:“线程1:breakpoint 3.1”表示程序遇到了断点。通过单击Xcode编辑器中的行号,可以意外或故意设置断点。断点用于停止程序执行,以允许W开发人员在该点检查执行状态。然后程序就可以恢复了

断点不是崩溃

此外,行号上方的蓝色箭头是断点指示器。您可以通过将其向右拖动将其删除,或通过单击(它将变暗)或继续(菜单调试:继续)将其禁用

要查看所有断点,请查看断点导航器:Command-7


阅读Xcode文档WRT调试是值得的。

1。处理数据应该在数据模型类中,它不是UI的一部分。2.什么是
名称
,它完全符合NSCODE?3. +10用于使用
NSKeyedArchiver
而非
NSUserDefaults
@Zaph“names”是我创建的NSMutableArray(代码中的其他地方)的名称,它保存用户输入的各种字符串。这是一个有点愚蠢的问题,但只是为了澄清:所以我不应该在iAction下使用这些代码?实际的崩溃消息/堆栈跟踪是什么?代码乍一看是正常的,但可能是无法写入文件,可能是数组中的某个对象实际上不是字符串,等等。错误应该会有所帮助。也同意@zaph->在其他地方序列化(但这不是错误的原因)。(可能save甚至没有连接到这个方法?@KirkSpaziani它说的是“线程1:在断点处停止”要添加文件名,请使用stringByAppendingPathComponent。例如,如果原始文件的最后一个字符是/,这将起作用。请检查代码,如果目录不存在且无法创建,则无法正确处理错误。最好使用
-writeToFile:options:error:
,因为它有一个
error
参数。@Zaph你说得对,但我写这篇文章只是为了“示例”目的。非常感谢!但我还是在“NSArray*路径”上找到了一个断点line@Julia“NSArray*路径”行的断点是什么意思?如果是断点,则检查该行上是否设置了断点。在
NSArray*path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)行中几乎不可能失败。可能是因为您从NSSearchPath获得了一个空数组,这在我的一生中从未发生过。