我正在 Swift 中创建一个函数,该函数调用服务器来确定下一次每周抽奖的时间。我编写了 PHP 脚本,用于发送当前 unix 时间戳和下一场比赛的 unix 时间戳之间的差异。但是,当我调用此脚本时,它返回 nil,而不是我需要的时间戳。我已经在这里发布了我的代码:
func getTimeInSecondsToNextWeeklyWinner() -> Int {
let url = NSURL(fileURLWithPath: kTimeToNextWeeklyWinner);
let request = NSMutableURLRequest(URL: url)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
print("Response: \(response)")})
task.resume()
return 0
}
最终打印:
Response: nil
我希望它打印出这样的内容:
Response: 425352
知道为什么会发生这种情况吗?任何帮助将不胜感激。
最佳答案
如果您要进行服务器调用,请使用 URLWithString 而不是 fileURLWithPath。后者用于获取设备的本地文件。 另请检查您的网络服务是否使用 PostMan 返回响应
关于ios - NSSession 的 dataTaskWithResult 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36158179/