gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:36:41 25 4
gpt4 key购买 nike

您好,我认为这是一个简单的问题,我是 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/

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