0
votes

Need your insight here...

This keeps crashing. Im calling a table on certain site, some say its the site that is causing but I can also make an exception. Can I get your two cents here?

    NSRange blockStartRange = [pageContent rangeOfString:@"\t<table border=\"0\" cellpadding=\"4\" cellspacing=\"0\" id=\"table"];

    NSRange blockEndRange = [pageContent rangeOfString:@"\t</table>" options:NSBackwardsSearch range:NSMakeRange(blockStartRange.location, [pageContent length] - blockStartRange.location)];

    [HTMLString appendString:[pageContent substringWithRange:NSMakeRange(blockStartRange.location, blockEndRange.location + blockEndRange.length - blockStartRange.location)]];

    //        [HTMLString replaceOccurrencesOfString:@"width: 700px;" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [HTMLString length])];

}

[webView loadHTMLString:HTMLString baseURL:[NSURL URLWithString:@"http://bowlatrabs.com/"]];

}

Error Log

2014-08-01 15:52:22.092 Bowl at Rabs[61401:90b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSCFString rangeOfString:options:range:locale:]: Range {2147483647, 2147542871} out of bounds; string length 59222'
*** First throw call stack:
(
0   CoreFoundation                      0x0270b1e4 __exceptionPreprocess + 180
1   libobjc.A.dylib                     0x020178e5 objc_exception_throw + 44
2   CoreFoundation                      0x0270afbb +[NSException raise:format:] + 139
3   Foundation                          0x000c38cd -[NSString rangeOfString:options:range:locale:] + 185
4   Foundation                          0x000cf156 -[NSString rangeOfString:options:range:] + 69
5   Bowl at Rabs                        0x0002d9e8 -[LeagueController viewDidLoad] + 920
6   UIKit                               0x005dc33d -[UIViewController loadViewIfRequired] + 696
7   UIKit                               0x005dc5d9 -[UIViewController view] + 35
8   UIKit                               0x005f6942 -[UINavigationController _startCustomTransition:] + 778
9   UIKit                               0x006038f7 -[UINavigationController _startDeferredTransitionIfNeeded:] + 688
10  UIKit                               0x006044e9 -[UINavigationController __viewWillLayoutSubviews] + 57
11  UIKit                               0x007450d1 -[UILayoutContainerView layoutSubviews] + 213
12  UIKit                               0x0052c964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
13  libobjc.A.dylib                     0x0202982b -[NSObject performSelector:withObject:] + 70
14  QuartzCore                          0x04d9b45a -[CALayer layoutSublayers] + 148
15  QuartzCore                          0x04d8f244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
16  QuartzCore                          0x04d8f0b0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
17  QuartzCore                          0x04cf57fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
18  QuartzCore                          0x04cf6b85 _ZN2CA11Transaction6commitEv + 393
19  QuartzCore                          0x04cf7258 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
20  CoreFoundation                      0x026d336e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
21  CoreFoundation                      0x026d32bf __CFRunLoopDoObservers + 399
22  CoreFoundation                      0x026b1254 __CFRunLoopRun + 1076
23  CoreFoundation                      0x026b09d3 CFRunLoopRunSpecific + 467
24  CoreFoundation                      0x026b07eb CFRunLoopRunInMode + 123
25  GraphicsServices                    0x035765ee GSEventRunModal + 192
26  GraphicsServices                    0x0357642b GSEventRun + 104
27  UIKit                               0x004bdf9b UIApplicationMain + 1225
28  Bowl at Rabs                        0x0000239c main + 76
29  libdyld.dylib                       0x0325d725 start + 0
)

libc++abi.dylib: terminating with uncaught exception of type NSException

(lldb) 
2

2 Answers

2
votes

You need to handle the case where the strings do not exist. Apple's documentation for the rangeOfString: method says: "Returns {NSNotFound, 0} if aString is not found or is empty (@"")."

So you need to check to see if the returned ranges have range.location == NSNotFound and handle that in whatever way is best.

0
votes

Other way to check is using NSRangeFromString

NSRangeFromString: Returns a range from a textual representation.

If the string passed into NSRangeFromString does not represent a valid range, it will return a range with its location and length set to 0.

NSString *string = @"invalid";
NSRange range = NSRangeFromString(string);
// {.location=0, .length=0}

You might check nshipster/range