循环遍历包含字典数组的 plist

发布于 2024-12-07 16:10:41 字数 1320 浏览 0 评论 0原文

我有一个 plist,其中包含一个包含三个元素的数组,所有元素都是字典。这些词典各包含四个项目(纬度、经度、标题、副标题)。我想循环遍历每个元素并获取纬度和经度(两者都是字典的属性)。

使用以下代码。

    - (void)loadAnnotations{

    //retrieve path of plist file and populate relevant types with its information
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Places1" ofType:@"plist"];
    branchList = [[NSArray alloc] initWithContentsOfFile:plistPath];

     NSLog(@"hi inside method",branchList.lastObject);

    self.branchList =[NSMutableArray array];
    //code ok until this
   for (NSDictionary *key in branchList)
 {    NSLog(@"hi in loop");

        PlaceFinderAnnotation *placeAnnotations = [[PlaceFinderAnnotation alloc] init];  
        //loop through annotations array, creating parking annotations filled with the information found in the plist

            CLLocationDegrees latitude = [[key valueForKey:@"latitude"]floatValue];
            CLLocationDegrees longitude = [[key valueForKey:@"longitude"]floatValue];
            placeAnnotations.coordinate = CLLocationCoordinate2DMake(latitude, longitude);

            [self.branchList addObject:placeAnnotations];
            [placeAnnotations release]; placeAnnotations= nil;
            objectForKey:@"subtitle"]];

        }
    }

问题是它没有进入循环。这意味着它不会打印出日志命令“hi inside the Looppp”。

I have a plist containing an array with three elements all of which are dictionaries. These dictionaries contain four items each (latitude, longitude, title, subtitle). I want to loop through each of the elements and get the latitude and longitude (both of which are attributes of the dictionary).

The following code is used.

    - (void)loadAnnotations{

    //retrieve path of plist file and populate relevant types with its information
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Places1" ofType:@"plist"];
    branchList = [[NSArray alloc] initWithContentsOfFile:plistPath];

     NSLog(@"hi inside method",branchList.lastObject);

    self.branchList =[NSMutableArray array];
    //code ok until this
   for (NSDictionary *key in branchList)
 {    NSLog(@"hi in loop");

        PlaceFinderAnnotation *placeAnnotations = [[PlaceFinderAnnotation alloc] init];  
        //loop through annotations array, creating parking annotations filled with the information found in the plist

            CLLocationDegrees latitude = [[key valueForKey:@"latitude"]floatValue];
            CLLocationDegrees longitude = [[key valueForKey:@"longitude"]floatValue];
            placeAnnotations.coordinate = CLLocationCoordinate2DMake(latitude, longitude);

            [self.branchList addObject:placeAnnotations];
            [placeAnnotations release]; placeAnnotations= nil;
            objectForKey:@"subtitle"]];

        }
    }

The problem is it doesnt go into the loop. meaning it doesnt print out the log command "hi inside the looppp".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可是我不能没有你 2024-12-14 16:10:41

让我们假设此代码成功(我们必须假设,因为您似乎没有进行检查以确保)。我们还假设(因为你没有说)“branchList”是当前类中的一个实例变量:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Places1" ofType:@"plist"];
branchList = [[NSArray alloc] initWithContentsOfFile:plistPath];

这希望给你留下一个数组。当然,您可以通过...检查以确保它为您留下一个数组( if (branchList)... )来消除“希望”。然后,由于“branchList”似乎是一个实例变量,因此您立即将其替换为空数组(使用访问器而不是像上面那样直接设置它):

self.branchList =[NSMutableArray array];

...所以然后您尝试迭代空循环(因此永远不会执行 NSLog() 语句)。

Let's assume this code succeeded (we have to assume because you don't appear to be checking to make sure). Let's also assume (because you don't say) "branchList" is an instance variable in the current class:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Places1" ofType:@"plist"];
branchList = [[NSArray alloc] initWithContentsOfFile:plistPath];

This hopefully leaves you with an array. You could of course eliminate the "hopefully" by ... checking to make sure it leaves you with an array ( if (branchList)... ). Then, since "branchList" seems to be an instance variable, you immediately blow it away by replacing it with an empty array (using an accessor rather than setting it directly as you did above):

self.branchList =[NSMutableArray array];

...so then you try to iterate an empty loop (so the NSLog() statement is never executed).

欢你一世 2024-12-14 16:10:41
self.branchList =[NSMutableArray array];

创建一个空数组,这就是 for 语句要求循环的内容。

删除该语句。

也许这就是您想要的:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Places1" ofType:@"plist"];
self.branchList = [NSArray arrayWithContentsOfFile:plistPath];
NSLog(@"hi inside method",branchList.lastObject);

for (NSDictionary *key in self.branchList) {
    NSLog(@"hi inside loopppp");
self.branchList =[NSMutableArray array];

creates an empty array and that is what the for statement is asked to loop through.

Delete that statement.

Perhaps this is what you want:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Places1" ofType:@"plist"];
self.branchList = [NSArray arrayWithContentsOfFile:plistPath];
NSLog(@"hi inside method",branchList.lastObject);

for (NSDictionary *key in self.branchList) {
    NSLog(@"hi inside loopppp");
醉酒的小男人 2024-12-14 16:10:41
if([branchlist count]){
    for (NSDictionary *key in self.branchList) {
           NSLog(@"Key item in branchlist %@",key);
    }
}else{
     NSLog(@"There is no items in branchlist");
}
if([branchlist count]){
    for (NSDictionary *key in self.branchList) {
           NSLog(@"Key item in branchlist %@",key);
    }
}else{
     NSLog(@"There is no items in branchlist");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文