Challenge Ch 26 working but could be better: any suggestions


#1

Hello, I’m new to Objective C. I finished the CH 26 Challenge. I need some help NSLogging the output, in particular the bool is not registering correctly, the float needs to be to the second decimal place, and it would be nice if the date only had the year, month and day. I tried %.2f for the float but it doesn’t work cuz the p-list is calling it an object not a float.

[code]
NSMutableArray *property = [[NSMutableArray alloc]init];

    NSMutableDictionary *city;
    
    city = [NSMutableDictionary dictionary];
    [city setObject:@"New York" forKey:@"location"];
    [city setObject:[NSDate dateWithString:@"2016-11-15 12:00:00 +0100"] forKey:@"date"];
    [city setObject:[NSNumber numberWithInt:2] forKey:@"bedRooms"];
    [city setObject:[NSNumber numberWithBool:0]  forKey:@"available"];
    [city setObject:[NSNumber numberWithFloat:1785.49] forKey:@"rent"];
    [property addObject:city];
    
    city = [NSMutableDictionary dictionary];
    [city setObject:@"Scottsdale" forKey:@"location"];
    [city setObject:[NSDate dateWithString:@"2031-06-28 12:00:00 +0100"] forKey:@"date"];
    [city setObject:[NSNumber numberWithInt:3] forKey:@"bedRooms"];
    [city setObject:[NSNumber numberWithBool:0]  forKey:@"available"];
    [city setObject:[NSNumber numberWithFloat:1137.33] forKey:@"rent"];
    [property addObject:city];
    
    city = [NSMutableDictionary dictionary];
    [city setObject:@"Reno" forKey:@"location"];
    [city setObject:[NSDate dateWithString:@"2011-04-23 12:00:00 +0100"] forKey:@"date"];
    [city setObject:[NSNumber numberWithInt:4] forKey:@"bedRooms"];
    [city setObject:[NSNumber numberWithBool:1]  forKey:@"available"];
    [city setObject:[NSNumber numberWithFloat:600.00] forKey:@"rent"];
    [property addObject:city];
    
    city = [NSMutableDictionary dictionary];
    [city setObject:@"Farmington" forKey:@"location"];
    [city setObject:[NSDate dateWithString:@"2011-03-11 12:00:00 +0100"] forKey:@"date"];
    [city setObject:[NSNumber numberWithInt:5] forKey:@"bedRooms"];
    [city setObject:[NSNumber numberWithBool:1]  forKey:@"available"];
    [city setObject:[NSNumber numberWithFloat:729.40] forKey:@"rent"];
    [property addObject:city];
    
    city = [NSMutableDictionary dictionary];
    [city setObject:@"Las Cruces" forKey:@"location"];
    [city setObject:[NSDate dateWithString:@"2010-08-00 12:00:00 +0100"] forKey:@"date"];
    [city setObject:[NSNumber numberWithInt:3] forKey:@"bedRooms"];
    [city setObject:[NSNumber numberWithBool:1]  forKey:@"available"];
    [city setObject:[NSNumber numberWithFloat:875.00] forKey:@"rent"];
    [property addObject:city];
    
    city = [NSMutableDictionary dictionary];
    [city setObject:@"Fort Collins" forKey:@"location"];
    [city setObject:[NSDate dateWithString:@"2015-12-03 12:00:00 +0100"] forKey:@"date"];
    [city setObject:[NSNumber numberWithInt:1] forKey:@"bedRooms"];
    [city setObject:[NSNumber numberWithBool:0]  forKey:@"available"];
    [city setObject:[NSNumber numberWithFloat:375.49] forKey:@"rent"];
    [property addObject:city];
    
    
    
    [property writeToFile:@"/tmp/stocks.plist" atomically:YES];
    
    NSArray *Properties = [NSArray arrayWithContentsOfFile:@"/tmp/stocks.plist"];
    
    for (NSDictionary *d in Properties){
        bool i = [d objectForKey:@"available"];
        if (i == 1){
            NSLog(@"You currently own property in %@, it has been unoccupied since %@. It has %@ rooms at $%@ per month. ",[d objectForKey:@"location"],[d objectForKey:@"date"],[d objectForKey:@"bedRooms"],[d objectForKey:@"rent"]);
        }else if (i == 0){
            NSLog(@"You currently own property in %@, it has been occupied since %@. It has %@ rooms at $%@ per month.  ",[d objectForKey:@"location"],[d objectForKey:@"date"],[d objectForKey:@"bedRooms"],[d objectForKey:@"rent"]);
        }
    }[/code]

The output is

2013-01-30 13:19:23.166 Challenge[17468:303] You currently own property in New York, it has been unoccupied since 2016-11-15 11:00:00 +0000. It has 2 rooms at $1785.489990234375 per month.
2013-01-30 13:19:23.167 Challenge[17468:303] You currently own property in Scottsdale, it has been unoccupied since 2031-06-28 11:00:00 +0000. It has 3 rooms at $1137.329956054688 per month.
2013-01-30 13:19:23.168 Challenge[17468:303] You currently own property in Reno, it has been unoccupied since 2011-04-23 11:00:00 +0000. It has 4 rooms at $600 per month.
2013-01-30 13:19:23.168 Challenge[17468:303] You currently own property in Farmington, it has been unoccupied since 2011-03-11 11:00:00 +0000. It has 5 rooms at $729.4000244140625 per month.
2013-01-30 13:19:23.169 Challenge[17468:303] You currently own property in Las Cruces, it has been unoccupied since 2010-07-31 11:00:00 +0000. It has 3 rooms at $875 per month.
2013-01-30 13:19:23.170 Challenge[17468:303] You currently own property in Fort Collins, it has been unoccupied since 2015-12-03 11:00:00 +0000. It has 1 rooms at $375.489990234375 per month.


#2

When you get the rent out of the property list, you are getting an object of type NSNumber (because you put an NSNumber object in the list).

Now if you remember the fact that NSNumber can respond to floatValue, you can then invoke that method to get the actual value.

Then your log statement would look like this:

NSLog (@"... at $%.2f per month. ", ..., [[d objectForKey:@"rent"] floatValue]);

You can exploit a similar technique with NSDate objects also.


#3

Perfect! That solve all my problems

NSArray *Properties = [NSArray arrayWithContentsOfFile:@"/tmp/stocks.plist"];
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"MMMM-dd-yyyy"];
        
        for (NSDictionary *d in Properties){
            if ([[d objectForKey:@"available"] boolValue] == YES){
                NSLog(@"You currently own a property in %@, it has been unoccupied since %@. It has %@ rooms at $%.2f per month. ",[d objectForKey:@"location"],[dateFormat stringFromDate:[d objectForKey:@"date"]],[d objectForKey:@"bedRooms"],[[d objectForKey:@"rent"] floatValue]);
            }else if ([[d objectForKey:@"available"] boolValue] == NO){
                NSLog(@"You currently own a property in %@, it has been occupied since %@. It has %@ rooms at $%.2f per month.  ",[d objectForKey:@"location"],[dateFormat stringFromDate:[d objectForKey:@"date"]],[d objectForKey:@"bedRooms"],[[d objectForKey:@"rent"] floatValue]);
            }