简体   繁体   中英

Swift: 2 similar errors: Cannot invoke 'x' with an argument list of type '(([xx]) -> ())'

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!

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()
    }

}

loadJns has this signature:

func loadJns(completion: ((JsonExtrct) -> Void)!) {

didLoadJns has this signature:

func didLoadJns(jns: [JsonExtrct]){

An array of JsonExtrct is not the same thing as a single JsonExtrct . Either change loadJns to accept an array, or change didLoadJns to accept a single element.

In either case, the ! is not what you mean. Looking at the rest of the code, you meant ? :

func loadJns(completion: ((JsonExtrct) -> Void)?) {

Implicitly unwrapped optionals are for interacting with ObjC methods that have not yet been audited for nullability. This isn't that case (and you treat completion as a normal optional in this method).

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM