检查并返回objective-c中的非常量整数

检查并返回objective-c中的非常量整数,objective-c,xcode,http,Objective C,Xcode,Http,关于以下问题“”, 我有一个返回以下标头的HTTP服务器: 由于我是新用户,所以无法附加屏幕截图,而是使用了blockquotes:( HTTP标头:{ -信息遗漏- Server=“HTTP客户端套件(测试用例号:21)” 我为从HTTP服务器获取响应而编写的代码块是: - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { *- code omit

关于以下问题“”, 我有一个返回以下标头的HTTP服务器:

由于我是新用户,所以无法附加屏幕截图,而是使用了blockquotes:(

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);
    }

}
我在这里所做的是实际查看字符串“testcase number”是否存在。如果存在,那么我希望提取数字21并使用NSInteger将其存储在一个变量中,现在的问题是数字是可变的,而不是常量(它将根据HTTP服务器返回的内容而每次改变),因此,在本例中,我无法使用与前面相同的方法来检查字符串中是否存在整数

如何实现这一点?提前感谢!

您可以使用a从字符串中提取数字。我在这里使用的模式是
“测试用例号:”

首先创建一个正则表达式:

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"Test case number:(\\d+)"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:&error];
NSString *string = @"HTTP Client Suite (Test case number:21)";
NSTextCheckingResult *match = [regex firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];
检查字符串是否与此正则表达式匹配:

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"Test case number:(\\d+)"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:&error];
NSString *string = @"HTTP Client Suite (Test case number:21)";
NSTextCheckingResult *match = [regex firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];
如果找到匹配项,则从原始字符串中提取。首先从原始字符串中获取数字的范围(起始位置、长度),然后对原始字符串使用
substringWithRange:
来提取数字。这有点冗长:)


现在,
number
应该以字符串形式包含您要查找的数字。

您好,我对您的代码有一个问题,不知怎的,它似乎只返回“2”而不是“21”,当数字用于例如“414”时,它将只返回“4”很好。正则表达式应该是
\\d+
而不是
\\d
。现在已修复。
+
表示我们正在查找一个或多个数字。没有它,我们只查找一个数字。