Search code examples
iosobjective-cplist

how to search specific data in plist ios


Hello I believe that this is a simple issue, I'm a newbie in objective c so please help me. I have a populated data in plist and i want to make a simple search ( uitextfield and uibutton) how will i make this? Here's my 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>

Thanks! Here's my method for loading 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);
         }
    }

The [self readPlist] will be placed at the veiwDidLoad method. the output will be seen at the NSLOG - all item I have stored in the plist. What i hope to achieved is to get a specific data by searcheing on a UITextfield with a UIbutton and will be seen at a UIlabel.

This code to iterate the dictionaries from the plist i got it from this How to search in a plist to retrieve a value for a specific key, here's the code from event button:

   //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;
   }

Solution

  • Here's the code i remade in event button. Thanks to Bijoy Thangaraj, Tark for reference and rmaddy for helping out.

    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 = @"";
     }