objective-c - 尝试分割非常大的字符串

标签 objective-c ios nsstring

Possible Duplicate:
Most memory efficient way to split an NSString in to substrings


我正在尝试拆分20Mb的字符串。我试过使用componentsSeparatedByString,但它消耗太多RAM。我认为这归因于这样的事实,即它拆分了字符串,但也保留了原始字符串的完整性。这意味着该字符串有效地两次存储在内存中(即使我在拆分后立即释放了原始字符串也仍然是一个问题。)

我对Objective C非常陌生。我尝试编写一些代码,将原始字符串中的子字符串添加到找到的字符串数组中,从而将其删除。这个想法是,随着找到的字符串的可变数组变大,原始字符串变小。唯一的问题是它会泄漏内存并崩溃。如果有人可以告诉我我在做什么错,那您真棒!

    NSRange range = [mainHtml rangeOfString:@"<p class=NumberedParagraph>"];
    int counter = 1;

    // locations will == int max if it can't find any more occurances
    while (range.location < [mainHtml length]) {
        NSString *curStr;
        NSRange curStrRange;

        NSRange rangeToSearchIn = NSMakeRange(range.location+1, [mainHtml length] - range.location - 1);
        NSRange nextRange = [mainHtml rangeOfString:@"<p class=NumberedParagraph>" options:NSCaseInsensitiveSearch range:rangeToSearchIn];

        if (nextRange.location > [mainHtml length])
        {
            // This is the last string - get everything up to the end of the file
            curStrRange = NSMakeRange(0, [mainHtml length]);
            curStr = [mainHtml substringFromIndex:range.location];
        } else {
            curStrRange = NSMakeRange(range.location, nextRange.location - range.location);
            curStr = [mainHtml substringWithRange:curStrRange];
        }

        // Remove the substring just processed from the orignal string
        // * it crashes here, normally on the 3rd itteration
        mainHtml = [mainHtml substringFromIndex:curStrRange.location + curStrRange.length];
        range = [mainHtml rangeOfString:@"<p class=NumberedParagraph>"];

        [self.parts addObject:curStr];
    }

最佳答案

我认为@babbidi的想法正确。 mainHtml很大,周围有许多自动发布的副本(每次迭代一个副本)尚未发布。尝试在代码中添加以下@autorelease,以在每个循环结束时释放所有自动释放的对象。如果您未使用Mac OS X 10.7,则只需在主循环外手动创建自动释放池,并在每次迭代中将其释放一次。

NSRange range = [mainHtml rangeOfString:@"<p class=NumberedParagraph>"];
int counter = 1;

// locations will == int max if it can't find any more occurances
while (range.location < [mainHtml length]) {
    @autorelease {
        NSString *curStr;
        NSRange curStrRange;

        NSRange rangeToSearchIn = NSMakeRange(range.location+1, [mainHtml length] - range.location - 1);
        NSRange nextRange = [mainHtml rangeOfString:@"<p class=NumberedParagraph>" options:NSCaseInsensitiveSearch range:rangeToSearchIn];

        if (nextRange.location > [mainHtml length])
        {
            // This is the last string - get everything up to the end of the file
            curStrRange = NSMakeRange(0, [mainHtml length]);
            curStr = [mainHtml substringFromIndex:range.location];
        } else {
            curStrRange = NSMakeRange(range.location, nextRange.location - range.location);
            curStr = [mainHtml substringWithRange:curStrRange];
        }

        // Remove the substring just processed from the orignal string
        // * it crashes here, normally on the 3rd itteration
        mainHtml = [mainHtml substringFromIndex:curStrRange.location + curStrRange.length];
        range = [mainHtml rangeOfString:@"<p class=NumberedParagraph>"];

        [self.parts addObject:curStr];
    }
}

关于objective-c - 尝试分割非常大的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7430781/

相关文章:

ios - 使用 RxSwift 定期更新

objective-c - 疯狂使用 UITextField

ios - 在 iOS 8 中,如何在 la Mail.app 中实现长扫删除手势

ios - 性能差异 : loadNibNamed vs. 以编程方式

ios - View 在屏幕上,但 self.view.window 仍然为零

ios - 两个 ViewControllers 方向

objective-c - 用 "and"替换字符串中的最后一个逗号。 ( Objective-C )

swift - 在 Swift 中旋转 NSString

ios - NSManagedObject 类关系中的 NSPredicate 键

ios - 使用 x-apple-reminder ://to launch the Reminders app in iOS