简体   繁体   English

无法调用非函数类型'JSON'的值,如何从Swift中的块返回值

[英]Cannot call value of non-function type 'JSON', How can I return value from block in Swift

I have a function: 我有一个功能:

func connectToWS(callback: (JSON)) -> Void {
    let urlPath: String = "http://xxx.webservice.com"
    let url: NSURL = NSURL(string: urlPath)!
    let request: NSURLRequest = NSURLRequest(URL: url)

    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithRequest(request){
        (data, response, error) -> Void in
        if error != nil {

        } else {
            let json:JSON = JSON(data: data!)
            // RETURN?
        }
    }
    task.resume()
}

All I'm trying to do is to return the NSDictionary JSON serialized dictionary when I call this function. 我要做的就是在调用此函数时返回NSDictionary JSON序列化字典。 But it seems like I can't because my json data is in the dataTaskWithRequest block. 但似乎我不能,因为我的json数据在dataTaskWithRequest块中。 I've tried using call back but it shows the error: 我尝试使用回叫,但显示错误:

Cannot call value of non-function type 'JSON'

Can anyone point me in the direction to solve this? 谁能指出我的解决方向?

I am using SwiftyJSON as a 3rd party library. 我正在使用SwiftyJSON作为第三方库。

Try this out: 试试看:

func connectToWS(callBack: ((data: NSData!, response: NSURLResponse!, error: NSError!) -> Void)?) {
    let urlPath: String = "http://xxx.webservice.com"
    let url: NSURL = NSURL(string: urlPath)!
    let request: NSURLRequest = NSURLRequest(URL: url)
    let session = NSURLSession.sharedSession()

    let task = session.dataTaskWithRequest(request) { data, response, error in
        callBack?(data: data, response: response, error: error)
        return
    }

    task.resume()
}

Then call this like: 然后像这样调用:

connectToWS() { data, response, error in
    if error != nil {
        if error.domain == NSURLErrorDomain && error.code == NSURLErrorTimedOut {
            print("timed out") // note, `response` is likely `nil` if it timed out
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 快速错误:无法调用非函数类型“ SKSpriteNode”的值 - Swift error: Cannot call value of non-function type “SKSpriteNode” 在Swift 3中无法调用非函数类型'UICollectionView'的值 - Cannot Call Value of Non-Function Type 'UICollectionView' in Swift 3 ResponseSerializer'无法使用Swift 3调用非函数类型'NSHTTPURLResponse?'的值 - ResponseSerializer 'cannot call value of non-function type 'NSHTTPURLResponse?'' with Swift 3 无法调用非函数类型的值 '((AnyObject) -> AnyObject?)!' - 斯威夫特 3 - Cannot call value of non-function type '((AnyObject) -> AnyObject?)!' - Swift 3 错误:Swift 3上的“无法调用非函数类型的值” - Error: 'Cannot call value of non-function type' on Swift 3 无法调用非功能类型的值 - Cannot call value of non-function type 无法调用非功能类型的值 - Cannot call value of non-function type Swift:类型的值没有成员,无法调用非函数类型的值 - Swift: value of type has no member, cannot call value of non-function type 无法在 Swift 中的 function 错误中调用非函数类型 'SwipeCard' 的值 - Cannot call value of non-function type 'SwipeCard' in the function error in Swift 迅速无法调用非功能类型'CLLocationCordinate2d'的值 - Cannot call value of non-function type 'CLLocationCordinate2d' in swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM