Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Wilfredo Lopez
3,693 PointsCannot invoke 'x' with an argument list of type '(([xx]) -> ())'
Hello tree house community!
I want to share an issue I have with an app I am creating. I added a CollectionView to the Main.StoryBoard but I am getting the following errors when I try to upload JSON information (Not images yet) to each grid. Under JnsGridController.swift, in order to receive the information from my JSON files, I had to create a new method that would callback as didLoadJns. Here is the part of that code:
jns = [JsonExtrct]()
let json = JnsJson()
json.loadJns(didLoadJns)
}
func didLoadJns(jns: [JsonExtrct]){
self.jns = jns
collectionView.reloadData()
In json.loadJns(didLoadJns), didLoadJns use to be nil before I added the func method but I am getting the following error: Cannot invoke 'loadJns' with an argument list of type '(([JsonExtrct]) -> ())'
Also in JnsJson.swift, since I added that callback function in JnsGridController its going to be receiving arrays from JsonExtrct instead of AnyObject so I changed the function method from func loadJns(completion: ((AnyObject) -> Void)!) { to func loadJns(completion: ((JsonExtrct) -> Void)!) { and now I am getting a similar error as the one in JnsGridController: Cannot invoke 'dispatch_async' with an argument list of type '(dispatch_queue_t!, () -> _)'
Any help or suggestion would be appreciated!
I also have my project uploaded to GitHub.
Here is the code where the errors rely:
JnsGridController.swift
import Foundation
import UIKit
class JnsGridController : UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet var collectionView : UICollectionView!
@IBOutlet var layout : UICollectionViewFlowLayout!
var jns : [JsonExtrct]!
var cellHeight : CGFloat = 240
override func viewDidLoad() {
super.viewDidLoad()
title = "Jeans"
collectionView.delegate = self
collectionView.dataSource = self
collectionView.backgroundColor = UIColor.clearColor()
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
//let cellWidth = calcCellWidth(self.view.frame.size)
let cellWidth = self.view.frame.width/2
layout.itemSize = CGSizeMake(cellWidth, cellHeight)
jns = [JsonExtrct]()
let json = JnsJson()
json.loadJns(didLoadJns)
}
func didLoadJns(jns: [JsonExtrct]){
self.jns = jns
collectionView.reloadData()
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("JnsCell", forIndexPath: indexPath) as! JnsCell
let JsonExtrct = jns[indexPath.row]
cell.titleLabel.text = JsonExtrct.titulo
return cell
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return jns.count
}
}
JnsJson.swift
import Foundation
class JnsJson {
func loadJns(completion: ((JsonExtrct) -> Void)!) {
var urlString = "http://xsgn.xxxhost.com/jsn/jnslst.json"
let session = NSURLSession.sharedSession()
let jnsUrl = NSURL(string: urlString)
//let jnsUrl = NSURL(scheme: "http", host: "xdsgn.xxxhost.com", path: "/jsn/jnslst.json")
var task = session.dataTaskWithURL(jnsUrl!){
(data, response, error) -> Void in
if error != nil {
println(error.localizedDescription)
} else {
var error : NSError?
var jnsData = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: &error) as! NSArray
var jns = [JsonExtrct]()
for jnsextrct in jnsData{
let jnsextrct = JsonExtrct(data: jnsextrct as! NSDictionary)
jns.append(jnsextrct)
}
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
dispatch_async(dispatch_get_main_queue()) {
completion?(jns)
}
}
}
}
task.resume()
}
}