I'm trying to reload a collection view after the json data has been updated but am running into the error:
Cannot assign value of type '()' to type '[ChannelInfo]'
This is how I have the custom object 'ChannelInfo' setup:
class ChannelInfo: NSObject {
var logo: String?
var channelName: String?
var id: NSNumber?
init(logo: String, channelName: String , id: NSNumber) {
self.logo = logo
self.channelName = channelName
self.id = id
}
init(resDictionary: [String:Any]) {
logo = resDictionary["artwork_608x342"] as? String
channelName = resDictionary["name"] as? String
id = resDictionary["id"] as? NSNumber
}
static func updateAllChannels(completionHandler:@escaping (_ channels: [ChannelInfo]) -> Void){
let nm = NetworkManager.sharedManager// singleton handles network connections and json parsing
nm.getJSONData(urlExtension: "channels/all/0/50", completion: {
data in
var channels = [ChannelInfo]()
if let jsonDictionary = nm.parseJSONFromData(data)
{
let resultsDictionaries = jsonDictionary["results"] as! [[String : Any]]
for resultsDictionary in resultsDictionaries {// enumerate through dictionary
let newChannel = ChannelInfo(resDictionary: resultsDictionary)
channels.append(newChannel)
}
}
completionHandler(channels)
})
}
}
This is how I reload the collection view after the json objects have been updated:
var channelArray: [ChannelInfo] = []
override func viewDidLoad() {
channelArray = ChannelInfo.updateAllChannels(completionHandler: { _ in
self.channelCollectionView.reloadData()
})//Error happens here starting with 'channelArray'
}
I get the feeling I may be missing a step, any help is appreciated.
Okay, let's take this one step at a time. You are saying
ChannelInfo.updateAllChannels...
So let's look at how that updateAllChannels
method is declared:
static func updateAllChannels(
completionHandler:@escaping (_ channels: [ChannelInfo]) -> Void){
So this a static function that returns no result .
But what you are actually saying is:
channelArray = ChannelInfo.updateAllChannels...
But we have just established that updateAllChannels
returns no result . So there is nothing there for you to assign to channelArray
. The whole notion of assignment here makes no sense. And the compiler is telling you so. You can't have this assignment channelArray =
. Delete that part.
I think (but don't quote me) that your larger problem is that you are ignoring the value returned to the completion handler. You have this:
ChannelInfo.updateAllChannels(completionHandler: { _ in
self.channelCollectionView.reloadData()
})
See the _ in
at the end of the first line? I think that's your whole issue. updateAllChannels
is handing you back the channels
as the parameter and you are throwing them away . Instead, I think you want something like this:
ChannelInfo.updateAllChannels(completionHandler: { channels in
channelArray = channels
self.channelCollectionView.reloadData()
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.