0

在这里,我正在遍历一个 NSDictionary 对象数组(在 EXCELLENT MapQuest 方向 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];
}

directionOutput 的内容(它是一个 NSMutableString)包含 的所有值[leg valueForKey:@"narrative"],用括号括起来,后跟一个连字符,然后是所有带括号的值[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
)
4

1 回答 1

1

[leg valueForKey:@"narrative"]似乎返回一个字符串数组,而 [leg valueForKey:@"distance"]返回一个NSNumbers 数组。中只有一个对象legs

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

快速枚举对您的输出没有任何好处。

于 2010-05-18T17:50:34.670 回答