[英]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
方法之前, didLoadJns
是nil
,但是却出现以下错误: 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.