0

設定されていないプロパティ (plist 内) が削除されないようにすることは可能ですか?

私のコード:

- (NSString*)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}

- (void)store {
    if ([[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]]) {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        [dict setValue:textField.text forKey:@"text"];
        [dict setValue:textField2.text forKey:@"text2"];
        [dict writeToFile:[[self dataFilePath] stringByAppendingPathComponent:fileName] atomically:NO];
    }
}

- (void)load {
    if ([[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]]) {
        NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:[[self dataFilePath] stringByAppendingPathComponent:fileName]];
        textField.text = [dict objectForKey:@"text"];
        textField2.text = [dict objectForKey:@"text2"];
    }
}

行をコメントアウトする[dict setValue:textField2.text forKey:@"text2"];と、プロパティが削除されます。プロパティを上書きすると言うことは可能ですが、存在し続けることはできますか?

マイク

4

2 に答える 2

0

これは私の解決策です:

- (NSString*)dataFilePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return documentsDirectory;
}

- (void)storeDictionary:(NSDictionary*)values {
    if ([[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]]) {
        NSMutableDictionary *dict = [self load];
        NSArray *allKeys = values.allKeys;
        for (int i = 0; i < allKeys.count; i++) {
            [dict setValue:[values objectForKey:[allKeys objectAtIndex:i]] forKey:[allKeys objectAtIndex:i]];
        }
        [dict writeToFile:[[self dataFilePath] stringByAppendingPathComponent:fileName] atomically:NO];
    }
}

- (NSMutableDictionary*)load {
    NSMutableDictionary *dict;
    if ([[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]]) {
        dict = [[NSMutableDictionary alloc] initWithContentsOfFile:[[self dataFilePath] stringByAppendingPathComponent:fileName]];
    }
    return dict;
}

マイク

于 2012-09-11T12:35:14.290 に答える
0

文字列を追加して、次のように plist に保存できます。

 - (void)store {
if ([[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]]) {
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    NSString *strText = [dict objectForKey:@"text"];
    [dict setValue:[strText stringByAppendingString:textField.text] forKey:@"text"];
     NSString *strText2 = [dict objectForKey:@"text2"];
    [dict setValue:[strText2 stringByAppendingString:textField2.text] forKey:@"text2"];
    [dict writeToFile:[[self dataFilePath] stringByAppendingPathComponent:fileName] atomically:YES];
  }
}
于 2012-09-11T11:57:57.803 に答える