oauth_token=requestkey&oauth_token_secret=requestsecret
How can I use NSScanner to get “requestkey” and “requestsecret”. I can’t seem to achieve it.
NSScanner* scanner = [NSScanner scannerWithString:string];
NSString *oauth_token = @"oauth_token=";
NSString *oauth_token_secret = @"oauth_token_secret=";
[scanner setCharactersToBeSkipped:nil];
NSString *token;
NSString *key;
while (![scanner isAtEnd]) {
[scanner scanString:oauth_token intoString:NULL];
[scanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@"&"] intoString:&token];
[scanner scanUpToString:oauth_token_secret intoString:NULL];
[scanner scanUpToString:oauth_token intoString:&key];
NSLog(@"token%@", token);
NSLog(@"key %@", key);
//token requestkey
//key oauth_token_secret=requestsecret
}
I can’t seem to figure out why is it null. Thanks!
Nothing is null. So I can’t speak to that.
It’s actually a pretty straight forward error if you just follow the logic of your code line by line. For example:
The simplest way to fix this is to use the
scanString:intoString:method to advance the cursor to the end ofoauth_token_secret.The log output now shows useful strings.
But, as H2CO3 said in the comments section,
componentsSeparatedByString:is a much better fit for this use case.