Is Fast Enumeration messing with my text output?

Posted by Dan Ray on Stack Overflow See other posts from Stack Overflow or by Dan Ray
Published on 2010-05-18T17:19:21Z Indexed on 2010/05/18 17:40 UTC
Read the original article Hit count: 344

Here I am iterating through an array of NSDictionary objects (inside the parsed JSON response of the EXCELLENT MapQuest directions API). I want to build up an HTML string to put into a UIWebView.

My code says:

for (NSDictionary *leg in legs ) {
    NSString *thisLeg = [NSString stringWithFormat:@"<br>%@ - %@", [leg valueForKey:@"narrative"], [leg valueForKey:@"distance"]];
    NSLog(@"This leg's string is %@", thisLeg);
    [directionsOutput appendString:thisLeg];
}

The content of directionsOutput (which is an NSMutableString) contains ALL the values for [leg valueForKey:@"narrative"], wrapped up in parens, followed by a hyphen, followed by all the parenthesized values for [leg valueForKey:@"distance"]. So I put in that NSLog call... and I get the same thing there! It appears that the for() is somehow batching up our output values as we iterate, and putting out the output only once.

How do I make it not do this but instead do what I actually want, which is an iterative output as I iterate?

Here's what NSLog gets. Yes, I know I need to figure out NSNumberFormatter. ;-)

This leg's string is (
    "Start out going NORTH on INFINITE LOOP.",
    "Turn LEFT to stay on INFINITE LOOP.",
    "Turn RIGHT onto N DE ANZA BLVD.",
    "Merge onto I-280 S toward SAN JOSE.",
    "Merge onto CA-87 S via EXIT 3A.",
    "Take the exit on the LEFT.",
    "Merge onto CA-85 S via EXIT 1A on the LEFT toward GILROY.",
    "Merge onto US-101 S via EXIT 1A on the LEFT toward LOS ANGELES.",
    "Take the CA-152 E/10TH ST exit, EXIT 356.",
    "Turn LEFT onto CA-152/E 10TH ST/PACHECO PASS HWY. Continue to follow CA-152/PACHECO PASS HWY.",
    "Turn SLIGHT RIGHT.",
    "Turn SLIGHT RIGHT onto PACHECO PASS HWY/CA-152 E. Continue to follow CA-152 E.",
    "Merge onto I-5 S toward LOS ANGELES.",
    "Take the CA-46 exit, EXIT 278, toward LOST HILLS/WASCO.",
    "Turn LEFT onto CA-46/PASO ROBLES HWY. Continue to follow CA-46.",
    "Merge onto CA-99 S toward BAKERSFIELD.",
    "Merge onto CA-58 E via EXIT 24 toward TEHACHAPI/MOJAVE.",
    "Merge onto I-15 N via the exit on the LEFT toward I-40/LAS VEGAS.",
    "Keep RIGHT to take I-40 E via EXIT 140A toward NEEDLES (Passing through ARIZONA, NEW MEXICO, TEXAS, OKLAHOMA, and ARKANSAS, then crossing into TENNESSEE).",
    "Merge onto I-40 E via EXIT 12C on the LEFT toward NASHVILLE (Crossing into NORTH CAROLINA).",
    "Merge onto I-40 BR E/US-421 S via EXIT 188 on the LEFT toward WINSTON-SALEM.",
    "Take the CLOVERDALE AVE exit, EXIT 4.",
    "Turn LEFT onto CLOVERDALE AVE SW.",
    "Turn SLIGHT LEFT onto N HAWTHORNE RD.",
    "Turn RIGHT onto W NORTHWEST BLVD.",
    "1047 W NORTHWEST BLVD is on the LEFT."
) - (
    0.0020000000949949026,
    0.07800000160932541,
    0.14000000059604645,
    7.827000141143799,
    5.0329999923706055,
    0.15299999713897705,
    5.050000190734863,
    20.871000289916992,
    0.3050000071525574,
    2.802999973297119,
    0.10199999809265137,
    37.78000259399414,
    124.50700378417969,
    0.3970000147819519,
    25.264001846313477,
    20.475000381469727,
    125.8580093383789,
    4.538000106811523,
    1693.0350341796875,
    628.8970336914062,
    3.7990000247955322,
    0.19099999964237213,
    0.4099999964237213,
    0.257999986410141,
    0.5170000195503235,
    0
)

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone