尝试在for循环中调用PList数据(iOS) [英] Trying To Call PList data in a for Loop (iOS)

查看:196
本文介绍了尝试在for循环中调用PList数据(iOS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我相信是一个简单的问题,一个简单的答案,我不知道我的生活。我创建了一个plist,我试图用信息填充一个MapView注解。我已经通过一些教程,这是我决定去(代码循环)的代码。



它没有显示在地图上的引脚所以我决定在整个代码的不同位置设置NSLogs来查找问题,如果你看下面的代码,它会注销read1而不是read2,所以在循环开始的某个地方实际上是一个问题。



$ p $ - (void)viewDidLoad
{
NSMutableArray * annotations = [[NSMutableArray alloc] init];
NSString * path = [[NSBundle mainBundle] pathForResource:@MillersStoresofType:@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];


$ / code $ / pre

我似乎有一些简单的语法错误所以我想这可能是这方面的一些事情。感谢您的帮助!

-

编辑



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

 <?xml version =1.0encoding = UTF-8\" >?; 
<!DOCTYPE plist PUBLIC - // Apple // DTD PLIST 1.0 // ENhttp://www.apple.com/DTDs/PropertyList-1.0.dtd\">
< plist version =1.0>
< dict>
< key>根< / key>
< array>
< dict>
< key>标题< / key>
< string>邻里市场#90< / string>
< key>地址< / key>
< string> 5117 Mudd Tavern Rd。,Thornburg,VA 22580< / string>
< key>纬度< / key>
< real> 38.133069< / real>
< key>经度< / key>
< real> -77.512423< / real>
< key>电话号码< / key>
< string>(757)874-5806< / string>
< / dict>
< dict>
< key>标题< / key>
< string>邻里市场#89< / string>
< key>地址< / key>
< string> 4902 Hampton Blvd.,Norfolk,VA 23508< / string>
< key>纬度< / key>
< real> 36.887794< / real>
< key>经度< / key>
< real> -76.302544< / real>
< key>电话号码< / key>
< string>(757)440-7792< / string>
< / dict>
< dict>
< key>标题< / key>
< string>邻里市场#88< / string>
< key>地址< / key>
< string> 7601 Sunset Crossing Dr.,Gainesville,VA 20155< / string>
< key>纬度< / key>
< real> 38.792053< / real>
< key>经度< / key>
< real> -77.62994399999999< / real>
< key>电话号码< / key>
< string>(571)248-8580< / string>
< / dict>


解决方案

plist实际上包含顶级字典一个关键的根,它的值是你感兴趣的数组。所以你必须把这个文件读入 NSDictionary ,然后检索它里面的数组。












$ b $ NS $ initWithContentsOfFile:路径];

用这些:

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


I have, what I believe to be a simple question with a simple answer that I cannot figure out for the life of me. I've created a plist that I'm trying to use to populate a MapView annotation with info. I've gone through some tutorials and this is the code I've decided to go with (running a loop).

It wasn't showing the pins on the map so I decided to set up NSLogs at different point throughout the code to find the problem and if you look at the code below, it logs out "read1" but not "read2" so it's actually a problem somewhere in the beginning of the loop.

- (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];
    }
}

I seem to have simple syntax errors for time to time so I'm thinking it may be something along those lines. Thanks for the help!

--

EDIT

Here's some of the XML code of my PList file:

<?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>

解决方案

The plist actually contains a dictionary at the top level with one key "Root" the value of which is the array you are interested in. So you have to read the file into an NSDictionary and then retrieve the array inside it.

Replace this line:

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

with these:

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

这篇关于尝试在for循环中调用PList数据(iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆