NSString containsString crashes
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
iOS7 only returns 'NSInvalidArgumentException', reason: '-[__NSCFString containsString:]:
The problem is that containsString:
is an iOS8 API and thus crashing under iOS7.
You could just replace
[wordModel.word containsString:tappedSentence]
with
[wordModel.word rangeOfString:tappedSentence].location != NSNotFound
How do I check if a string contains another string in Objective-C?
NSString *string = @"hello bla bla";
if ([string rangeOfString:@"bla"].location == NSNotFound) {
NSLog(@"string does not contain bla");
} else {
NSLog(@"string contains bla!");
}
The key is noticing that rangeOfString:
returns an NSRange
struct, and the documentation says that it returns the struct {NSNotFound, 0}
if the "haystack" does not contain the "needle".
And if you're on iOS 8 or OS X Yosemite, you can now do: *(NOTE: This WILL crash your app if this code is called on an iOS7 device).
NSString *string = @"hello bla blah";
if ([string containsString:@"bla"]) {
NSLog(@"string contains bla!");
} else {
NSLog(@"string does not contain bla");
}
(This is also how it would work in Swift)
unrecognized selector sent to instance exception in iOS7
You can check in framework header.
- (BOOL)containsString:(NSString *)aString NS_AVAILABLE(10_10, 8_0);
This method of containsString only exist after iOS 8 so its obvious that it will throw error in iOS7....
Please use below method for ios7 and you can use above method for ios8:
if ([string rangeOfString:@"bla"].location != NSNotFound)
{
NSLog(@"charecter found");
}
May be this can solve your issue.
Thanks
saving a large nsstring file many times crashes the application
If you don't see where this is leaking then please switch to ARC. It's as clear as day in the middle:
Alloc xml
Set the variable xml
to nil (LEAK!!!!!)
Release the content of xml
(which is nil) <--- this does nothing
The last two are reversed. You need to release it before you set it to nil. I suggest reading a little more about pointers if you do not understand this concept. The release message acts on the content of the pointer, not the pointer itself. The latter wouldn't make sense.
Assertion failure in -[SWRevealView layoutSublayersOfLayer:]
SWRevealViewController.m's layoutSubviews method should include [super layoutSubviews]
Related Topics
Create and Store PDF Document Programmatically Using Swift for iOS
Auto Layout Constraint on Calayer iOS
Chrome iOS - Is It Just a Uiwebview
How to Pop Specific View Controller in Swift
App Crashes on Enabling Camera Access from Settings iOS 8
Service Workers and iOS/Safari
How to Select Range of Values When Using Arc4Random()
How to Get Visible Viewcontroller from App Delegate When Using Storyboard
Uicollectionview Registerclass: Forcellwithreuseidentifier Method Breaks Uicollectionview
Facebook Sdk 3.0: How to Receive User's E-Mail
Drop Cap with Nsattributedstring
How to Trap the Back Button Event
Still Not Optimized for iPhone 6 and iPhone 6 Plus
Corelocation Heading Base on Back Camera (Augmented Reality)
How to Convert an iPhone Osstatus Code to Something Useful
Cannot Create Outlet Connections to Subviews in Interface Builder (Xcode 5)
Request Permissions Again After User Denies Location Services