获取具有返回类型的递归函数的EXC_BAD_ACCESS - Objective-C

时间:2016-10-24 09:16:04

标签: ios objective-c recursion uiwebview synchronization

我有以下情况:

5个UIWebViews - 1个用于提问,其他4个用于选项。 每个加载html字符串从本地数据库中检索。但在加载那个html字符串之前,我自定义它,以便拥有自定义css,数学公式,应用base64编码图像等,如下所示:

[self processText:queText forWebView:wvQue];
[self processText:optText1 forWebView:wvOpt1];
[self processText:optText2 forWebView:wvOpt2];
[self processText:optText3 forWebView:wvOpt3];
[self processText:optText4 forWebView:wvOpt4];

-(void)processText:(NSString*)htmlString forWebView:(UIWebView*)webView withOptionButton:(UIButton*)optionButton
{
    NSLog(@"Processing....");

    webView.delegate = self;

    if(htmlString.length == 0)
    {
        webView.hidden = YES;
        optionButton.hidden = YES;
        [webView loadHTMLString:@"" baseURL:nil]; //dummy
    }
    else
    {
        webView.hidden = NO;
        optionButton.hidden = NO;

        webView.scrollView.directionalLockEnabled = YES;
        webView.scrollView.showsVerticalScrollIndicator = NO;
        webView.scrollView.showsHorizontalScrollIndicator = NO;

        htmlString = [self applyImageIfExist:htmlString];

        [webView loadHTMLString:[self customizeString:htmlString forWebView:webView] baseURL:nil];
    }
}

在上面的代码中,applyImageIfExist方法是递归的,有时会得到EXC_BAD_ACCESS。

-(NSString*)applyImageIfExist:(NSString*)aString
    {
        if([aString rangeOfString:@"<img alt"].location != NSNotFound)
        {
            NSString *tempStr = [NSString stringWithString:aString];

            NSRange tagStart = [tempStr rangeOfString:@"<img alt"]; //(NSRange) tagStart = location=4, length=8

            NSRange tagEnd = [[tempStr substringFromIndex:tagStart.location] rangeOfString:@"/>"]; //(NSRange) tagEnd = location=57, length=2

            NSString *imgTag = [tempStr substringWithRange:NSMakeRange(tagStart.location, tagEnd.location)];

            if(imgTag != nil)
                imgTag = [imgTag stringByAppendingString:@"/>"];    //<img alt="" src="IMG-725bd4a7f35ecdc6-20160405191358.png"

            for (NSString *iName in [allQuestionImages allKeys])
            {
                if([imgTag rangeOfString:iName].location != NSNotFound)
                {
                    NSString *dataTag = [CommonUtility setStringIfNotNull:[[allQuestionImages valueForKey:iName] valueForKey:@"DataUri"]];

                    tempStr = [tempStr stringByReplacingOccurrencesOfString:imgTag withString:dataTag];

                    break;
                }
            }

            NSLog(@"Applying image : Recursion ...");
            return [self applyImageIfExist:tempStr];
        }
        else
        {
            NSLog(@"Returning ...");
            return aString;
        }
    }

任何人都知道为什么会这样。这是线程问题吗?我应该使用@synchronized块吗? ...... ???

谢谢!

0 个答案:

没有答案