With reference to the following question "String contains string in objective-c", I have a HTTP server that returns the following header:
P.S. Cannot attach a screenshot because I'm a new user, used blockquotes instead :(
HTTP header: {
-info omitted-
Server = "HTTP Client Suite (Test case number:21)";
The block of code which i have written to get a response from the HTTP server is:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
*- code omitted -*
// **HTTP header field**
// A dictionary containing all the HTTP header fields of the receiver
// By examining this dictionary clients can see the βrawβ header information returned by the server
NSDictionary *headerField = [[NSDictionary alloc]initWithDictionary:[(NSHTTPURLResponse *)httpResponse allHeaderFields]];
// Call headerField dictionary and format into a string
NSString *headerString = [NSString stringWithFormat:@"%@", headerField];
NSLog(@"HTTP Header: %@",headerString);
// String to match (changeable) and temporary string to store variable
NSString *stringToMatch = @"Test case number";
NSString *tempString = @"";
// Check if headerString contains a particular string
// By finding and returning the range of the first occurrence of the given string (stringToMatch) within the receiver
// If the string is not found/doesn't exists
if ([headerString rangeOfString:stringToMatch].location == NSNotFound)
{
NSLog(@"Header does not contain the string: '%@'", stringToMatch);
tempString = NULL;
NSLog(@"String is %@", tempString);
}
else
{
NSLog(@"Header contains the string: '%@'", stringToMatch);
tempString = stringToMatch;
NSLog(@"String is '%@'", tempString);
}
}
What I'm doing here is to actually see if the string "Test case number" exists. If it does, then I would want to extract the number 21 and store it in a variable using NSInteger, the problem now is the number is mutable and not constant (it will change each time depending on what the HTTP server returns), therefore I'm unable to use the same approach which I did earlier on to check whether an integer exists within a string in this case.
How do I go about achieving this? Thanks in advance!