- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试使用 PHP API 和 Swift 客户端在 Xcode Playground 中测试 OAuth2 实现。基本上,我的代码看起来像这样
let url = NSURL(string: "http://localhost:9142/account/validate")!
var request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody!.setValue("password", forKey: "grant_type")
// Other values set to the HTTPBody
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) in
// Handle response here
}
但是当我实例化 url 变量时,我不断收到这个错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
我尝试在实例化它时不展开它,而是在我使用它时尝试它,它没有改变任何东西,错误出现在我第一次展开它时。
越来越奇怪了..下面
let url = NSURL(string: "http://localhost:9142/account/validate")!
println(url)
输出
http://localhost:9142/account/validate fatal error: unexpectedly found nil while unwrapping an Optional value
我真的不明白错误是从哪里来的,因为我是 Swift 的新手
最佳答案
发生的事情是您被迫展开设置为 nil 的 HTTPBody,导致此行出现运行时错误:
request.HTTPBody!.setValue("password", forKey: "grant_type")
您需要为请求主体创建一个 NSData 对象,然后按照以下代码将其分配给 request.HTTPBody:
let url = NSURL(string: "http://localhost:9142/account/validate")!
var request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
// Create a parameter dictionary and assign to HTTPBody as NSData
let params = ["grant_type": "password"]
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions.allZeros, error: nil)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) in
// Handle response here
}
我希望这有助于解决您的问题。
为了在不使用 JSON 序列化器的情况下序列化数据,您可以创建自己的类似于下面的序列化器:
func dataWithParameterDictionary(dict: Dictionary<String, String>) -> NSData? {
var paramString = String()
for (key, value) in dict {
paramString += "\(key)=\(value)";
}
return paramString.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: false)
}
然后这样调用它:
let dict = ["grant_type": "password"]
let data = dataWithParameterDictionary(dict)
request.HTTPBody = data
关于php - Xcode- swift -NSURL : "fatal error: unexpectedly found nil while unwrapping an Optional value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28765260/
func initializePickerViewProperties() { let font = UIFont (name: "SanFranciscoDisplay-Regular",
我试图将预先附加的字符串作为 URL 请求传递,但我不断收到错误消息: fatal error :在展开可选值时意外发现 nil 此错误指向行:let searchTerm = "http://goo
我有一个像这样的html结构 旁边的两个 div 确实有类和子元素,但为了这个问题,您不需要知道它们是什么。在 DOM 准备就绪后,我将两个内部 div 包裹在一个新的 div 中,就
显然是我的语法错误。 我有大量的 div,其中少量的内容包含在 anchor 中。我要做的是移除 anchor 。 感谢您的帮助! http://jsfiddle.net/danielredwood/
关闭。这个问题是 opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 已关闭 2 年前。 奥 git _
我已经包装了阶段,我想使用“wrap”函数来打开它们。但是,在这种情况下,由于我不清楚的原因,我无法获得展开的相位。 包裹的阶段是 通过观察横截面,我可以看到相被包裹 我已将图像从 0 缩放到 2*p
用 jQuery 包装 div 后,展开它们然后尝试再次包装它们似乎会丢失 float 属性(我的猜测),我不知道如何解决这个问题。 当第一次按下 JSFiddle 中的换行按钮时,它会像我期望的那样
我将容器 .vorteile 通过 jQuery 包装在 .vorteile_outer 中。当我想在 .vorteile 上使用 .unwrap() 删除 .vorteile_outer 时,.vo
这个问题已经有答案了: jQuery : remove element except inside element (4 个回答) 已关闭 9 年前。 jquery中有一个很棒的方法叫做wrap()它
这个问题已经有答案了: jQuery : remove element except inside element (4 个回答) 已关闭 9 年前。 我有一段 HTML 代码: table 我如何从
当用户提交或聚焦无效输入时,我有一个包含在错误 div 中的文本字段。如果用户纠正了错误,我想删除包装器,但是,unwrap() 方法会导致字段失去焦点。 $(special_input).live(
我正在寻找一种方法来“解开”已发布到 MVC 核心服务的 JSON。比方说,我有以下方法: [HttpPost] public dynamic SayHello(string First
我引用了以下网页: https://serverfault.com/questions/153983/sockets-found-by-lsof-but-not-by-netstat 使用Python
我有一个 jQuery 突出显示工具,它可以动态地将一个类添加到一个字符串中。如果一个元素包含单词 Hello World 并且用户搜索'ell',它为单词添加了一个跨度 Hello world 要重
如何在 Iframe 中展开 span 标签。 Directional 我尝试使用此删除 $('iframe').contents().find(".newtext").unwrap(); 如果 re
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 4 年前。 Improve
在我的代码中,我有很多带有 Option 的结构在他们里面。我需要在很多地方与他们一起工作,所以我的代码中充斥着像 car.engine.as_ref().unwrap() 这样的结构访问。 .这对代
这个问题在这里已经有了答案: Unable to read file contents to string - Result does not implement any method in sco
出于某种原因,Rust 编译器提示 Result 没有实现 unwrap,即使我提供的错误类型确实实现了 Debug。下面提供了出错的代码。 use std::fmt::{Display, Debug
多么奇怪的错误: use std::collections::BTreeMap; struct MyStruct1; struct Error; fn get_res() -> Result), Er
我是一名优秀的程序员,十分优秀!