Bryan
Bryan

Reputation: 1375

If/Else Statment Breaking for-loop

I'm at a loss right now, and I can't seem to figure out why my simple if/else statments are breaking my for-loops. These two methods save and read data from a plist. When the commented text is commented, everything works as it should. When they are uncommented, data doesn't save (or doesn't read properly).

EDIT: After updating below code to remove [NSNull null] (updated code can be seen below), I'm still having the exact same problem. Any ideas?

Also, "tab" is an NSMutableArray.

- (void)saveDataToFile
{ 
// translate objects in to an nsarray that contains dictionaries so we can write

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 email] forKey:@"email"];
    [dictionary setObject:[r phone] forKey:@"phone"];
    [dictionary setObject:[r hours] forKey:@"hours"];
//        if ([r tab] != nil) {
//        [dictionary setObject:[r tab] forKey:@"tab"];
//    }
    [dictionary setValue:[NSNumber numberWithFloat:[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 setEmail:[dict objectForKey:@"email"]];
    [person setPhone:[dict objectForKey:@"phone"]];
    [person setHours:[dict objectForKey:@"hours"]];
//        if ([dict objectForKey:@"tab"] == nil)  {
//            NSMutableArray *array = [[NSMutableArray alloc] init];
//            [person setTab:array];
//        }else {
//            [person setTab:[dict objectForKey:@"tab"]];
//        }

    [person setTabTotal:[[dict valueForKey:@"tabTotal"] floatValue]];

    [self.employeeList addObject:person];
    }


}

Upvotes: 0

Views: 94

Answers (1)

Beirs
Beirs

Reputation: 568

You can't save NSNull values to a dictionary file. You can only have a limited set of types, like NSString, NSNumber, NSArray, NSDictionary.

When saving, instead of setting a value to NSNull, don't set it at all.

When reading, if objectForKey: returns nil, means no such key was set.

If you really want to store NSNull values, then have a look at this: How to serialize a NSDictionary/NSArray/Property List (plist) which contains NSNull.

Upvotes: 1

Related Questions