NSDictionary对象消失

时间:2016-02-22 23:55:00

标签: objective-c

我的应用使用plist在会话之间保存数据。我使用以下三种方法来保存和读取plist文件中的数据。但是,我遇到了一个很大的问题。

当我保存数据时,所有12个键/值对都在NSMutableDictionary中完美设置(我在调试器中检查了这一点,所有12对肯定都在那里)。但是,当我读取数据时,只显示了11对。丢失的第12个键/值对是“Tab”,一个NSMutableArray。所有其他数据类型都是NSString或NSNumber。

Tab的属性声明:

@property (strong, nonatomic) NSMutableArray *tab;

代码:

- (NSString *)dataFileName
{
    NSError *err = nil;
    NSURL *dir = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSAllDomainsMask appropriateForURL:nil create:YES error:&err];


    NSString *path = [[dir path] stringByAppendingString:@"/employeeListData.plist"];


    return path; //path has not been declared yet
}


- (void)saveDataToFile
{

NSMutableArray *a = [NSMutableArray array];
for (int c = 0; c < [self.employeeList count]; c++) {
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    id r = [self.employeeList objectAtIndex:c];
    [dictionary setObject:[r username] forKey:@"username"];
    [dictionary setObject:[r passWord] forKey:@"passWord"];
    [dictionary setObject:[r employeeName] forKey:@"employeeName"];
    [dictionary setObject:[r grade] forKey:@"grade"];
    [dictionary setObject:[r email] forKey:@"email"];
    [dictionary setObject:[r phone] forKey:@"phone"];
    [dictionary setObject:[r freePeriods] forKey:@"freePeriods"];
    [dictionary setObject:[r committee] forKey:@"committee"];
    [dictionary setValue:[NSNumber numberWithBool:[r hasKey]] forKey:@"hasKey"];
    [dictionary setObject:[r hours] forKey:@"hours"];
    [dictionary setObject:[NSArray arrayWithArray:[r tab]] forKey:@"tab"];
    float num = [r tabTotal];
    NSNumber *floatObject = [NSNumber numberWithFloat:num];
    [dictionary setObject:floatObject forKey:@"tabTotal"];
    [a addObject:dictionary];
    }

    [a writeToFile:[self dataFileName] atomically:YES];
}

- (void)readDataFromFile
 {
[self createEmployeeList];
NSArray *tempArray = [NSArray arrayWithContentsOfFile:[self dataFileName]];
for (int d = 0; d < [tempArray count]; d++) {
    Employee *person = [[Employee alloc] init];
    NSDictionary *dict = [tempArray objectAtIndex:d];
    [person setUsername:[dict objectForKey:@"username"]];
    [person setPassWord:[dict objectForKey:@"passWord"]];
    [person setEmployeeName:[dict objectForKey:@"employeeName"]];
    [person setGrade:[dict objectForKey:@"grade"]];
    [person setEmail:[dict objectForKey:@"email"]];
    [person setPhone:[dict objectForKey:@"phone"]];
    [person setFreePeriods:[dict objectForKey:@"freePeriods"]];
    [person setCommittee:[dict objectForKey:@"committee"]];
    [person setHasKey:[[dict valueForKey:@"hasKey"] boolValue]];
    [person setHours:[dict objectForKey:@"hours"]];
    [person setTab:[[dict objectForKey:@"tab"] mutableCopy]];
    NSNumber *floatNumber = dict[@"tabTotal"];
    float floatValue = floatNumber.floatValue;
    [person setTabTotal:floatValue];
    [self.employeeList addObject:person];
    }


}

1 个答案:

答案 0 :(得分:0)

问题是由于我的自定义对象数组。我在保存方法中添加了以下代码:

NSMutableArray *x = [NSMutableArray array];
    for (int y = 0; y < [[r tab]count]; y++) {
        NSMutableDictionary *items = [NSMutableDictionary dictionary];
        id z = [[r tab] objectAtIndex:y];
        [items setObject:[z itemName] forKey:@"tabName"];
        [items setObject:[NSNumber numberWithInt:[z price]] forKey:@"tabPrice"];
        [x addObject:items];
    }
    [dictionary setObject:x forKey:@"tab"];

我的阅读方法中的以下代码:

NSMutableArray *things = [[NSMutableArray alloc] init];
    things = dict[@"tab"];
    person.tab = [[NSMutableArray alloc] init];
    for (int g = 0; g < things.count; g++) {
        TabItem *item = [[TabItem alloc] init];
        NSDictionary *ion = [things objectAtIndex:g];
        [item setItemName:[ion objectForKey:@"tabName"]];
        [item setPrice:[[ion objectForKey:@"tabPrice"] intValue]];
        [person.tab addObject:item];
    }

由于自定义对象的数组无法存储在NSDictionaries中,因此我使用较小版本的较大版本的save和read方法来完成任务。完整的更新代码可以在下面找到。

- (void)saveDataToFile
{

NSMutableArray *a = [NSMutableArray array];
for (int c = 0; c < [self.employeeList count]; c++) {
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    id r = [self.employeeList objectAtIndex:c];
    [dictionary setObject:[r username] forKey:@"username"];
    [dictionary setObject:[r passWord] forKey:@"passWord"];
    [dictionary setObject:[r employeeName] forKey:@"employeeName"];
    [dictionary setObject:[r grade] forKey:@"grade"];
    [dictionary setObject:[r email] forKey:@"email"];
    [dictionary setObject:[r phone] forKey:@"phone"];
    [dictionary setObject:[r freePeriods] forKey:@"freePeriods"];
    [dictionary setObject:[r committee] forKey:@"committee"];
    [dictionary setValue:[NSNumber numberWithBool:[r hasKey]] forKey:@"hasKey"];
    [dictionary setObject:[r hours] forKey:@"hours"];
    NSMutableArray *x = [NSMutableArray array];
    for (int y = 0; y < [[r tab]count]; y++) {
        NSMutableDictionary *items = [NSMutableDictionary dictionary];
        id z = [[r tab] objectAtIndex:y];
        [items setObject:[z itemName] forKey:@"tabName"];
        [items setObject:[NSNumber numberWithInt:[z price]] forKey:@"tabPrice"];
        [x addObject:items];
    }
    [dictionary setObject:x forKey:@"tab"];
    [dictionary setObject:[NSNumber numberWithInt:[r tabTotal]] forKey:@"tabTotal"];
    [a addObject:dictionary];

    }

[a writeToFile:[self dataFileName] atomically:YES];
}

- (void)readDataFromFile
{
[self createEmployeeList];
NSArray *tempArray = [NSArray arrayWithContentsOfFile:[self dataFileName]];
for (int d = 0; d < [tempArray count]; d++) {
    Employee *person = [[Employee alloc] init];
    NSDictionary *dict = [tempArray objectAtIndex:d];
    [person setUsername:[dict objectForKey:@"username"]];
    [person setPassWord:[dict objectForKey:@"passWord"]];
    [person setEmployeeName:[dict objectForKey:@"employeeName"]];
    [person setGrade:[dict objectForKey:@"grade"]];
    [person setEmail:[dict objectForKey:@"email"]];
    [person setPhone:[dict objectForKey:@"phone"]];
    [person setFreePeriods:[dict objectForKey:@"freePeriods"]];
    [person setCommittee:[dict objectForKey:@"committee"]];
    [person setHasKey:[[dict valueForKey:@"hasKey"] boolValue]];
    [person setHours:[dict objectForKey:@"hours"]];
    NSMutableArray *things = [[NSMutableArray alloc] init];
    things = dict[@"tab"];
    person.tab = [[NSMutableArray alloc] init];
    for (int g = 0; g < things.count; g++) {
        TabItem *item = [[TabItem alloc] init];
        NSDictionary *ion = [things objectAtIndex:g];
        [item setItemName:[ion objectForKey:@"tabName"]];
        [item setPrice:[[ion objectForKey:@"tabPrice"] intValue]];
        [person.tab addObject:item];
    }
    [person setTabTotal:[[dict objectForKey:@"tabTotal"] intValue]];
    [self.employeeList addObject:person];
    }


}
相关问题