查找 cookie 是否有变量值
在我之前在 stackoverflow 上找到的答案(现在找不到)中,有人建议使用 NSRange rangeOfString 来查看特定变量中是否有来自 cookie 的数据:
NSRange range = [[[NSHTTPCookie requestHeaderFieldsWithCookies:[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:cookie]]] objectForKey:@"Cookie"] rangeOfString:@"variable"];
if (range.length != NSNotFound) {
//it has data
} else {
//it doesn't
}
从哪些资源中我可以找到这应该有效,但事实并非如此,即使 cookie 为空。
我做错了什么?
In a previous answer I found on stackoverflow (can't find now) someone suggested using NSRange rangeOfString to see if a particular variable had data in it from a cookie:
NSRange range = [[[NSHTTPCookie requestHeaderFieldsWithCookies:[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:cookie]]] objectForKey:@"Cookie"] rangeOfString:@"variable"];
if (range.length != NSNotFound) {
//it has data
} else {
//it doesn't
}
From what resources I can find this should work, but it doesn't, even when the cookie is null.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这有点奇怪。即使 range.length 显然为 null,如果您记录输出,您也无法检查其是否为 null 并且 NSNotFound 是否未按预期工作。您必须检查该值是否 > 0。
This is a bit odd. Even though range.length is apparently null if you log the output you can't check if its null and NSNotFound doesn't work as expected. You have to check if the value > 0.