Reputation: 23500
I'm using this method to find the first <> couple into a string (XML content) :
NSScanner* scanner = [NSScanner scannerWithString:contentToParse];
int startPos = 0;
int endPos = 0;
// Open search
if ([scanner scanString:@"<" intoString:nil]) {
startPos = [scanner scanLocation]-1;
NSLog(@"found '<' at pos %i", startPos);
// close search
if ([scanner scanString:@">" intoString:nil]) {
endPos = [scanner scanLocation]-1;
NSLog(@"found '>' at pos %i", endPos);
NSString* tag = [contentToParse substringWithRange:NSMakeRange(startPos, endPos-startPos)];
NSLog(@"Tag found : %@", tag);
}
}
but only "found '<' at pos 0" is logged. My XML content contains many many <> items...
Why is that method not working ?
Upvotes: 0
Views: 542
Reputation: 28572
scanString:intoString:
tries to scan the string parameter at the current location. If such string is not present at the current location, it simply returns NO
.
You may want use scanUpToString:intoString:
(reference) instead, which scans advancing the scan location until the given string is encountered.
NSScanner *scanner = [NSScanner scannerWithString:contentToParse];
// open search
[scanner scanUpToString:@"<" intoString:nil];
if (![scanner isAtEnd]) {
[scanner scanString:@"<" intoString:nil];
// close search
NSString *tag = nil;
[scanner scanUpToString:@">" intoString:&tag];
if (![scanner isAtEnd]) {
NSLog(@"Tag found : %@", tag);
}
}
Upvotes: 3