Reputation: 291
I am new to iOS programming, I am working on traversing plist. But I can't access a single record in the label. That is if I want to access only the details of "Amul" then what to write in the if condition
Below is my plist file
<?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>[email protected]</string>
<key>Name</key>
<string>Amul</string>
<key>Phone Number</key>
<string>984756393</string>
</dict>
<dict>
<key>Email Address</key>
<string>[email protected]</string>
<key>Name</key>
<string>Anoop</string>
<key>Phone Number</key>
<string>87453726</string>
</dict>
<dict>
<key>Email Address</key>
<string>[email protected]</string>
<key>Name</key>
<string>Bhagesss</string>
<key>Phone Number</key>
<string>7684947</string>
</dict>
</array>
</plist>
Also am reading all the data in an Array as shown below
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++;
}
Now what condition should I raise so that only that record is shown whose name I provide in the textfield. Please help...
Upvotes: 0
Views: 697
Reputation: 7504
Use isEqualToString method on NSString. BTW, you don't have valueForKey method in NSArray! As suggested earlier answer, use dictionary. Here is complete code.
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++;
}
Upvotes: 2
Reputation: 69027
EDIT:
There is a mismatch between the data in the plist and the way you are accessing the resulting data structure, it seems to me.
Try to do this:
if (i < [array count]) {
NSDictionary* dict = [array objectAtIndex:i];
if([[dict valueForKey:@"Name"] isEqualToString:@"Amul"]) {
...
Upvotes: 1