从NSString中删除HTML会导致问题

从NSString中删除HTML会导致问题,html,objective-c,Html,Objective C,我制作了一个RSS阅读器,我正在解析描述,但是描述中有HTML标记,所以我使用以下方法创建了一个NSString类别来删除标记: - (NSString *)stripTags:(NSString *)str { NSMutableString *html = [NSMutableString stringWithCapacity:[str length]]; NSScanner *scanner = [NSScanner scannerWithString:str]; scanner.cha

我制作了一个RSS阅读器,我正在解析描述,但是描述中有HTML标记,所以我使用以下方法创建了一个NSString类别来删除标记:

- (NSString *)stripTags:(NSString *)str
{
NSMutableString *html = [NSMutableString stringWithCapacity:[str length]];

NSScanner *scanner = [NSScanner scannerWithString:str];
scanner.charactersToBeSkipped = NULL;
[scanner setCharactersToBeSkipped:nil];
NSString *tempText = nil;

while (![scanner isAtEnd])
{
    [scanner scanUpToString:@"<" intoString:&tempText];

    if (tempText != nil)
        [html appendString:tempText];

    [scanner scanUpToString:@">" intoString:NULL];

    if (![scanner isAtEnd])
        [scanner setScanLocation:[scanner scanLocation] + 1];

    tempText = nil;
}

return html;
}

这似乎是因为您使用第一行设置了原始字符串的容量

NSMutableString *html = [NSMutableString stringWithCapacity:[str length]];
我相信这设置了一个最小容量大小,虽然它可以自由地增长超过这个大小,但它不能缩小到您的新大小

一个快速解决方案可能是将长度最初设置为1或小于最小预期文本的值

例如:

NSMutableString *html = [NSMutableString stringWithCapacity:1];
您可以简单地使用:

NSMutableString *html = [[NSMutableString alloc] init];
我尝试了这个代码,它的工作原理和它应该的完全一样

NSMutableString *html = [[NSMutableString alloc] init];
NSLog(@"Before: %u", [html length]);

NSScanner *scanner = [NSScanner scannerWithString:@"<a>this is a test</a>"];
scanner.charactersToBeSkipped = NULL;
[scanner setCharactersToBeSkipped:nil];
NSString *tempText = nil;

while (![scanner isAtEnd])
{
    [scanner scanUpToString:@"<" intoString:&tempText];

    if (tempText != nil)
        [html appendString:tempText];

    [scanner scanUpToString:@">" intoString:NULL];

    if (![scanner isAtEnd])
        [scanner setScanLocation:[scanner scanLocation] + 1];

    tempText = nil;
}

NSLog(@"After: %u", [html length]);
NSMutableString*html=[[NSMutableString alloc]init];
NSLog(@“在:%u之前,[html长度]);
NSScanner*scanner=[NSScanner scannerWithString:@“这是一个测试”];
scanner.characterstobeskiped=NULL;
[扫描仪设置字符stobeskiped:nil];
NSString*TENTEXT=nil;
而(![scanner isattend])
{
[扫描程序扫描字符串:@“intoString:NULL];
如果(![scanner isattend])
[扫描仪设置扫描位置:[扫描仪扫描位置]+1];
TENTEXT=零;
}
NSLog(@“在:%u之后,[html长度]);

XCode是一个IDE。Objective-C是一种语言。实际上Xcode是IDE的名称。。。不是XCode;)我想知道写下你们都喜欢的应用程序的名字有什么困难;)接得好
[NSMutableString]
我想也可以。@Benny我已经尝试过你的解决方案,但我的描述仍然存在同样的问题。我试过
NSMutableString*html=[NSMutableString]
NSMutableString*html=[nsmutablestringwithcapacity:1]
NSMutableString*html=[[NSMutableString alloc]init]
添加建议的代码后,在NSMutableString*html之后放置一个NSLog[html length],然后在返回html之前放置另一个NSLog[html length]。发布您的输出。这将帮助我们确定该错误是否发生在函数内部。@Benny由于此警告,它不会将该错误输出到日志:format Strings未使用数据参数,因为length是int,请尝试使用NSLog(“长度%u,[html长度])
NSMutableString *html = [[NSMutableString alloc] init];
NSLog(@"Before: %u", [html length]);

NSScanner *scanner = [NSScanner scannerWithString:@"<a>this is a test</a>"];
scanner.charactersToBeSkipped = NULL;
[scanner setCharactersToBeSkipped:nil];
NSString *tempText = nil;

while (![scanner isAtEnd])
{
    [scanner scanUpToString:@"<" intoString:&tempText];

    if (tempText != nil)
        [html appendString:tempText];

    [scanner scanUpToString:@">" intoString:NULL];

    if (![scanner isAtEnd])
        [scanner setScanLocation:[scanner scanLocation] + 1];

    tempText = nil;
}

NSLog(@"After: %u", [html length]);