gpt4 book ai didi

ios - 遍历iOS中plist的字典数组

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

我是 iOS 编程的新手,我正在研究遍历 plist。但是我无法访问标签中的单个记录。也就是说,如果我只想访问“Amul”的详细信息,那么在 if 条件中应该写些什么

下面是我的plist文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>Email Address</key>
<string>ams@asd.com</string>
<key>Name</key>
<string>Amul</string>
<key>Phone Number</key>
<string>984756393</string>
</dict>
<dict>
<key>Email Address</key>
<string>cat@se.com</string>
<key>Name</key>
<string>Anoop</string>
<key>Phone Number</key>
<string>87453726</string>
</dict>
<dict>
<key>Email Address</key>
<string>per@se.com</string>
<key>Name</key>
<string>Bhagesss</string>
<key>Phone Number</key>
<string>7684947</string>
</dict>
</array>
</plist>

我也在读取数组中的所有数据,如下所示

NSArray *array=[[NSArray alloc]initWithContentsOfFile:filePath];


if (i<[array count]) {
NSLog(@"%@", [array objectAtIndex:i]);
NSString *strName=[[NSString alloc]initWithFormat:@"%@",[array
valueForKey:@"Name"]];
NSString *strPhone=[[NSString alloc]initWithFormat:@"%@",[array
valueForKey:@"Phone Number"]];
NSString *strEmail=[[NSString alloc]initWithFormat:@"%@",[array
valueForKey:@"Email Address"]];

lblName.text=strName;
lblPhone.text=strPhone;
lblEmail.text=strEmail;

i++;
}

现在我应该提出什么条件,以便只显示我在文本字段中提供的名称的记录。请帮助...

最佳答案

在 NSString 上使用 isEqualToString 方法。顺便说一句,NSArray 中没有 valueForKey 方法!正如前面建议的答案,使用字典。这是完整的代码。

NSArray *array=[[NSArray alloc]initWithContentsOfFile:filePath];

int i = 0;
while (i<[array count]) {

NSDictionary *dict = [array objectAtIndex:i];

if([[dict objectForKey:@"Name"] isEqualToString:@"Amul"])
{
lblName.text=strName;
lblPhone.text=strPhone;
lblEmail.text=strEmail;
break;
}
i++;
}

关于ios - 遍历iOS中plist的字典数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12472617/

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