将NSString拆分为子字符串的最有效内存方式 [英] Most memory efficient way to split an NSString in to substrings

查看:78
本文介绍了将NSString拆分为子字符串的最有效内存方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

    int start = [html rangeOfString:@"class=WordSection1>"].location + 24;
    int end = [html rangeOfString:@"<div class=\"endofsections\">"].location;
    self.parts = [[NSMutableArray alloc] init];

    NSString* startHtml = [html substringToIndex:start - 1];
    NSString* mainHtml = [html substringWithRange:NSMakeRange(start - 1, end - start - 1)];
    NSString* endHtml = [html substringFromIndex:end];
    // !! At this point we have the string in memory twice
    [html release];

    [self.parts addObject: startHtml];

    NSArray *splitHtml = [mainHtml componentsSeparatedByString:@"<p class=NumberedParagraph>"];
    //[mainHtml release]; <-- this causes bad access errors. Does the split do a copy or does it just create a new set of pointers but use the same memory?

    for(NSString* part in splitHtml){
        if (first){
            [self.parts addObject: part];
            first = NO;
        } else {
            [self.parts addObject: [NSString stringWithFormat:@"<p class=NumberedParagraph>%@", part]];
        }
     }

    [self.parts addObject:endHtml];

与此相关的问题是html约为20Mb.我将其拆分为startHtml,mainHtml和endHtml.拆分后,我再释放html.但是,在此版本之前,所有4个NSString都在内存中,因此该应用使用了额外的40Mb左右.

The issue with this is that html is about 20Mb. I split it in to startHtml, mainHtml and endHtml. After splitting it I then release html. However prior to this release all 4 NSStrings are in memory so the app is using an extra 40Mb or so.

然后我拆分mainHtml并将子字符串分配给一个名为splitHtml的NSArray,这再次意味着它们在内存中存储了两次.我尝试释放mainHtml,但这会导致EXC_BAD_ACCESS错误.

I then split mainHtml and assign the substrings to an NSArray called splitHtml, this again means that they are stored in memory twice. I try to release mainHtml but this causes an EXC_BAD_ACCESS error.

在释放该对象之前,有什么方法可以解决此对象在内存中存储两次的问题吗?

Is there any way to get around this object being stored in memory twice before being released issue?

我计划用一个while循环替换for循环,该循环从splitHtml中删除已处理的NSString. splitHtml为空时,将满足循环条件.这样,随着parts数组消耗更多的内存,splitHtml数组消耗的内存更少.我需要释放每个NSString还是将其删除,并使数组整体上占用更少的内存?

I plan to replace the for loop with a while loop that removes the processed NSStrings from splitHtml. The loop condition will be satisfied when splitHtml is empty. This is so that as the parts array consumes more memory the splitHtml array consumes less memory. Do I need to release each NSString or can I just remove it and have the array consume less memory as a whole?

谢谢

推荐答案

好吧..您不能释放mainHtml,因为它是作为自动释放对象创建的,因此在函数完成后将调用release,并且如果到那时该对象已被释放.

Well.. you can't release mainHtml because it is created as an autorelease object, so release will get called after your function is done and it will crash if the object is already released by then.

您可以尝试创建一个额外的函数来拆分字符串并返回数组,也许有一个自己的自动释放池,您可以在运行该函数后释放该释放池以确保释放了字符串.

You could try to create an extra function that splits the string and returns the array, perhaps with an own autorelease pool that you release after the function is run to make sure the strings are released.

这篇关于将NSString拆分为子字符串的最有效内存方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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