2012-05-16 113 views
0

因此,我试图将数组保存到文件。这应该在下面的代码,但通过调用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]; 

} 
+0

你试图在这一点应用程序崩溃究竟调试? – rishi

+0

as Saad说,问题似乎在这一行:NSMutableArray * temp = [[NSMutableArray alloc] initWithArray:[NSData dataWithContentsOfFile:fileAtPath]]; 我似乎错过了数据类型的东西,但我绝对不知道它可能是什么 – user1242094

回答

1
NSData

NOTNSArray

[[NSMutableArray alloc]initWithArray:]需要一个NSArray的实例。
[NSData dataWithContentsOfFile:fileAtPath]返回NSData的一个实例。
这两个不会一起工作。

如果projectsNSMutableArray简单地使用这样的:

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]; 
} 
+0

非常感谢,这个帮助很大,现在我看得更清楚了。 – user1242094

1

这是CZ要分配不当指针,则分配一个NSConcreteData对象指针的NSMutableArray并试图调用一些阵列的方法由于此发生

+0

所以什么是正确的程序?我必须以某种方式转换模数据? – user1242094

+0

[NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray]试试这个会返回一个数组 – Saad

+0

非常感谢您亲切先生 – user1242094