ios - 尝试在 for 循环中调用 PList 数据(iOS)

标签 ios xcode for-loop mkmapview plist

我有,我认为这是一个简单的问题和一个简单的答案,但我一生都无法弄清楚。我创建了一个 plist,我试图用它来填充带有信息的 MapView 注释。我已经阅读了一些教程,这是我决定使用的代码(运行一个循环)。

它没有在 map 上显示引脚,所以我决定在整个代码的不同点设置 NSLogs 以查找问题,如果您查看下面的代码,它会记录“read1”而不是“read2”所以它实际上是循环开头某处的问题。

- (void)viewDidLoad
{
    NSMutableArray *annotations = [[NSMutableArray alloc]init];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"MillersStores" ofType:@"plist"];
    NSMutableArray *anns = [[NSMutableArray alloc] initWithContentsOfFile:path];
    NSLog(@"read1");
    for(int i = 0; i < [anns count]; i++) {
        NSLog(@"read2");
        float realLatitude = [[[anns objectAtIndex:i] objectForKey:@"Latitude"] floatValue];
        float realLongitude = [[[anns objectAtIndex:i] objectForKey:@"Longitude"] floatValue];
        NSLog(@"read3");

        MillersLocations *myAnnotation = [[MillersLocations alloc] init];
        CLLocationCoordinate2D theCoordinate;
        theCoordinate.latitude = realLatitude;
        theCoordinate.longitude = realLongitude;
        myAnnotation.coordinate = theCoordinate;
        myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"Title"];
        myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:@"Address"];
        [mapView addAnnotation:myAnnotation];
        [annotations addObject:myAnnotation];
        [myAnnotation release];
    }
}

我似乎时不时会出现一些简单的语法错误,所以我认为这可能与这些错误有关。感谢您的帮助!

--

编辑

这是我的 PList 文件的一些 XML 代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Root</key>
    <array>
        <dict>
            <key>Title</key>
            <string>Neighborhood Market #90</string>
            <key>Address</key>
            <string>5117 Mudd Tavern Rd., Thornburg, VA 22580</string>
            <key>Latitude</key>
            <real>38.133069</real>
            <key>Longitude</key>
            <real>-77.512423</real>
            <key>Phone Number</key>
            <string>(757) 874-5806</string>
        </dict>
        <dict>
            <key>Title</key>
            <string>Neighborhood Market #89</string>
            <key>Address</key>
            <string>4902 Hampton Blvd., Norfolk, VA 23508</string>
            <key>Latitude</key>
            <real>36.887794</real>
            <key>Longitude</key>
            <real>-76.302544</real>
            <key>Phone Number</key>
            <string>(757) 440-7792</string>
        </dict>
        <dict>
            <key>Title</key>
            <string>Neighborhood Market #88</string>
            <key>Address</key>
            <string>7601 Sunset Crossing Dr., Gainesville, VA 20155</string>
            <key>Latitude</key>
                <real>38.792053</real>
            <key>Longitude</key>
            <real>-77.62994399999999</real>
            <key>Phone Number</key>
            <string>(571) 248-8580</string>
        </dict>

最佳答案

plist 实际上在顶层包含一个字典,其中有一个键“Root”,其值是您感兴趣的数组。因此您必须将文件读入 NSDictionary,然后检索其中的数组。

替换这一行:

NSMutableArray *anns = [[NSMutableArray alloc] initWithContentsOfFile:path];

这些:

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSArray *anns = [dict objectForKey:@"Root"];

关于ios - 尝试在 for 循环中调用 PList 数据(iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8206761/

相关文章:

ios - "Runner"需要配置文件。在 Signing & Capabilities 编辑器中选择配置文件

ios - 工作灯 : get device token

c# - 仅更改所选项目的 TabBar 文本颜色(Xamarin.Forms iOS)

ios - 如何将 iPhone 与外部设备连接?

ios - 在 NSURLSession 中获取值

java - 让计时器与 for 循环一起工作

iOS崩溃报告 "unexpected start state"异常?

ios - 模拟器无法打开

java - 嵌套 For 循环的有效替代方案

javascript - 如何使用数组和 && 条件构建长 If 条件?