简体   繁体   English

从plist输出数据到NSString

[英]Outputting data to an NSString from a plist

I'm extracting some string data from a plist: 我正在从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: 并使用以下代码在UILabel中输出数据:

  NSString * myString = [NSString stringWithFormat:@"%@",self.data];

    myLabel.text = myString;

the output is being displayed as ( 7, 8) 输出显示为( 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 并分隔值,这样我可以显示类似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 . 首先,您需要确定NSMutableDictionary的密钥。
Load the .plist file, loop through and add the keys to an NSMutableArray . 加载.plist文件,循环浏览并将密钥添加到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 . 确保可以通过UIPickerView访问该数组。
The UIPickerView should be able to retrieve the keys like this: UIPickerView应该能够像这样检索密钥:

- (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: 最后,像这样更新UILabel

- (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: UIPickerView现在包含以下行:

Feb 7, 2000
Jan 27, 2001

When for example "Jan 27, 2001" is selected, the UILabel shows: 例如,当选择“ 2001年1月27日”时, UILabel显示:

First:8 Second:7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM