因此,我试图将数组保存到文件。这应该在下面的代码,但通过调用addProjectsObject
方法来完成,程序崩溃枝条下面的错误代码:当NSFilemanager存储NSMutableArray时发生崩溃
***** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteData count]: unrecognized selector sent to instance 0x7eeb800'**
I'post,我认为是对有关该问题的代码,我也需要提有另一个文件存储在相同的filePath,它的工作原理是完美的。我想到了路径可能是问题,但是因为这两个文件都有不同的名称,这不应该是问题的关键吗?
-(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];
}
你试图在这一点应用程序崩溃究竟调试? – rishi
as Saad说,问题似乎在这一行:NSMutableArray * temp = [[NSMutableArray alloc] initWithArray:[NSData dataWithContentsOfFile:fileAtPath]]; 我似乎错过了数据类型的东西,但我绝对不知道它可能是什么 – user1242094