0

我真的不能像他们说的那样透过树木看到森林。首先,这是我的代码:

 -(NSMutableArray*)getWaranty:(NSString*)string start:(NSString*)start and:(NSString*)end{
NSMutableArray *waranties = [[NSMutableArray alloc] init];
NSRange startRange = [string rangeOfString:start];
NSRange valueRange;
valueRange.location = startRange.location + startRange.length; //beginpunt
NSRange eindRange = [string rangeOfString:end];
valueRange.length = eindRange.location - valueRange.location;
NSString *result = [[NSString alloc]init];
result = nil;
if(valueRange.location != NSNotFound){
    if(valueRange.length != NSNotFound){
        result = [string substringWithRange:valueRange];
        NSString *testResult = [[NSString alloc]init];
        testResult = result;

        NSString* beginTrim = [[NSString alloc]init];
        NSString* beginTrim1 = [[NSString alloc]init];
        NSString* beginTrim2 = [[NSString alloc]init];
        NSString* beginTrim3 = [[NSString alloc]init];
        NSString* beginTrim4 = [[NSString alloc]init];
        NSString* beginTrim5 = [[NSString alloc]init];
        NSString* beginTrim6 = [[NSString alloc]init];
        NSString* beginTrim7 = [[NSString alloc]init];
        NSString* beginTrim8 = [[NSString alloc]init];
        NSString* beginTrim9 = [[NSString alloc]init];
        NSString* beginTrim10 = [[NSString alloc]init];
        beginTrim = [testResult stringByReplacingOccurrencesOfString:@"<li class=\"TopTwoWarrantyListItem\">"withString:@""];
        beginTrim1 = [beginTrim stringByReplacingOccurrencesOfString:@"<b>"withString:@""];
        beginTrim2 = [beginTrim1 stringByReplacingOccurrencesOfString:@"<li>"withString:@""];
        beginTrim3 = [beginTrim2 stringByReplacingOccurrencesOfString:@"</li>"withString:@""];
        beginTrim4 = [beginTrim3 stringByReplacingOccurrencesOfString:@"</b>"withString:@""];
        beginTrim5 = [beginTrim4 stringByReplacingOccurrencesOfString:@"<a href=\"javascript:SelectTabFromLink(5);\" id=\"SeeWarrantyTab\" class=\"uif_link\">"withString:@""];
        beginTrim6 = [beginTrim5 stringByReplacingOccurrencesOfString:@"</a>"withString:@""];
        NSString *lang =[[NSString alloc]init];
        lang= @"nl";
        if(lang == @"nl"){
            beginTrim7 = [beginTrim6 stringByReplacingOccurrencesOfString:@".  Raadpleeg het tabblad Garantie voor meer informatie."withString:@""];
        }
        beginTrim8 = [beginTrim7 stringByReplacingOccurrencesOfString:@"</div>"withString:@""];
        beginTrim9 = [beginTrim8 stringByReplacingOccurrencesOfString:@"\r\n"withString:@""];
        beginTrim10 = [beginTrim9 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        NSString *warant = [[NSString alloc]init];
        NSString *theWarant = [[NSString alloc]init];
        NSString *hoev = [[NSString alloc]init];
        NSString *lic = [[NSString alloc]init];
        NSString *datum = [[NSString alloc]init];
        if(lang == @"nl"){
            //split met garantie
            warant = [beginTrim10 stringByReplacingOccurrencesOfString:@"  garantie met einddatum op"withString:@""];
            NSArray *array = [[NSArray alloc]init];
            array = [warant componentsSeparatedByString:@"                                                                                                                 "];
            NSInteger hoeveel = [array count];
            if(hoeveel != 0){
                hoev = [NSString stringWithFormat:@"%d",hoeveel];
                [waranties addObject:hoev];
                for(int i = 0; i < hoeveel; i++){
                    if([array objectAtIndex:i] != NULL){
                        theWarant = [array objectAtIndex:i];
                        NSArray *specArray = [[NSArray alloc]init];
                        specArray = [theWarant componentsSeparatedByString:@"  "];
                        lic = [specArray objectAtIndex:0];
                        datum = [specArray objectAtIndex:1];
                        [waranties addObject:lic];
                        [waranties addObject:datum];
                        NSLog(@" de waraiens 1 :%@", [waranties objectAtIndex:0]);
                        NSLog(@" de waraiens 1 :%@", [waranties objectAtIndex:1]);
                    }
                    else{

                    }

                }
            NSLog(@" de waraiens 1 :%@", [waranties objectAtIndex:0]);
            NSLog(@" de waraiens 2 :%@", [waranties objectAtIndex:1]);
            NSLog(@" de waraiens 3 :%@", [waranties objectAtIndex:2]);
            NSLog(@" de waraiens 4 :%@", [waranties objectAtIndex:3]);
            NSLog(@" de waraiens 5 :%@", [waranties objectAtIndex:4]);
            return waranties;
            }
        }
    }
}

}

代码应返回如下内容:

2013-03-12 21:12:51.783 DellSupportApp[6254:c07] de waraiens 1:2

2013-03-12 21:12:51.785 DellSupportApp[6254:c07] de waraiens 2:戴尔业务支持/专业支持

2013-03-12 21:12:51.785 DellSupportApp[6254:c07] de waraiens 3:30/07/2009

2013-03-12 21:12:51.785 DellSupportApp[6254:c07] de waraiens 4:下一个工作日

2013-03-12 21:12:51.786 DellSupportApp[6254:c07] de waraiens 5:30/07/2009

但棘手的是。它只能像 10 次中的 1 次一样工作...在此函数中调用该方法:

-(NSMutableArray*) parseIt: (NSString*) toPars {
NSMutableArray *waranties = [[NSMutableArray alloc]init];
NSString *beginTitle = @"<title>";
NSString *eindTitle = @"</title>";
NSString *title = [self getTitle:toPars start:beginTitle and:eindTitle];
if(title != NULL){
    NSLog(@"de title = %@", title);
    [waranties addObject:title];
    NSString *try = [waranties objectAtIndex:0];
    NSLog(@"de title = %@", try);
}

NSString *beginWar = @"<div class=\"TopTwoWarrantySummaryDiv\">";
NSString *eindWar = @"<div class=\"RequestWarrantyExtensionDiv\">";
NSMutableArray *warrant = [[NSMutableArray alloc]init];
warrant = [self getWaranty:toPars start:beginWar and:eindWar];
if(warrant != NULL){
    [waranties addObject:warrant];
     NSLog(@" de warant %@ ", [waranties objectAtIndex:1]);
}
return waranties;

我觉得它进入这里显示的第一种方法非常麻烦,大约 10 次,而我只问过一次......我现在已经研究了 4 个多小时的问题,厌倦了一些错误,实际上错误主要是没有显示...

请指出我的错误...

4

2 回答 2

1

查看您的代码,我担心它有多脆弱。它正在寻找一系列空间来分隔保修,但这真的是分割数据的最佳方式吗?

除此之外,您还在对这些分裂做出假设。例如:

specArray = [theWarant componentsSeparatedByString:@"  "];

已分配并且您检查索引 0 和 1,但永远不要确保数组中有 2 个值。

NSLog在底部的 s 也不会在处理数组边界之前检查它们。

众所周知,空格是数据的不良分隔符。如果您正在寻找解析 html 的工具,可能会更好地完成这项工作。

于 2013-03-12T20:29:20.243 回答
0

如果waranties在执行之前是一个空数组,则此语句将失败并超出范围:

if(warrant != NULL){
    [waranties addObject:warrant];
     NSLog(@" de warant %@ ", [waranties objectAtIndex:1]);
}

数组中的第一个对象的索引为 0,而不是 1。

每次找不到标题时都会发生这种情况。如果总是有标题,这可能不是您的问题。但是,编码更节省:

if(warrant != NULL){
    [waranties addObject:warrant];
     NSLog(@" de warant %@ ", [waranties objectAtIndex:[caranties count]-1]);
}

或者可能更好:

if(warrant != NULL){
    [waranties addObject:warrant];
     NSLog(@" de warant %@ ", [waranties lastObject]);
}

无论如何,由于这只是调试输出,它肯定不是问题的根本原因。由于其格式化功能,我刚刚选择了答案而不是评论。

于 2013-03-12T20:29:32.057 回答