iphone - NSMutableArray 没有获取 XML 值
<p><p>我正在尝试使用 <code>TBXML</code> 解析 XML 文件并将值放入多维 <code>NSMutableArray</code>
但问题是这些值没有被添加到数组中,如果我输出这些值,那么它会返回 <code>null</code> 和 <code>0</code>。</p>
<p>这是我的代码:</p>
<pre><code>- (void)viewDidLoad
{
;
;
// RETURNS 0
NSLog(@"weatherArray count: %d", );
// RETURNS NULL
NSLog(@"weatherArray value: %@", weatherArray);
}
- (void)downloadAndParseXml:(NSString *)xmlUrl
{
// Allocating the array
weatherArray = ;
TBXMLSuccessBlock sBlock = ^(TBXML *tbxmlDocument) {
if (tbxmlDocument.rootXMLElement)
;
};
TBXMLFailureBlock fBlock = ^(TBXML *tbxmlDocument, NSError *error) {
NSLog(@"Failureblock: %@\n%@", , );
};
success:sBlock failure:fBlock];
}
- (void)fillArrayWithXmlContents:(TBXMLElement *)element {
do {
if (element->firstChild)
;
if ([ isEqualToString:@"note"]) {
// Adding values into the multidimensional array
[weatherArray addObject:[NSArray arrayWithObjects:
],
],
],
], NULL]];
// Correct value is getting logged
NSLog(@"weatherArray value: %@", weatherArray);
}
} while ((element = element->nextSibling));
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您可能需要做的是等待进程完成,并且该 block 实际上不知道您在其中做什么,或者我会说内部函数未被 block 捕获。所以使用数组(仅在 block 完成后调用正在使用您的数组的函数。</p>
<p>例子:</p>
<pre><code>TBXMLSuccessBlock s = ^(TBXML *tbxml) {
NSLog(@"yes");
// Do something with TBXML object "tbxml/add it ur array
//Here you call the function which is about to use your array.
};
//Not outside the block.
</code></pre>
<p><strong>编辑</strong>:引用这个==> <a href="https://stackoverflow.com/questions/7853915/how-do-i-avoid-capturing-self-in-blocks-when-implementing-an-api/7854315#7854315" rel="noreferrer noopener nofollow">How do I avoid capturing self in blocks when implementing an API?</a> </p></p>
<p style="font-size: 20px;">关于iphone - NSMutableArray 没有获取 XML 值,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/18504999/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/18504999/
</a>
</p>
页:
[1]