开发者

How to extract a string from a dictionary in an array into a different array

hello I have a NSMutableArray iconlocarr. I also have another array containing dictionary data called xmlnodes which looks like this:

{
    nodeChildArray = (
        {
            nodeContent = "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png";
        }
    );
    nodeName = weatherIconUrl;
}

I am trying to add the nodeContent data (icon url) into my iconlocarr array:

[iconlocarr addObject:[[[xmlnodes objectAtIndex:i] objectForKey:@"nodeChildArray"] valueForKey:@"nodeContent"]];

The problem I have is that the above code adds the following data:

(
    {
        nodeContent = "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_b开发者_运维百科lack_low_cloud.png";
    }
)

As well the data I actually want

"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png"

How do I just add the nodeContents data and not the rest? The intention is to pass the data to a NSURL


Are you sure that's what it's adding? From your code, I would expect it to add an array containing the nodeContent string, e.g.

(
    @"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png;
)

The problem here is you're trying to extract a single value from an array (your nodeChildArray value) and you haven't defined exactly how you want to do that. Do you want the first item in the array? The last? A random item? You should figure that out. In any case, you can use

NSArray *ary = [[xmlnodes objectAtIndex:i] objectForKey:@"nodeChildArray"];

to get the nodeChildArray value and then determine how exactly you want to pick which value inside it to extract the contents from.


Try this instead:

iconlocarr addObject:[[[[xmlnodes objectAtIndex:i] objectForKey:@"nodeChildArray"] objectAtIndex:0] objectForKey:@"nodeContent"]];

which consider object "nodeChildArray" as an array, then extract index 0 and then the returned dictionary will contain the "nodeContent".


KVC should handle this easily:

for (id nodeContents in [xmlnodes valueForKeyPath:@"nodeChildArray.nodeContent"])
    [iconlocarr addObjectsFromArray:nodeContents];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜