I tried it in objective c, with try catch block.It's working fine with this code
NSString *string = @"This is demo text";
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
//it's(data) length is 17
@try {
NSData *subdata = [data subdataWithRange:NSMakeRange(4000, 20)];
} @catch (NSException *exception) {
NSLog(@"exception ERROR: %@",exception);
}
It's print exception error.
But when I tried it with try catch block in swift, it's not giving exception error and app crashed at that point.
var sourceString = "This is demo text"
let sourceData = sourceString.data(using: String.Encoding.utf8)! // sourceData is equivalent to "wav" from question
guard let subdata = Data(sourceData.subdata(in: 4000 ..< (4000 + 20))) else{
throw LevelParsingException.InvalidLevelContent
}
I am trying to handle NSRangeException error in swift 3.