- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试学习如何将 swiftHTTP 与 mishap api ( https://www.mashape.com/textanalysis/textanalysis ) 一起使用。到目前为止,这是我的代码,
import SwiftHTTP
func splitSentenceIntoWordsUsingTextAnalysis (string: String) -> String {
var request = HTTPTask()
var params = ["text": "这是中文测试"] //: Dictionary<String,AnyObject>
//request.requestSerializer = JSONRequestSerializer()
request.requestSerializer.headers["X-Mashape-Key"] = "My-API-Key"
request.requestSerializer.headers["Content-Type"] = "application/x-www-form-urlencoded"
request.responseSerializer = JSONResponseSerializer()
request.POST("https://textanalysis.p.mashape.com/segmenter", parameters: params, success: {(response: HTTPResponse) in if let json: AnyObject = response.responseObject { println("\(json)") } },failure: {(error: NSError, response: HTTPResponse?) in println("\(error)") })
// {
// result = "\U4f60 \U53eb \U4ec0\U4e48 \U540d\U5b57";
// }
return ?? // I want to return the "result" in the json as a string.
}
如何将 json 中的“结果”作为字符串返回?
最佳答案
SwiftHTTP 和 NSURLSession 一样,在设计上是异步的。这意味着您不能只从该方法返回。
import SwiftHTTP
func splitSentenceIntoWordsUsingTextAnalysis (string: String, finished:((String) -> Void)) {
var request = HTTPTask()
var params = ["text": "这是中文测试"] //: Dictionary<String,AnyObject>
//request.requestSerializer = JSONRequestSerializer()
request.requestSerializer.headers["X-Mashape-Key"] = "My-API-Key"
request.requestSerializer.headers["Content-Type"] = "application/x-www-form-urlencoded"
request.responseSerializer = JSONResponseSerializer()
request.POST("https://textanalysis.p.mashape.com/segmenter", parameters: params, success: {
(response: HTTPResponse) in
if let res: AnyObject = response.responseObject {
// decode res as string.
let resString = res as String
finished(resString)
}
}, failure: {(error: NSError, response: HTTPResponse?) in
println(" error \(error)")
})
}
然后你会像这样使用它。
splitSentenceIntoWordsUsingTextAnalysis("textToSplit", {(str:String) in
println(str)
// do stuff with str here.
})
另请参阅此 Github 问题。
关于ios - 如何将此 swiftHTTP 函数的 json 属性作为字符串返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27723917/
我尝试解析来自这样的服务器的 json 响应 HTTP.GET(ServerPatientApi.SPLASH, parameters: nil) { response in
我正在使用 SwiftHTTP在 iOS 应用程序中执行 Web 服务器中的基本身份验证。如果身份验证成功,一切都会按预期进行。但是,如果身份验证失败(代码为 401),调用失败处理程序大约需要 35
我在 swift 中创建了一个新的 Xcode 7 项目。然后我退出 Xcode,并创建 pod 文件。 pod init 然后我使用提供的配置编辑文件: source 'https://github
我将 SwiftHTTP.xcodeproject 添加到我的项目中, 在构建阶段:我添加 4 个 SwiftHTTP.framework ViewController --- 导入 SwiftHTT
我突然开始观察到我的一个应用程序的新更新出现以下崩溃。它似乎经常发生,并且令人困惑,因为我在过去 6 个月内没有对代码的这一部分进行任何更改。我确实想知道 this (EXC_BAD_ACCESS 返
我正在使用 swiftHTTP 向我的服务器发出请求,当我的互联网连接速度较慢时,它会转到响应部分!我在下面设置了示例代码: HTTP.GET("myURL") { response in let
我正在尝试在 Swift 中创建一个 Twitter 时间轴。我正在使用 SimpleAuth 以及 SwiftHTTP。我在另一个类中成功授权了 Twitter 并获得了访问 token 和用户 I
我正在尝试学习如何将 swiftHTTP 与 mishap api ( https://www.mashape.com/textanalysis/textanalysis ) 一起使用。到目前为止,这
重要事实 I forgot to mention an important factor in the question. I am running this in a TestCase. I thi
我是一名优秀的程序员,十分优秀!