Iphone 使用NSFilemanager存储NSMutableArray时崩溃
因此,我正在尝试将数组保存到文件中。这应该在以下代码中完成,但通过调用Iphone 使用NSFilemanager存储NSMutableArray时崩溃,iphone,objective-c,xcode,nsmutablearray,nsfilemanager,Iphone,Objective C,Xcode,Nsmutablearray,Nsfilemanager,因此,我正在尝试将数组保存到文件中。这应该在以下代码中完成,但通过调用addProjectsObject方法,程序将崩溃,并返回以下错误代码: ***** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteData count]: unrecognized selector sent to instance 0x7eeb800'** 我将发布我认为与这个问题相
addProjectsObject
方法,程序将崩溃,并返回以下错误代码:
***** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteData count]: unrecognized selector sent to instance 0x7eeb800'**
我将发布我认为与这个问题相关的代码,同时我需要提到的是,在同一个文件路径中存储了另一个文件,它工作得非常完美。我突然想到,路径可能是问题所在,但由于两个文件都有不同的名称,这不应该是问题所在,对吗
-(void) addProjectsObject:(NSString *)newProject{
projects = [self getProjectsFromDisk];
[projects addObject:newProject];
[self saveProjectsToDisk];
}
-(NSMutableArray *) getProjectsFromDisk{
NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* fileName = @"projects";
NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName];
if(![[NSFileManager defaultManager] fileExistsAtPath:fileAtPath])
{
[[NSFileManager defaultManager] createFileAtPath:fileAtPath contents:nil attributes:nil];}
NSMutableArray* temp = [[NSMutableArray alloc]initWithArray:[NSData dataWithContentsOfFile:fileAtPath]];
return temp;
}
-(void) saveProjectsToDisk {
NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* fileName = @"projects";
NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName];
if(![[NSFileManager defaultManager] fileExistsAtPath:fileAtPath])
{
[[NSFileManager defaultManager] createFileAtPath:fileAtPath contents:nil attributes:nil];}
[projects writeToFile:fileAtPath atomically:NO];
}
这是cz您正在分配不适当的指针,您将NSConcretedData对象指针分配给了NSMutablearray,并试图调用某些数组方法,因此发生了这种情况
[[NSMutableArray alloc]initWithArray:][/code>需要一个NSArray实例。
[NSData dataWithContentsOfFile:fileAtPath]
返回NSData的实例。
那两个不能一起工作
如果projects
是一个NSMutableArray
只需使用以下命令:
NSMutableArray* temp = [NSMutableArray arrayWithContentsOfFile:fileAtPath];
代码的其余部分也可以精简。不需要检查文件是否存在,甚至不需要创建文件来在两行之后覆盖它
这也会起作用:
- (NSMutableArray *)projectsFromDisk {
NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* fileName = @"projects";
NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName];
NSMutableArray* temp = [NSMutableArray arrayWithContentsOfFile:fileAtPath];
if (!temp) {
// if file can't be read (does not exist, or invalid format) create an empty array
temp = [NSMutableArray array];
}
return temp;
}
- (void)saveProjectsToDisk {
NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* fileName = @"projects";
NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName];
[projects writeToFile:fileAtPath atomically:NO];
}
您是否尝试过调试应用程序在哪一点崩溃?正如Saad所说,问题似乎出在这一行:NSMutableArray*temp=[[NSMutableArray alloc]initWithArray:[NSData dataWithContentsOfFile:fileAtPath]];我似乎遗漏了一些关于数据类型的信息,但我完全不知道这可能是什么,所以正确的过程是什么?我必须以某种方式转换模具数据吗?[NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray]尝试此操作,它将返回一个数组hahaha。我不是一个先生,jst一个像你这样的开发者:)随时问任何问题。skype:the_Saad1非常感谢,这帮了大忙,我现在看得更清楚了。