简体   繁体   English

NSRange的问题

[英]Problem with NSRange

I'm having a problem with NSRange. 我遇到了NSRange的问题。 Here is my code: 这是我的代码:

NSRange range = [[[NSHTTPCookie requestHeaderFieldsWithCookies:[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:cookie]]] objectForKey:@"Cookie"] rangeOfString:@"x"];
NSLog(@"%f", range.length);
if (range.length >= 1) {
    NSLog(@"Do Something");
} else {
    NSLog(@"AUTHING");
}

Console output: 控制台输出:

0.000000
Do something

And then the second time I run through the code: 然后我第二次浏览代码:

0.000000
AUTHING

What the hell is going on? 这到底是怎么回事? NSNotFound I think it was does not work and I'm not the only person finding this problem so using it is not a solution. NSNotFound我认为它不起作用,我不是唯一一个发现这个问题的人因此使用它不是一个解决方案。

Thanks for any help. 谢谢你的帮助。

Cheers 干杯

Edit: I tried using NSLog(@"%d", range.length) but it gives incorrect output the first time it runs through, the second time it runs through it is correct. 编辑:我尝试使用NSLog(@“%d”,range.length),但它第一次运行时输出错误,第二次运行是正确的。 I've tried using NSNotFound thinking that the strange output is due to it being NSNotFound but it didn't trigger 我已经尝试过使用NSNotFound认为奇怪的输出是由于它是NSNotFound但它没有触发

If you want to see if the string was found using -[NSString rangeOfString:] , you need to see if NSRange.location == NSNotFound: 如果你想查看是否使用-[NSString rangeOfString:]找到了字符串,你需要查看NSRange.location == NSNotFound:

if (range.location != NSNotFound) {
    // String was found
else {
    // String not found
}

As a general comment, debugging is much easier if you split up the nested method calls and do a quick NSLog to see what's going on. 作为一般性评论,如果您拆分嵌套方法调用并执行快速NSLog以查看正在进行的操作,则调试会更容易。 (i'm not that good at adhering to that so it's not meant as criticism). (我不擅长坚持这一点所以它并不意味着批评)。

One thing i first noted was the use of "%f" to display the length, try using %i (integer) and you should be able to get the correct length. 我首先注意到的一件事是使用“%f”来显示长度,尝试使用%i(整数),你应该能够得到正确的长度。 %f will always display 0.00000. %f将始终显示0.00000。

what url are you using? 你用的是什么网址? given that you are pulling the data from the headers, the string "x" may or may not be present in the field. 如果您从标题中提取数据,字符串“x”可能会也可能不会出现在字段中。 i would suggest NSLog-ing the NSString* object that you pull out of the dictionary and checking to see what's going on. 我建议NSLog-ing NSString *对象,你从字典中提取并检查发生了什么。 Eg:NSString *cookie = @"http://www.google.com/"; 例如:NSString * cookie = @“http://www.google.com/”;

NSHTTPCookieStorage *store = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSURL *url = [NSURL URLWithString:cookie];
NSDictionary *header = [NSHTTPCookie requestHeaderFieldsWithCookies: [store cookiesForURL:url]];
NSString *cookieParameter = [header objectForKey:@"Cookie"];    

NSLog(@"Cookie param is %@", cookieParameter);

// Test range of "x"
NSRange range = [cookieParameter rangeOfString:@"x"];
NSLog(@"%f", range.length); // will print out 0.00000
NSLog(@"%i", range.length); // will print out correct length (always 1 for "x")
NSLog(@"%i", range.location); // will print out the location of the first instance of "x"
if (range.length >= 1) {
    NSLog(@"Do Something");
} else {
    NSLog(@"AUTHING");
}

It seems like the code just detects the index of the string "x" from what I can tell, is this the intended result? 似乎代码只是根据我所知道的来检测字符串“x”的索引,这是预期的结果吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM