iOS7仅返回';NSInvalidArgumentException';,原因:'-[\uu NSCFString包含字符串:]:

iOS7仅返回';NSInvalidArgumentException';,原因:'-[\uu NSCFString包含字符串:]:,ios,xcode,Ios,Xcode,我正在使用一个图书应用程序,用户可以点击一个单词,然后播放包含该单词的音频文件的一部分,并突出显示该单词。不幸的是,我现在遇到了一些错误,因为我已经尝试优化代码,从仅支持iOS8到也支持iOS7 这是我得到的错误: 2015-06-10 10:42:50.455 Chi Shin får en hund[63976:607] selected :sammen -- <_UITextKitTextPosition: 0x7862b8b0> (28B) 2015-06-10 10:42:

我正在使用一个图书应用程序,用户可以点击一个单词,然后播放包含该单词的音频文件的一部分,并突出显示该单词。不幸的是,我现在遇到了一些错误,因为我已经尝试优化代码,从仅支持iOS8到也支持iOS7

这是我得到的错误:

2015-06-10 10:42:50.455 Chi Shin får en hund[63976:607] selected :sammen -- <_UITextKitTextPosition: 0x7862b8b0> (28B)
2015-06-10 10:42:50.456 Chi Shin får en hund[63976:607] -[__NSCFString containsString:]: unrecognized selector sent to instance 0x78f585a0
2015-06-10 10:42:50.458 Chi Shin får en hund[63976:607] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString containsString:]: unrecognized selector sent to instance 0x78f585a0'
*** First throw call stack:
(
    0   CoreFoundation                      0x023371e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x020b68e5 objc_exception_throw + 44
    2   CoreFoundation                      0x023d4243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x0232750b ___forwarding___ + 1019
    4   CoreFoundation                      0x023270ee _CF_forwarding_prep_0 + 14
    5   Chi Shin faÃär en hund              0x000ab695 -[DataViewController pageWordTapped:] + 805
    6   UIKit                               0x015384f4 _UIGestureRecognizerSendActions + 230
    7   UIKit                               0x01537168 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 383
    8   UIKit                               0x01538bdd -[UIGestureRecognizer _delayedUpdateGesture] + 60
    9   UIKit                               0x0153c13d ___UIGestureRecognizerUpdate_block_invoke + 57
    10  UIKit                               0x0153c0be _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 317
    11  UIKit                               0x015327ac _UIGestureRecognizerUpdate + 199
    12  UIKit                               0x011dda5a -[UIWindow _sendGesturesForEvent:] + 1291
    13  UIKit                               0x011de971 -[UIWindow sendEvent:] + 1021
    14  UIKit                               0x011b05f2 -[UIApplication sendEvent:] + 242
    15  UIKit                               0x0119a353 _UIApplicationHandleEventQueue + 11455
    16  CoreFoundation                      0x022c077f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    17  CoreFoundation                      0x022c010b __CFRunLoopDoSources0 + 235
    18  CoreFoundation                      0x022dd1ae __CFRunLoopRun + 910
    19  CoreFoundation                      0x022dc9d3 CFRunLoopRunSpecific + 467
    20  CoreFoundation                      0x022dc7eb CFRunLoopRunInMode + 123
    21  GraphicsServices                    0x0380a5ee GSEventRunModal + 192
    22  GraphicsServices                    0x0380a42b GSEventRun + 104
    23  UIKit                               0x0119cf9b UIApplicationMain + 1225
    24  Chi Shin faÃär en hund              0x000dad2a main + 138
    25  libdyld.dylib                       0x030606d9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
和功能

- (void) pageWordTapped:(UITapGestureRecognizer *)recognizer{

    UITextView *textView =  (UITextView *)recognizer.view;

    CGPoint location = [recognizer locationInView:textView];
//    NSLog(@"Tap Gesture Coordinates: %.2f %.2f -- %@", location.x, location.y,textView.text);

    CGPoint position = CGPointMake(location.x, location.y);

    //get location in text from textposition at point
    UITextPosition *tapPosition = [textView closestPositionToPoint:position];

    //fetch the word at this position (or nil, if not available)
    UITextRange *textRange = [textView.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];

    NSString *tappedSentence = [textView textInRange:textRange];
    NSLog(@"selected :%@ -- %@",tappedSentence,tapPosition);

    if (!togglePlayIsOn) {

        for (int i = 0; i < wordArray.count; i ++) {

            WordModel *wordModel = wordArray[i];

            if ((wordModel.word.length - tappedSentence.length) < 3 && [wordModel.word containsString:tappedSentence]) {

                double timeInterval = [wordModel.endTime doubleValue] - [wordModel.startTime doubleValue];
                NSTimeInterval intervalForTimer = timeInterval;

                NSLog(@"timeInterval :%@,   %@,   %f", wordModel.startTime, wordModel.endTime, intervalForTimer);

                [NSTimer scheduledTimerWithTimeInterval:0.0
                                                 target:self
                                               selector:@selector(highlightWord:)
                                               userInfo:@{@"rangeLocation": wordModel.rangeLocation, @"rangeLength": wordModel.rangeLength}
                                                repeats:NO];

                [NSTimer scheduledTimerWithTimeInterval:intervalForTimer
                                                 target:self
                                               selector:@selector(unHighlightEndOfText:)
                                               userInfo:@{@"wordLength": [NSNumber numberWithInt:1]}
                                                repeats:NO];

                [self playWord:[wordModel.startTime doubleValue] endTime:[wordModel.endTime doubleValue]];

                return;

            }

        }

    }

}
-(void)pageWordTapped:(UITapGestureRecognitizer*)识别器{
UITextView*textView=(UITextView*)识别器.view;
CGPoint位置=[识别器位置视图:文本视图];
//NSLog(@“点击手势坐标:%.2f%.2f--%@”,位置.x,位置.y,文本视图.text);
CGPoint位置=CGPointMake(位置.x,位置.y);
//从点处的textposition获取文本中的位置
UITextPosition*tapPosition=[textView closestPositionToPoint:position];
//在此位置提取单词(如果不可用,则为nil)
UITextRange*textRange=[textView.tokenizer RangeEnclosuringPosition:tapPosition with Granularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
NSString*TAPPEDSENTRAGE=[textView textInRange:textRange];
NSLog(@“所选:%@--%@”,点击句子,点击位置);
如果(!切换Playison){
对于(int i=0;i

如果我对
If(!togglePlayIsOn)
语句的注释超过了
If,那么应用程序不会崩溃,因此我相信在该代码段中会发生错误。

检查
[NSString containsString:://code>的方法仅在iOS8上可用,要在iOS7上执行此操作,您需要:

NSString *string = @"foo";
if ([string rangeOfString:@"oo"].location == NSNotFound) {

} else {

}

问题是
containssString:
是一个IOS8API,因此在iOS7下崩溃。 你可以换一个

[wordModel.word containsString:tappedSentence]


由于使用了
[wordModel.word containsString:tappedstance]
containsString,您的代码崩溃在iOS 8及更高版本中可用。如果您想同时使用iOS 7和iOS 8+,请使用以下代码-

使用rangeOfString在类别中实现containsString:

@implementation NSString (Contains)

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

希望这对你有帮助。

哦,是的。。这绝对是最简单、最简单的方法。。谢谢
[wordModel.word rangeOfString:tappedSentence].location != NSNotFound
@implementation NSString (Contains)

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