Nsstring Containsstring Crashes

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.
Sample Image

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



Leave a reply



Submit