0

だから私は配列をファイルに保存しようとしています。これは次のコードで行う必要がありますが、 addProjectsObjectメソッドを呼び出すと、次のエラー コードでプログラムがクラッシュします。

***** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteData count]: unrecognized selector sent to instance 0x7eeb800'**

問題に関連すると思われるコードを投稿します。また、同じ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];

}
4

2 に答える 2

1

これは、不適切なポインターを割り当てている cz です。NSConcreteData オブジェクト ポインターを NSMutablearray に割り当て、何らかの配列メソッドを呼び出そうとしましたが、これが原因で発生しました

于 2012-05-16T08:24:35.093 に答える
1

NSDataではありません NSArray

[[NSMutableArray alloc]initWithArray:]NSArray のインスタンスが必要です。
[NSData dataWithContentsOfFile:fileAtPath]NSData のインスタンスを返します。
あの二人はうまくいかない。

単純にこれを使用するprojects場合:NSMutableArray

NSMutableArray* temp = [NSMutableArray arrayWithContentsOfFile:fileAtPath];

残りのコードも削除できます。ファイルが存在するかどうかを確認したり、2 行後に上書きするためだけにファイルを作成したりする必要はありません。

これも機能します:

- (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];
}
于 2012-05-16T09:30:36.987 に答える