Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 解析时如何从xml文件中删除空格_Iphone_Objective C_Ios4 - Fatal编程技术网

Iphone 解析时如何从xml文件中删除空格

Iphone 解析时如何从xml文件中删除空格,iphone,objective-c,ios4,Iphone,Objective C,Ios4,我使用了推荐的代码行: - (void)parser:(NSXMLParser *)parser foundCharacters:(NSMutableString *)string { // save the characters for the current item... if ( [currentElement isEqualToString:@"title"]) { currentNodeContent = [[NSMutableString alloc]init];

我使用了推荐的代码行:

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSMutableString *)string
{
// save the characters for the current item...
if ( [currentElement isEqualToString:@"title"]) 
{
    currentNodeContent = [[NSMutableString alloc]init];
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] > 0) {
        [currentNodeContent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
      }
    NSLog(@"currentnode content...%@",currentNodeContent);    
    [blogtitle addObject:currentNodeContent];
    NSLog(@"%@",blogtitle);
 }

我仍然得到数组中的空值。我猜字符串“currentNodeContent”的初始化应该在其他地方。

在您的
NSXMLParserDelegate
中检查并仅添加非空字符串,例如
[currentNodeContent length]>0

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSMutableString *)string
{
// save the characters for the current item...
if ( [currentElement isEqualToString:@"title"]) 
{
    NSString *newString = [string stringByTrimmingCharactersInSet:[NSCharacterSet    whitespaceAndNewlineCharacterSet]];
    if ([newString length] > 0) 
    {
        [blogtitle addObject:newString];
    }         

    NSLog(@"currentnode content...%@",currentNodeContent);    

    NSLog(@"%@",blogtitle);
 }

只是不要将空字符串添加到数组中。在添加之前检查长度。你应该存储修剪过的字符串,以避免修剪两次。感谢你的提醒Georg,我的意思是检查字符串的长度,我没有想到代码,我感谢你的评论,以便我可以改进我的回答方式….,编辑了答案