0

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];

}
rishi
  • 11,779
  • 4
  • 40
  • 59
user1242094
  • 107
  • 2
  • 10
  • have you tried debugging exactly at which point application is crashing? – rishi May 16 '12 at 08:26
  • as The Saad says, the problem seems to be in this line: NSMutableArray* temp = [[NSMutableArray alloc]initWithArray:[NSData dataWithContentsOfFile:fileAtPath]]; i seem to miss something about the datatypes but i have absolutely no idea what it might be – user1242094 May 16 '12 at 08:58

2 Answers2

1

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

Saad
  • 8,857
  • 2
  • 41
  • 51
1

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];
}
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247