代码之家  ›  专栏  ›  技术社区  ›  Dan Ray

快速枚举会影响我的文本输出吗?

  •  0
  • Dan Ray  · 技术社区  · 14 年前

    在这里,我将迭代一组nsdictionary对象(在优秀的mapquest directions api的解析JSON响应中)。我想建立一个HTML字符串放入uiWebView。

    我的代码说:

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

    DirectionsOutput的内容(它是NSmutableString)包含的所有值 [leg valueForKey:@"narrative"] ,用parens括起来,后跟连字符,后跟的所有括号值 [leg valueForKey:@"distance"] . 所以我打了一个nslog电话…我也得到了同样的东西!似乎for()正在以某种方式在迭代时对输出值进行批处理,并且只输出一次。

    我如何使它不这样做,而是做我实际想要的,这是一个迭代输出作为我迭代?

    这是nslog得到的。是的,我知道我需要找出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
    )
    
    1 回复  |  直到 14 年前
        1
  •  1
  •   Nikolai Ruhe    14 年前

    [leg valueForKey:@"narrative"] 似乎返回字符串数组,而 [leg valueForKey:@"distance"] 返回的数组 NSNumber S.里面只有一个物体 legs .

    不过,我不明白为什么发布的输出中缺少“<br>”。

    快速枚举对输出没有任何影响。