循环遍历包含字典数组的 plist
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让我们假设此代码成功(我们必须假设,因为您似乎没有进行检查以确保)。我们还假设(因为你没有说)“branchList”是当前类中的一个实例变量:
这希望给你留下一个数组。当然,您可以通过...检查以确保它为您留下一个数组(
if (branchList)...
)来消除“希望”。然后,由于“branchList”似乎是一个实例变量,因此您立即将其替换为空数组(使用访问器而不是像上面那样直接设置它):...所以然后您尝试迭代空循环(因此永远不会执行
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:
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):...so then you try to iterate an empty loop (so the
NSLog()
statement is never executed).创建一个空数组,这就是 for 语句要求循环的内容。
删除该语句。
也许这就是您想要的:
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: