Reputation: 107
So I#m trying to save an array to a file. This should be done in the following code but by calling the addProjectsObject
method, the program crashes withe the following error code:
***** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteData count]: unrecognized selector sent to instance 0x7eeb800'**
I'post the code that i think is relevant for the Problem, also i need to mention that there is another file stored in the same filePath, and it works perfectly. It occurred to me that the path might be the problem, but since both files have different names that shouldn't be the problem right?
-(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];
}
Upvotes: 0
Views: 631
Reputation: 90117
NSData
is NOT NSArray
.
[[NSMutableArray alloc]initWithArray:]
expects an instance of NSArray.
[NSData dataWithContentsOfFile:fileAtPath]
returns an instance of NSData.
Those two won't work together.
If projects
is an NSMutableArray
simply use this:
NSMutableArray* temp = [NSMutableArray arrayWithContentsOfFile:fileAtPath];
The rest of the code can be stripped down too. There is no need to check if a file exist, or even create a file just to overwrite it two lines later.
This will work too:
- (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];
}
Upvotes: 1
Reputation: 8947
this is cz you are assigning inappropriate pointer, you assigned an NSConcreteData object pointer to NSMutablearray and tried to call some array method due to which this occured
Upvotes: 1