NSString包含字符串崩溃

时间:2014-10-16 17:28:32

标签: ios nsstring

我试图根据其中一个字符串字段过滤数组。

nameLower和filterLower都包含NSString值,但我一直得到:

__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;
}

6 个答案:

答案 0 :(得分:119)

如果您希望代码在iOS 7和iOS 8上运行,则应使用其中一个rangeOfString调用。基本上,如果返回的范围的长度为零,则子字符串不存在。

/* 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);

显然,使用rangeOfString在类别中实现containsString是微不足道的:

@implementation NSString (Contains)

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

@end

答案 1 :(得分:8)

将rangeOfString与NSNotFound进行比较

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

答案 2 :(得分:3)

使用以下内容:

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
          }                             
     }

答案 3 :(得分:0)

对于在 XLForm 中遇到此问题的用户,请确保在使用pod安装 XLForm

platform :ios, '7'
pod 'XLForm'

已在3.1

中修复

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

}

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

}

答案 4 :(得分:0)

我将我的解决方案封装在YJKit中,即使对于iOS 8以下的旧版本,也可以调用 - [NSString containsString:]。

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;
}

这是我的源代码: https://github.com/huang-kun/YJKit/blob/master/YJKit/Base/Foundation/Categories/Generics/NSString%2BYJCompatible.m

答案 5 :(得分:-1)

w0mbat给出的答案的快速版本:

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