[英]Found nil when unwrapping non nil value
I'm trying to make a get request in swift passing the following parameters 我正在尝试通过swift传递以下参数来获取get请求
var dict = [
"id_struttura" : 2,
"prenCheck" : [
"codice" : "14:30_15:20_1_3831_0",
"id_sport" : 6,
"ora_fine" : "20/02/2015 15:20:00",
"ora_inizio" : "20/02/2015 14:30:00",
"rec" : 0,
"soci" : [
[
"esterno" : 0,
"id" : "1980"
],
[
"esterno" : 0,
"id" : "51"
]
]
]
]
using the following method: 使用以下方法:
private func genericRequest(method: Methods, url: String, data: [String: AnyObject]?) -> AnyObject?{
var getRequest : NSURLRequest?
var postRequest : NSMutableURLRequest?
if method == Methods.GET{
var completeUrl = url
if(data != nil){
//println(data)
completeUrl += "?"
for (key, value) in data! {
completeUrl += "\(key)=\(value)&"
}
completeUrl = completeUrl.substringToIndex(completeUrl.endIndex.predecessor())
}
//println(completeUrl)
let url = NSURL(string: completeUrl)
getRequest = NSURLRequest(URL: url!)
}else if method == Methods.POST{
postRequest = NSMutableURLRequest(URL: NSURL(string: url)!)
var session = NSURLSession.sharedSession()
postRequest!.HTTPMethod = "POST"
var params : String = ""
for (key, value) in data! {
params += "\(key)=\(value)&"
}
params = params.substringToIndex(params.endIndex.predecessor())
var err: NSError?
postRequest!.HTTPBody = params.dataUsingEncoding(NSUTF8StringEncoding);
}
var response: NSURLResponse? = nil
var error: NSError? = nil
var request = getRequest != nil ? getRequest! : postRequest!
let reply = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&error)
//println((response as NSHTTPURLResponse).statusCode)
let results = NSString(data:reply!, encoding:NSUTF8StringEncoding)
var json: AnyObject? = convertDataToJSON(reply)
return json
}
calling it as genericRequest(Method.GET ,url:url, data: dict)
(as Method is simply an enum) it can't unwrap the data variable in for (key,value)in data!
将其称为genericRequest(Method.GET ,url:url, data: dict)
(因为Method只是一个枚举)它无法解包数据中for (key,value)in data!
数据变量for (key,value)in data!
when making a get request, but it can when it's making a post request. 在发出get请求时,它可以在发出请求时发出请求。 Since I have to use only get requests for this task (i'm dealing with a bad rest interface), could someone help me? 由于我必须只使用获取此任务的请求(我正在处理一个糟糕的休息界面),有人可以帮助我吗?
UPDATE: I also tried with if let data = data { ...
and it crashes on this line saying unexpectedly found nil while unwrapping an Optional value
更新:我也试过if let data = data { ...
它在这一行崩溃说unexpectedly found nil while unwrapping an Optional value
You are using a lot of force unwrapping ( !
). 你正在使用大量的力量展开( !
)。 Each place where you do that is a place where you are promising the compiler that the optional will never ever be nil. 你所做的每个地方都是一个你承诺编译器的地方,可选的永远不会是零。 In most places I think that you should allow the compiler to prove the nil safety by using other methods of dealing with optionals (optional chaining, nil coalescing, map etc.) 在大多数地方,我认为您应该允许编译器通过使用其他处理选项的方法来证明零安全性(可选链接,零合并,映射等)
In your specific case I don't know why you aren't doing: 在你的具体情况下,我不知道你为什么不这样做:
if let data = data {
instead of: 代替:
if (data != nil) {
and then having to force unwrap data with !
然后不得不强制打开数据!
. 。
I recently blogged and talked about the tools for dealing with optionals in Swift if you want more information about how not force unwrap. 我最近在博客上 谈到了在Swift中处理选项的工具,如果你想了解更多关于如何不强制解包的信息。
Why you are not checking if data is nil in your POST method as you did in GET method. 为什么不像在GET方法中那样检查POST方法中的数据是否为nil。 I think the post method is where you are trying to unwrap nil 我认为post方法是你试图解开nil的地方
if method == Methods.GET{
var completeUrl = url
// here you check if data is nil
if(data != nil){
//println(data)
completeUrl += "?"
for (key, value) in data! {
completeUrl += "\(key)=\(value)&"
}
completeUrl = completeUrl.substringToIndex(completeUrl.endIndex.predecessor())
}
//println(completeUrl)
let url = NSURL(string: completeUrl)
getRequest = NSURLRequest(URL: url!)
}else if method == Methods.POST{
postRequest = NSMutableURLRequest(URL: NSURL(string: url)!)
var session = NSURLSession.sharedSession()
postRequest!.HTTPMethod = "POST"
var params : String = ""
//check here as well
if data != nil{
for (key, value) in data! {
params += "\(key)=\(value)&"
}
params = params.substringToIndex(params.endIndex.predecessor())
}
var err: NSError?
postRequest!.HTTPBody = params.dataUsingEncoding(NSUTF8StringEncoding);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.