Outputting data to an NSString from a plist
I'm extracting some string data from a plist:
<dict>
<key>Feb 7, 2000</key>
<array>
<string>7</string>
<string>8</string>
</array>
<key>Jan 27, 2001</key>
<array>
<string>8<开发者_如何学运维;/string>
<string>7</string>
</array>
and using the following code to output the data in a UILabel:
NSString * myString = [NSString stringWithFormat:@"%@",self.data];
myLabel.text = myString;
the output is being displayed as ( 7, 8)
does anyone know how i might go about removing the parenthesis and comma. and also separating the values so I could display something like first: 7 second: 8
Example:
<dict>
<key>Feb 7, 2000</key>
<array>
<string>7</string>
<string>8</string>
</array>
<key>Jan 27, 2001</key>
<array>
<string>8</string>
<string>7</string>
</array>
</dict>
Code:
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
for(id key in dict) {
NSLog(@"First:%@ Second:%@", [[dict objectForKey:key] objectAtIndex:0], [[dict objectForKey:key] objectAtIndex:1]);
}
Output:
First:7 Second:8
First:8 Second:7
EDIT (response to comment)
First you need to determine the keys within the NSMutableDictionary
.
Load the .plist
file, loop through and add the keys to an NSMutableArray
.
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSMutableArray *keys = [[NSMutableArray alloc] init];
for(id key in dict) [keys addObject:[NSString stringWithFormat:@"%@",key]];
Make sure the array can be accessed by the UIPickerView
.
The UIPickerView
should be able to retrieve the keys like this:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [keys objectAtIndex:row];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [keys count];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
Finally, update the UILabel
like this:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
theLabel.text = [NSString stringWithFormat:@"First:%@ Second:%@",
[[dict objectForKey:[keys objectAtIndex:row]] objectAtIndex:0],
[[dict objectForKey:[keys objectAtIndex:row]] objectAtIndex:1]];
}
The UIPickerView
now contains the following rows:
Feb 7, 2000
Jan 27, 2001
When for example "Jan 27, 2001" is selected, the UILabel
shows:
First:8 Second:7
精彩评论