0

初始化NSMutableString如下:

 -(NSString*)filterIt:(NSString*)source
{
    temp1= [[NSString alloc] initWithString:[source stringByReplacingOccurrencesOfString:@"rlm;" withString:@""]];
    //NSString *m_temp;
    temp1 = [temp1 stringByReplacingOccurrencesOfString:@"&" withString:@""];
    temp1 = [temp1 stringByReplacingOccurrencesOfString:@"#x" withString:@"&#x"];
    NSRange range = [temp1 rangeOfString:@"&#x"];
    NSRange range1 = NSMakeRange(range.location, 8);
    if (range1.location != NSNotFound) {
        NSString* temp2 = [temp1 stringByReplacingCharactersInRange:range1 withString:@""];
        //[temp1 setString:temp2];
        temp1 = temp2;
        range = [temp1 rangeOfString:@"&#x"];
        while (range.location < [temp1 length]) {
            range1 = NSMakeRange(range.location, 8);
            temp2 = [temp1 stringByReplacingCharactersInRange:range1 withString:@""];
            //[temp1 setString:temp2];
            temp1 = temp2;
            range = [temp1 rangeOfString:@"&#x"];
        }
    }
    //m_temp = [temp1 mutableCopy];
//  [temp1 release];
    return temp1;
}

如果我尝试在 dealloc 方法中释放此字符串并尝试运行应用程序,我的应用程序将崩溃。

请给我一些建议,我该如何发布这个temp1

提前致谢

4

2 回答 2

0

您可以将 Mutable 字符串作为自动释放返回

或者

参考这个...

于 2011-07-18T06:22:20.887 回答
0

我假设您在方法中进行此调用。根据您提供的代码,确保代码片段实际上是:

temp1= [[NSMutableString alloc] initWithString:[source stringByReplacingOccurrencesOfString:@"rlm;" withString:@""]];

我假设您正在调用 stringByReplacingOcurrenceOfString:withString: 到源。

话虽如此,您声称程序在达到“dealloc”时崩溃。这意味着 temp1 在您的代码中被声明为实例变量...如果是这种情况,正确的代码应该是(假设 temp1 是具有保留属性集的声明属性):

self.temp1 = [[NSMutableString alloc] initWithString:[source stringByReplacingOccurrencesOfString:@"rlm;" withString:@""]];

如果 temp1 既不是实例变量也不是属性,您可能希望在方法内部指出 temp1 是 NSMutableString 并返回自动释放的对象。

于 2011-07-18T07:59:11.510 回答