Check for substring of NSString crashing

你离开我真会死。 提交于 2020-01-14 09:46:09

问题


I've tried checking for an occurrence of a substring in an NSString in two ways, both have crashed. This is an NSString object I get from one of my NSManagedObject's properties.

  1. Using NSRange

    NSString *searchString = @"drive";

    NSRange range = [text rangeOfString:searchString options:(NSCaseInsensitiveSearch)];

    if (range.location != NSNotFound) { NSLog(@"Found"); }

  2. Using NSScanner

    NSScanner *scanner = [NSScanner scannerWithString:text];

    NSLog(@"%d",[scanner scanString:searchString intoString:nil]);

Both of these work when text = @"drive" but cause EXC_BAD_ACCESS crashes when the text is "i drive", or "drive to". Nothing happens if the text is "idrive" or "driveto".

Even stranger, sometimes the examples throw NSInvalidArgumentExceptions, saying that I tried to pass an NSCFSet or a DateComponents object to rangeOfString:, neither of which I use in my app.

Any ideas?


回答1:


If you sometimes get EXC_BAD_ACCESS and sometimes get the message "object-of-type-you-weren't expecting does not respond to method" the chances are you are not retaining the object for long enough and it is getting deallocated before you get to where you use it.

If the memory hasn't been reused when you get there, things will appear to work. If the space has been reused for another object and aligns perfectly i.e. same pointer, you'll get something like "x does not respond to selector". If it's overwritten in such a way that the pointer does not point to a valid object, you'll get EXC_BAD_ACCESS




回答2:


The following worked perfectly for me including the spaces.

NSString *string = @"hello one two three";
if ([string rangeOfString:@"one two"].location == NSNotFound) {
    NSLog(@"string does not contain substring");
} else {
    NSLog(@"string contains substring!");
}

And if you want it to be case insensitive, just convert both the string and the search string to lowercase. You should be knowing how to do that I guess.



来源:https://stackoverflow.com/questions/7310040/check-for-substring-of-nsstring-crashing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!