试图剥离空格在NSXMLparser foundCharacters方法中不起作用 [英] trying to strip out whitespace doesn't work in NSXMLparser foundCharacters method

查看:82
本文介绍了试图剥离空格在NSXMLparser foundCharacters方法中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析一个xml文件而且我一直试图去除currentElementValue中的空格字符,因为它搞乱了几件事。

I'm parsing an xml file and i've been trying to strip out the whitespace characters in my currentElementValue because it's messing up a couple of things.

I可以在我的输出窗口中看到一些回车和标签存在

I can see in my output window that a couple of carriage returns and tabs are present

(gdb) po string
Keep the arms on the side, and lift your leg.
(gdb) po currentElementValue



        Keep the arms on the side, and lift your leg.
(gdb) 

这是我的foundCharacters函数,我一直在尝试使用stringByTrimmingCharactersInSet遗憾的是没有成功。

This is my foundCharacters function and I've been trying to use stringByTrimmingCharactersInSet unfortunately with no success.

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
    if(!currentElementValue) 
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    else
    {       

        [currentElementValue appendString:string];

        currentElementValue = [currentElementValue stringByTrimmingCharactersInSet:
                               [NSCharacterSet newlineCharacterSet]];       
            NSString *instructions = @"instructions";   
        [directions setValue:string forKey:instructions];  //mm 
        [appDelegate.directions setValue:string forKey:instructions];
        //[appDelegate.directions setObject:string forKey:currentElementValue];
    //  [appDelegate
    }
}

我是得到此错误
***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'尝试使用appendString改变不可变对象:'
这很奇怪,因为我的currentElementValue是一个NSMutableString ..
那么出了什么问题?有没有人有线索或想法?

I've been getting this error *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with appendString:' Which is strange since my currentElementValue is a NSMutableString .. So what's going wrong ? Does anyone have a clue or idea ?

推荐答案

让我们一步一步,找到你的错误,并解决内存泄漏:

Let's step through this, find your bug, and solve a memory leak:

首先,创建一个NSMutableString。大。 (+1保留计数)

First, you create an NSMutableString. Great. (+1 retain count)

然后在NSMutableString上附加另一个字符串。没关系。 (仍为+1保留计数)。

Then you append another string onto your NSMutableString. That's fine. (still +1 retain count).

然后修剪newlineCharacterSet,返回...一个自动释放的NSString。由于此对象与原始对象不同,因此您已泄漏原始对象(因为它具有+1保留计数,并且您不再具有指向它的指针),并且您现在有一个不可变的NSString可以引导。这意味着下次调用此方法时,您将尝试将一个字符串附加到NSString上,这将抛出无法改变不可变对象异常。

Then you trim the newlineCharacterSet, which returns... an autoreleased NSString. Since this object is different from your original object, you've leaked your original object (since it had a +1 retain count and you no longer have a pointer to it), and you now have an immutable NSString to boot. This means that the next time this method gets called, you're going to try to append a string onto an NSString, which will throw the "can't mutate an immutable object" exception.

以下是解决此问题的快捷方法:

Here's the quick way to solve this:

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
    if(!currentElementValue) 
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    else
    {           

        [currentElementValue appendString:string];

        NSString *trimmedString = [currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
        [currentElementValue setString:trimmedString];

        NSString *instructions = @"instructions";       
        [directions setValue:string forKey:instructions];  //mm 
        [appDelegate.directions setValue:string forKey:instructions];
        //[appDelegate.directions setObject:string forKey:currentElementValue];
    //  [appDelegate
    }
}

(保存修剪字符串到另一个变量,然后使用NSMutableString的setString:方法传入内容,但不会丢失指向NSMutableString的指针)

(save the trimmed string to a different variable, then use NSMutableString's setString: method to transfer the contents in, but without losing your pointer to your NSMutableString)

这篇关于试图剥离空格在NSXMLparser foundCharacters方法中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆