1

他のユーザーの同様の問題を調べましたが、コードの問題はまだ見つかりません。

これが問題です。

アプリの起動時に setupDirectory というメソッドを呼び出して、名前用の plist と構成用の plist を 1 つずつ作成 (または必要に応じてアプリ バンドルからコピー) します。

@implementation Data

static NSString * NamePlist;
static NSString * ConfigPlist;

+(void) setupDirectory
{

    //Names
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * dataPath = [paths objectAtIndex:0];
    dataPath = [dataPath stringByAppendingPathComponent:@"NamesPlist.plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath:dataPath]) {
        NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"NamesPlist" ofType:@"plist"];
        [fileManager copyItemAtPath:sourcePath toPath:dataPath error:nil];
    }

    //Config
    NSString *configPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    configPath = [configPath stringByAppendingPathComponent:@"ConfigPlist.plist"];

    if (![fileManager fileExistsAtPath:configPath]) {
        NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"ConfigPlist" ofType:@"plist"];
        [fileManager copyItemAtPath:sourcePath toPath:configPath error:nil];
    }

NamePlist = dataPath;
ConfigPlist = configPath;

}

次に、名前を追加する必要がある場合は、メソッド Add name を呼び出して、plist に名前を追加します。

+(void) addName: (NSString *) name andConfigs: (NSArray *) configs
{
    // Name

    // Get the old content of the plist file
    NSDictionary *oldContent = [NSDictionary dictionaryWithContentsOfFile:Namelist];
    // Copy the old content of the plist file to a new dictionary
    NSMutableDictionary * newContent = [oldContent mutableCopy];
    //Add the new config to the dictionary
    [newContent setObject:[NSNull null] forKey:name];
    // Set the new dictionary as the plist file
    [newContent writeToFile:NamePlist atomically:YES];

    //Configs

    oldContent = [NSDictionary dictionaryWithContentsOfFile:ConfigPlist];
    // Copy the old content of the plist file to a new dictonary
    newContent = [oldContent mutableCopy];
    //Add the new config to the dictionary
    [newContent setObject:configs forKey:name];
    // Set the new dictionary as the plist file
    [newContent writeToFile:ConfigPlist atomically:YES];
}

しかし、plist から情報を取得する必要がある場合、何も取得できないため、書き込みませんでしたか?

+(NSArray *) getAllNames
{
    NSDictionary * names = [NSDictionary dictionaryWithContentsOfFile:NamePlist];
    NSArray * allNames = [(NSArray*) [names allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
    return allNames;
}

GetallNames は空の配列を返します。

この質問がすでに回答されている場合は申し訳ありませんが、私は本当に探しましたが、何が問題なのかまだわかりません

編集1:

writeToFile の戻り値を使用して、何かが失敗したかどうかを確認しました。configsPlist からの writeToFile では YES を取得していますが、NamesPlist からの writeToFile では NO を取得しています。したがって、構成は正常に機能していると思いますが、namesPlistは機能していません

編集2:

デバッガーを使用すると、![fileManager fileExistsAtPath:configPath]!fileManager fileExistsAtPath:dataPath]が返さNOれていることに気付くので、実際にはこれらの if ステートメントには入っていません。

解決

[NSNull null] の代わりに文字列を使用して作成しようとしたところ、動作するようになりました... [NSNull null] を使用して、null オブジェクトを使用して辞書を作成する必要があると考えました。

4

1 に答える 1