我向 Main.StoryBoard
添加了 CollectionView
,但当我尝试将 JSON 信息(还不是图像)上传到每个网格时,出现以下错误。在 JnsGridController.swift 下,为了从我的 JSON
文件接收信息,我必须创建一个以 didLoadJns
形式回调的新方法。这是该代码的一部分:
jns = [JsonExtrct]()
let json = JnsJson()
json.loadJns(didLoadJns)
}
func didLoadJns(jns: [JsonExtrct]){
self.jns = jns
collectionView.reloadData()
在 json.loadJns(didLoadJns)
中,在添加 func
方法之前,didLoadJns
的值是 nil
但我收到以下错误:无法使用类型为“(([JsonExtrct]) -> ())”的参数列表调用“loadJns”
同样在 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!, () -> _)'
如有任何帮助或建议,我们将不胜感激!
这是错误所在的代码:
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
具有以下签名:
func loadJns(completion: ((JsonExtrct) -> Void)!) {
didLoadJns
具有以下签名:
func didLoadJns(jns: [JsonExtrct]){
JsonExtrct
数组与单个 JsonExtrct
不同。更改 loadJns
以接受数组,或更改 didLoadJns
以接受单个元素。
无论哪种情况, !
都不是您的意思。看看其余的代码,您的意思是 ?
:
func loadJns(completion: ((JsonExtrct) -> Void)?) {
隐式解包选项用于与尚未审核可空性的 ObjC 方法进行交互。情况并非如此(您在此方法中将完成
视为正常的可选操作)。
关于arrays - Swift:2 个类似错误:无法使用 'x' 类型的参数列表调用 '(([xx]) -> ())',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31907789/