简体   繁体   English

Swift:2个类似的错误:无法使用类型为(([[xx])->())'的参数列表调用'x'

[英]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. 我向Main.StoryBoard添加了CollectionView ,但是当我尝试将JSON信息(尚无图像)上传到每个网格时,出现以下错误。 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 . JnsGridController.swift下 ,为了从JSON文件接收信息,我必须创建一个新方法,该方法将像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]) -> ())' json.loadJns(didLoadJns) ,在我添加func方法之前, didLoadJnsnil ,但是却出现以下错误: 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!, () -> _)' 同样在JnsJson.swift中 ,由于我在JnsGridController中添加了回调函数, 因此它将从JsonExtrct而不是AnyObject接收数组,因此我将函数方法从func loadJns(completion: ((AnyObject) -> Void)!) {更改为func loadJns(completion: ((JsonExtrct) -> Void)!) {现在我收到与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 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: loadJns具有以下签名:

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

didLoadJns has this signature: didLoadJns具有此签名:

func didLoadJns(jns: [JsonExtrct]){

An array of JsonExtrct is not the same thing as a single JsonExtrct . JsonExtrct数组与单个JsonExtrct Either change loadJns to accept an array, or change didLoadJns to accept a single element. 更改loadJns以接受数组,或更改didLoadJns以接受单个元素。

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. 隐式解包的可选参数用于与尚未经过可空性审核的ObjC方法进行交互。 This isn't that case (and you treat completion as a normal optional in this method). 并不是这种情况(在这种方法中,您将completion视为正常的可选操作)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift 4无法使用类型为实参的列表调用'index' - Swift 4 Cannot invoke 'index' with an argument list of type 在Swift xcode 6.3.1中无法使用类型为'([[(nameOfClass)])'的参数列表调用'functionName' - Cannot invoke 'functionName' with an argument list of type '([(nameOfClass)])' in Swift xcode 6.3.1 Swift数组:无法使用类型'(Int)'的参数列表调用'append' - Swift Array: Cannot invoke 'append' with an argument list of type '(Int)' 无法使用参数“ NSNumber”(Swift)的类型调用“ init” - Cannot invoke 'init' with argument of type 'NSNumber' (Swift) 无法为 type: 调用初始化程序,参数列表类型为: - Cannot invoke initializer for type: with an argument list of type: 无法使用类型为((of:Any)'的参数列表调用索引 - Cannot invoke index with an argument list of type '(of: Any)' 无法使用类型为(T)的参数列表调用“追加” - cannot invoke 'append' with an argument list of type '(T)' Swift 2.0过滤自定义对象的数组-无法使用列表类型的参数调用“过滤器” - Swift 2.0 filtering array of custom objects - Cannot invoke 'filter' with an argument of list type Swift:在字典数组上使用过滤器功能吗? 错误:无法使用类型为参数的列表调用“过滤器” - Swift: use filter function on array of dictionaries? Error: Cannot invoke 'filter' with an argument list of type Swift无法使用类型为“([Score],Score)”的参数列表调用“find”,其中Score为结构 - Swift Cannot invoke 'find' with an argument list of type '([Score], Score)' where Score is a struct
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM