次の質問「String contains string in Objective-c」を参照して、次のヘッダーを返す HTTP サーバーを使用しています。
PS私は新しいユーザーであるため、スクリーンショットを添付できません。代わりに引用符を使用しました:(
HTTP ヘッダー: {
-情報省略-
Server = "HTTP クライアント スイート (テスト ケース番号:21)";
HTTP サーバーからの応答を取得するために私が書いたコードのブロックは次のとおりです。
- (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);
}
}
ここで行っているのは、文字列 "Test case number" が存在するかどうかを実際に確認することです。もしそうなら、数値21を抽出し、NSIntegerを使用して変数に格納したいと思います。問題は、数値が可変で定数ではないことです(HTTPサーバーが返すものに応じて毎回変化します)。したがって、私はこの場合、文字列内に整数が存在するかどうかを確認するために以前に行ったのと同じアプローチを使用できません。
これを達成するにはどうすればよいですか?前もって感謝します!