gpt4 book ai didi

iphone - 将数据从 plist 输出到 NSString

转载 作者:行者123 更新时间:2023-11-28 18:44:09 25 4
gpt4 key购买 nike

我正在从 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>

并使用以下代码在 UILabel 中输出数据:

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

myLabel.text = myString;

输出显示为 ( 7, 8)

有谁知道我应该如何去除括号和逗号。并将值分开,这样我就可以显示类似 first: 7 second: 8

的内容

最佳答案

例子:

<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>

代码:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path]; 

for(id key in dict) {
NSLog(@"First:%@ Second:%@", [[dict objectForKey:key] objectAtIndex:0], [[dict objectForKey:key] objectAtIndex:1]);
}

输出:

First:7 Second:8
First:8 Second:7


编辑 (回复评论)

首先,您需要确定 NSMutableDictionary 中的键。
加载 .plist 文件,遍历并将键添加到 NSMutableArray

NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path]; 
NSMutableArray *keys = [[NSMutableArray alloc] init];
for(id key in dict) [keys addObject:[NSString stringWithFormat:@"%@",key]];

确保数组可以被 UIPickerView 访问。
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;
}

最后,像这样更新 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]];
}

UIPickerView 现在包含以下行:

Feb 7, 2000
Jan 27, 2001

例如,当选择“2001 年 1 月 27 日”时,UILabel 显示:

First:8 Second:7

关于iphone - 将数据从 plist 输出到 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6889298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com