他们说,我真的看不到森林穿过树林。 首先,这是我的代码:
-(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
但棘手的是。它的效果只有十分之一...... 在这个函数中调用该方法:
-(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个多小时的问题,厌倦了一些错误,实际上错误大多没有显示......
请指出我的错误......
答案 0 :(得分:1)
看看你的代码我担心它有多脆弱。它正在寻找一系列空间来分离保修,但这真的是分割数据的最佳方式吗?
除此之外,你正在对这些分裂做出假设。例如:
specArray = [theWarant componentsSeparatedByString:@" "];
分配并检查索引0和1,但从不确保数组中有2个值。
底部的NSLog
也不会在处理数组边界之前检查它们。
空间对于数据来说是非常糟糕的分界符。如果你想解析html,那里有可以更好地完成工作的工具。
答案 1 :(得分: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]);
}
或probalby甚至更好:
if(warrant != NULL){
[waranties addObject:warrant];
NSLog(@" de warant %@ ", [waranties lastObject]);
}
无论如何,因为这只是调试输出,它肯定不是你问题的根本原因。由于其格式化功能,我刚刚选择了答案而不是评论。