0

このサイトで、同様の問題を抱えている人々と一緒にいくつかの質問を見つけましたが、それでも私のものを解決することはできません. つまりね:

2 つの plist ファイルを含む iOS アプリを作成しています。ユーザーが初めてアプリを開くと、アプリケーションはこれら 2 つのファイルを Documents ディレクトリに移動します。その後、Documents ディレクトリ パス内の各ファイルから読み取ることができるので、このコードが機能することはわかっています。ただし、明らかに不明な理由により、それらのいずれにも書き込むことができません。最初の plist ファイルに書き込むために使用するコードは次のとおりです。

+ (void)insertNewElementWith:(NSArray *)objects andKeys:(NSArray *)keys {
    NSString *filePath = [self returnPathForFirstPlistFile];
    NSMutableDictionary *contentOfFirstPlistFile = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
    [contentOfFirstPlistFile setObject:dictionary forKey:[NSNumber numberWithInt:[contentOfFirstPlistFile count]]];
    [contentOfFirstPlistFile writeToFile:filePath atomically:YES];
    [dictionary release];
}

returnPathForFirstPlistFile ではなくが含まcontentOfFirstPlistFileれているため、機能します。上記のメソッドが数回呼び出されたにもかかわらず、なぜそれが返され続けるのか、誰かが私に説明できますか?{ }null{ }

アップデート

Documents ディレクトリにファイルを配置するために使用するコードを次に示します。placePlistsInDocumentsDirectoryユーザーがアプリからログインしたときにのみ呼び出されるため、これは 1 回だけ発生します。

+ (NSString *)returnPathForFirstPlistFile {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"First.plist"];

    return filePath;
}

+ (NSString *)returnPathForSecondPlistFile {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"%@", [paths objectAtIndex:0]);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Second.plist"];

    return filePath;
}

+ (void)placePlistsInDocumentsDirectory {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *firstPlistPath = [self returnPathForFirstPlistFile];
    NSString *secondPlistPath = [self returnPathForSecondPlistFile];

    if (![fileManager fileExistsAtPath:firstPlistPath]) {
        NSError *error;
        NSString *bundle = [[NSBundle mainBundle] pathForResource:@"First" ofType:@"plist"];
        [fileManager copyItemAtPath:bundle toPath:firstPlistPath error:&error];
    }

    if (![fileManager fileExistsAtPath:secondPlistPath]) {
        NSError *error;
        NSString *bundle = [[NSBundle mainBundle] pathForResource:@"Second" ofType:@"plist"];
        [fileManager copyItemAtPath:bundle toPath:secondPlistPath error:&error];
    }
}
4

1 に答える 1

2
[NSNumber numberWithInt:[contentOfFirstPlistFile count]]

NSNumber オブジェクトを提供します。これはディクショナリの有効なキーではありません。キーは NSString でなければなりません。使用する

[NSString stringWithFormat:@"%d",[contentOfFirstPlistFile count]]

その代わり。ただし、数字を辞書のキーとして使用している場合は、配列を使用してみませんか?

私はこの問題を自分で再現しようとしましたが、それを管理できる唯一の方法は、ルート オブジェクトとして辞書を持たない plist を使用することでした。これにより、nil ではない辞書が返されましたが、変更は許可されませんでした。これをファイルに書き戻すことはできませんでした。以前の質問のいくつかを見ると、以前は配列ベースの plist を使用していたため、これが問題である可能性があります。

于 2011-10-08T17:26:29.710 に答える