NSRangeException when searching for bytes near end of NSData
cocoa-touch, ios, nsrange, objective-c
Solution
The second member of `NSRange` is not the end point of the range but its length. So in your case it should be:
NSRange range = {([imageData length]-8), 8};
Problem
I want to check the last 2 bytes of files in my app to make sure they are not corrupt .jpg's `rangeOfData:options:range:` looks like a good option, but I am having a hard time figuring out how to get a proper `NSRange`. The range I am looking for is starting near the end of the `NSData`, up to the end. Here is what I have so far: ``` NSData *imageData = [NSData dataWithContentsOfFile:filePath]; NSRange range = {([imageData length]-8),[imageData length]}; NSString *str = @"FFD9"; NSData *jpgTest = [str dataUsingEncoding:NSUTF8StringEncoding]; NSRange found = [imageData rangeOfData:jpgTest options:NSDataSearchBackwards range:range]; ``` Here is the error I get: ** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSConcreteData rangeOfData:options:range:]: range {14954, 14962} enxceeds data length 14962' How do I properly get the range to search the last few bytes of my `NSData`?