意外的非空函数返回值无效函数(Swift 2.0) [英] Unexpected Non-Void Return Value In Void Function (Swift 2.0)

查看:1280
本文介绍了意外的非空函数返回值无效函数(Swift 2.0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在撇除StackOverflow的问题,试图找出我的错误,我的代码,但我只是不能似乎!我试图将我的Swift 1.2项目转换为Swift 2.0,我有一个问题,我的类下载JSON数据。



我不断收到错误



这里是我使用的代码,有些被截断了;

  ... 

class func fetchMinionData() - > [Minion] {

var myURL =http://myurl/test.json

let dataURL = NSURL(string:myURL)

let request = NSURLRequest(URL:dataURL !, cachePolicy:.ReloadIgnoringLocalAndRemoteCacheData,timeoutInterval:5.0)

let session = NSURLSession.sharedSession()

session.dataTaskWithRequest(request, completionHandler:
中的{(数据,响应,错误)let minionJSON = JSON(data!)

var minions = [Minion]()

,minionDictionary)in minionJSON {
minions.append(Minion(minionDetails:minionDictionary))
}

return minions
//这是错误发生的地方

})。resume()
}

...

也许我忽略了一些简单的东西,但我不确定为什么我的函数会被认为是无效的。任何想法将非常感谢!谢谢!

解决方案

您有问题,因为您的行:

  return minions 

不会从函数返回。相反,它从 dataTaskWithRequest 中的完成处理程序返回。而且它不应该这样做,因为闭包是一个void函数。



这个问题来自于 dataTaskWithRequest 是一个异步操作。



因此,你需要改变你的设计模式。



<这样做的一种方法如下:

  static var minions:[Minion] = [] {
didSet {
NSNotificationCenter.defaultCenter()。postNotificationName(minionsFetched,object:nil)
}
}



func fetchMinionData(){

var myURL =http://myurl/test.json
let dataURL = NSURL(string:myURL)
let request = NSURLRequest :dataURL !, cachePolicy:.ReloadIgnoringLocalAndRemoteCacheData,timeoutInterval:5.0)

let session = NSURLSession.sharedSession()

session.dataTaskWithRequest(request,completionHandler:{ ,错误)in
let minionJSON = JSON(data!)

var minions = [Minion]()

for(_,minionDictionary)in minionJSON {
minions.append(minion(minionDetails:minionDictionary))
}

self.minions = minions
//这是错误发生的地方

})。resume()
}

使用名称minionsFetched侦听 NSNotification 。只有在您收到通知后,您才能处理这些下属,就像他们被抓取一样。


I have been skimming the StackOverflow questions trying to figure out where I'm going wrong with my code, but I just can't seem to! I am trying to convert my Swift 1.2 project to Swift 2.0, and am having an issue with my class that downloads JSON data.

I am continually receiving the error Unexpected non-void return value in void function.

Here is the code, somewhat truncated, that I am using;

...

class func fetchMinionData() -> [Minion] {

    var myURL = "http://myurl/test.json"

    let dataURL = NSURL(string: myURL)

    let request = NSURLRequest(URL: dataURL!, cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 5.0)

    let session = NSURLSession.sharedSession()

    session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
        let minionJSON = JSON(data!)

        var minions = [Minion]()

        for (_, minionDictionary) in minionJSON {
            minions.append(Minion(minionDetails: minionDictionary))
        }

        return minions
        //THIS IS WHERE THE ERROR OCCURS

    }).resume()
}

...

Perhaps I am overlooking something simple, but I'm unsure why my function would be considered void at all. Any thoughts would be immensely appreciated! Thank you!

解决方案

You have a problem because your line:

return minions

does not return from your function. Instead, it returns from the completion handler in dataTaskWithRequest. And it shouldn't be doing so because that closure is a void function.

The problem which you have results from the fact that dataTaskWithRequest is an asynchronous operation. Which means that it can return later after executing your function.

So, you need to change your design pattern.

One way of doing that would be the following:

static var minions:[Minion] = [] {
    didSet {
        NSNotificationCenter.defaultCenter().postNotificationName("minionsFetched", object: nil)
   }
}



class func fetchMinionData() {

    var myURL = "http://myurl/test.json"
    let dataURL = NSURL(string: myURL)
    let request = NSURLRequest(URL: dataURL!, cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 5.0)

    let session = NSURLSession.sharedSession()

    session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
        let minionJSON = JSON(data!)

        var minions = [Minion]()

        for (_, minionDictionary) in minionJSON {
            minions.append(Minion(minionDetails: minionDictionary))
        }

        self.minions = minions
        //THIS IS WHERE THE ERROR OCCURS

    }).resume()
}

Then before calling your function you should register to listen for NSNotification with name "minionsFetched". And only after you get that notification you should process the minions as if they were fetched.

这篇关于意外的非空函数返回值无效函数(Swift 2.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆