Home » Ios » NSString containsString crashes

NSString containsString crashes

Posted by: admin November 30, 2017 Leave a comment

Questions:

I’m trying to filter an array according to one of it’s string fields.

Both nameLower and filterLower has NSString value inside, and yet i keep getting:

__NSCFString containsString:]: unrecognized selector sent to instance 0x7f876b79e160

-(void) filterFriendsArray:(NSString*)filter {
    [_filteredFriendsArray removeAllObjects];
    for (FacebookUser* user in _friendsArray)
    {
        NSString* nameLower = [user.user.name lowercaseString];
        NSString* filterLower = [filter lowercaseString];
        if ([nameLower containsString:filterLower])
            [_filteredFriendsArray addObject:user];
    }
    _displayedFriendsArray = _filteredFriendsArray;
}
Answers:

If you want your code to work on iOS 7 as well as iOS 8 you should use one of the rangeOfString calls instead. Basically if the range returned has a length of zero, the substring is not there.

/* These methods return length==0 if the target string is not found. So, to check for containment: ([str rangeOfString:@"target"].length > 0).  Note that the length of the range returned by these methods might be different than the length of the target string, due composed characters and such.
*/
- (NSRange)rangeOfString:(NSString *)aString;
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask;
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)searchRange;
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)searchRange locale:(NSLocale *)locale NS_AVAILABLE(10_5, 2_0);

Obviously it’s trivial to implement containsString yourself in a category using rangeOfString:

@implementation NSString (Contains)

- (BOOL)myContainsString:(NSString*)other {
  NSRange range = [self rangeOfString:other];
  return range.length != 0;
}

@end

Questions:
Answers:

compare rangeOfString with NSNotFound

NSRange range = [self rangeOfString:other];
if(range.location != NSNotFound){
    //do something
}

Questions:
Answers:

Use following:

if (![[NSString class] respondsToSelector:@selector(containsString)])
     {
         //ios 7
         NSRange range = [mystring rangeOfString:other];
         if(range.location != NSNotFound){
           //do something
         }
     }
     else  //for iOS 8 
     {
          if ([mystring containsString: other])
          {
              //do something
          }                             
     }

Questions:
Answers:

For those who encountered this in XLForm, make sure when you install XLForm using pods

platform :ios, '7'
pod 'XLForm'

It is already fixed in 3.1

from

if ([cellClassString contains:@"/"]) {

}

to

if ([cellClassString rangeOfString:@"/"].location != NSNotFound) {

}

Questions:
Answers:

I encapsulate my solution in YJKit, and you can call -[NSString containsString:] even for old version which below iOS 8.

bool _yj_streq(const char *str1, const char *str2, size_t length) {
    for (int i = 0; i < length; i++) {
        if (*str1++ != *str2++) {
            return false;
        }
    }
    return true;
}

- (BOOL)yj_containsString:(NSString *)string {

    NSAssert(string != nil, @"*** -[%@ containsString:] can not use nil argument.", [self class]);

    size_t len1 = (size_t)self.length;
    size_t len2 = (size_t)string.length;

    if (len1 == 0 || len2 == 0 || len1 < len2) {
        return NO;
    }

    const char *str1 = self.UTF8String;
    const char *str2 = string.UTF8String;

    for (size_t i = 0; i <= len1 - len2; i++) {
        const char *substr1 = str1 + i;
        if (_yj_streq(substr1, str2, len2)) {
            return YES;
        } else {
            continue;
        }
    }

    return NO;
}

Here is my source code:
https://github.com/huang-kun/YJKit/blob/master/YJKit/Base/Foundation/Categories/Generics/NSString%2BYJCompatible.m

Questions:
Answers:

Swift version of the answer given by w0mbat:

extension NSString {
    func compatibleContainsString(string: NSString) -> Bool{
        let range = self.rangeOfString(string as String)
        return range.length != 0
    }
}