Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用json序列化的void函数swift 4中意外的非void返回值_Swift - Fatal编程技术网

使用json序列化的void函数swift 4中意外的非void返回值

使用json序列化的void函数swift 4中意外的非void返回值,swift,Swift,我目前正试图通过编写一个可以多次使用的通用函数来清理项目中的代码。但是,我需要我的函数返回一个数组。我的错误是“void函数中意外的非void返回值” 这是我的密码 func JSONSerialisation(JsonUrl: String) -> NSArray{ let url = URL(string: JsonUrl) let task = URLSession.shared.dataTask(with: url!) { (data, responce, erro

我目前正试图通过编写一个可以多次使用的通用函数来清理项目中的代码。但是,我需要我的函数返回一个数组。我的错误是“void函数中意外的非void返回值”

这是我的密码

func JSONSerialisation(JsonUrl: String) -> NSArray{
    let url = URL(string: JsonUrl)
    let task = URLSession.shared.dataTask(with: url!) { (data, responce, error) in
        if error != nil
        {
            print("error")

        }
        else
        {
            if let content = data
            {
                do
                {
                    if let Json = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSArray{
                        return Json //Error occurs here
                    }

                }
                catch
                {

                }

            }

        }

    }
    task.resume()

}
在堆栈溢出上有一些类似的问题,但到目前为止,没有一个能够解决我的问题,因为他们都说add
->NSArray


提前感谢

您不能直接从异步任务返回数据。使用闭包

使用类似于的闭包创建函数

func JSONSerialisation(JsonUrl: String, Completion block: @escaping ((NSArray) -> ())){
    let url = URL(string: JsonUrl)
    let task = URLSession.shared.dataTask(with: url!) { (data, responce, error) in
        if let e = error{
            print(e.localizedDescription)
        }
        else{
            if let content = data{
                do {
                    if let Json = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSArray{
                        block(Json)
                    }
                }
                catch let error{
                    print(error.localizedDescription)
                }
            }
        }
    }
    task.resume()
}
并致电:

JSONSerialisation(JsonUrl: "Your Url") { (json) in
    print(json)
}

这很有帮助

您不能直接从异步任务返回数据。使用闭包

使用类似于的闭包创建函数

func JSONSerialisation(JsonUrl: String, Completion block: @escaping ((NSArray) -> ())){
    let url = URL(string: JsonUrl)
    let task = URLSession.shared.dataTask(with: url!) { (data, responce, error) in
        if let e = error{
            print(e.localizedDescription)
        }
        else{
            if let content = data{
                do {
                    if let Json = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSArray{
                        block(Json)
                    }
                }
                catch let error{
                    print(error.localizedDescription)
                }
            }
        }
    }
    task.resume()
}
并致电:

JSONSerialisation(JsonUrl: "Your Url") { (json) in
    print(json)
}

这很有帮助,因为您的方法是
URLSession.shared.dataTask(with:)
是异步的,所以无论如何都不会工作。使用闭包。无论如何都无法工作,因为您的方法是
URLSession.shared.dataTask(with:)
是异步的。使用结束语。谢谢@AshvinGudaliyaThank you@AshvinGudaliya