我想用NSNull处理SwiftyJSON返回值,但我不知道怎么做?如果达到超时,我会使用object : NSNull()
这是我的代码:
func ping(url:String) -> JSON {
var startTime = NSDate.timeIntervalSinceReferenceDate()
var waiting = true
var JSONresponse:JSON!
Alamofire
.request(.GET, myURL)
.responseJSON {
(request, response, data, error) -> Void in
JSONresponse = JSON(object: data!)
waiting = false
}
while (waiting) {
NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate())
usleep(10)
var timeout:Double = 10.0
var currentTime = NSDate.timeIntervalSinceReferenceDate()
var elapsedTime: NSTimeInterval = currentTime - startTime
if (elapsedTime > timeout)
{
waiting = false
//JSONresponse returns NSNull() on timeout
JSONresponse = JSON(object: NSNull())
}
}
return JSONresponse
}
如何解析返回值,即在if子句中?如果返回值来自类型NSNull
,我想生成错误消息。
THX!