问题
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>
回答1:
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"];
回答2:
What's the value of anns
? I'm going to guess it's nil
and that's why your loop never starts. Replace your "read1" with:
NSLog(@"anns is: %@", anns);
and see what you get.
Also, why not use fast enumeration for this?
for(NSDictionary *annotation in anns){
//...
}
is cleaner, more concise, and guaranteed to be as-fast-or-faster than your iteration-based for loop.
来源:https://stackoverflow.com/questions/8206761/trying-to-call-plist-data-in-a-for-loop-ios