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

标签 ios objective-c plist

您好,我认为这是一个简单的问题,我是 objective c 的新手,所以请帮助我。 我在 plist 中有一个填充数据,我想进行一个简单的搜索(uitextfield 和 uibutton),我该怎么做?这是我的列表:

     <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 中的所有项目。我希望实现的是通过在带有 UIbutton 的 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;
   }

最佳答案

这是我在事件按钮中重新制作的代码。谢谢 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 = @"";
 }

关于ios - 如何在 plist ios 中搜索特定数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26133647/

相关文章:

swift - 将数据从 plist 传递到 DetailView

macos - 没有网络连接时启动 NetworkState

ios - 在 IOS7 中验证更多文本字段

iphone - 将视频+生成的音频写入 AVAssetWriterInput,音频卡顿

ios - 在 TTTAttributedLabel 中,两种不同的文本颜色不起作用

ios - 如何在不破坏用户数据的情况下安装 ipa 更新

iphone - 在新 iPhone 应用程序和新 Rails 3 后端之间移动对象图

ios - Facebook 与 sharekit 共享不链接到我的应用程序

ios - 我的 iOS 应用程序如何为一系列颜色设置动画?

ios - socket.io 官方客户端连接问题?