如何在plist ios中搜索特定数据

时间:2014-10-01 03:28:07

标签: ios objective-c plist

你好我相信这是一个简单的问题,我是目标c的新手所以请帮助我。 我在plist中有一个填充数据,我想做一个简单的搜索(uitextfield和uibutton)我将如何制作这个?这是我的plist:

     <plist version="1.0">
     <array>
         <dict>
             <key>ID</key>
             <integer>0</integer>
             <key>title</key>
             <string>Toyota Gen. Santos</string>
             <key>address</key>
             <string>National Hwy., City Heights, General Santos City, South Cotabato</string>
             <key>tel</key>
             <string>(083) 554 2994</string>
             <key>latitude</key>
             <real>6.119086</real>
             <key>longitude</key>
             <real>125.159881</real>
             <key>img</key>
             <string>locator_gmap_marker.png</string>
        </dict>
        ....
    </array>
    </plist>

谢谢!这是我加载plist的方法

    //read plist
    -(NSString *) dataFilePath{
         NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
         NSString *documentDirectory = [path objectAtIndex:0];
         return [documentDirectory stringByAppendingPathComponent:@"locations.plist"];
    }

    - (void)readPlist{
         //read plist
         NSString *filePath = [self dataFilePath];
         if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
         NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
         NSLog(@"%@\n",array);
         }
    }

[self readPlist]将放在veiwDidLoad方法中。输出将在NSLOG上看到 - 我存储在plist中的所有项目。我希望实现的是通过使用UI按钮在UITextfield上搜索特定数据,并在UIlabel上看到。

此代码用于从plist中迭代字典,我从这个How to search in a plist to retrieve a value for a specific key得到它,这是来自事件按钮的代码:

   //event button
   //Read locations details from plist
   NSString *path = [[NSBundle mainBundle] pathForResource:@"locations" ofType:@"plist"];
   NSArray *locations = [NSArray arrayWithContentsOfFile:path];

   for (NSDictionary *row in locations){
        NSNumber *tel = [row objectForKey:@"tel"];
        NSNumber *address = [row objectForKey:@"address"];
        NSString *title = [row objectForKey:@"title"];

        self.titleLbl.text = title;
        self.addrsLbl.text = address;
        self.telLbl.text = tel;
   }

1 个答案:

答案 0 :(得分:1)

这是我在事件按钮中重新制作的代码。谢谢 Bijoy Thangaraj,Tark作为参考,rmaddy帮忙。

How to search in a plist to retrieve a value for a specific key

Search on Arrays inside Plist in Xcode

 - (IBAction)eventSearch:(UIButton *)sender {
     //Read locations details from plist
     NSString *path = [[NSBundle mainBundle] pathForResource:@"locations" ofType:@"plist"];
     NSArray *locations = [NSArray arrayWithContentsOfFile:path];

     //convert string to number
     NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
     [f setNumberStyle:NSNumberFormatterDecimalStyle];
     NSNumber *IDnum = [f numberFromString:self.idTxtField.text];

     for (NSDictionary *row in locations) {

          NSNumber *itemID = [row objectForKey:@"ID"];

          if(IDnum == itemID){
             NSString *tel = [row objectForKey:@"tel"];
             NSString *address = [row objectForKey:@"address"];
             NSString *title = [row objectForKey:@"title"];

             self.titleLbl.text = title;
             self.addLbl.text = address;
             self.telLbl.text = tel;
          }
     }
     NSLog(@"accessed!");
     self.idTxtField.text = @"";
 }