【发布时间】:2015-10-21 13:23:16
【问题描述】:
我是 swift 2 语言的新手,我正在尝试在我的项目中使用 Json 文件,所以我去使用 SwiftyJson 库框架,但我遇到了一个小问题,即我无法使用儿子返回的结果我的项目类中的文件
这是我的代码
import UIKit
class NewsViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate{
@IBOutlet weak var championlist: UICollectionView!
@IBOutlet weak var menuButton: UIBarButtonItem!
private var imagesArray = ["About" , "About" , "About" , "About" , "Logo" , "Search" , "About"]
override func viewDidLoad() {
super.viewDidLoad()
if let path = NSBundle.mainBundle().pathForResource("data", ofType: "json") {
if let data = NSData(contentsOfFile: path) {
let json = JSON(data: data, options: NSJSONReadingOptions.AllowFragments, error: nil)
print (json["keys"]["117"])
let champlist = json["keys"].count
}
}
if self.revealViewController() != nil {
menuButton.target = self.revealViewController()
menuButton.action = "revealToggle:"
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
// Do any additional setup after loading the view.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: UICollectionViewDataSource
func numberOfSectionsInCollectionView(championlist: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func collectionView(championlist: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.champlist.count
}
func collectionView(championlist: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = championlist.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell
cell.image.image = UIImage(named: imagesArray[indexPath.row])
cell.label.text = "\(indexPath.row)"
//var cell = collectionView.cellForItemAtIndexPath(indexPath)
cell.layer.borderWidth = 2.0
cell.layer.borderColor = UIColor.whiteColor().CGColor
// Configure the cell
return cell
}
}
我收到错误(使用未解析的标识符“chmplist”
【问题讨论】:
-
如果你也能添加你的json文件就更好了
-
JSON文件没问题,问题出在这里(使用未解析的标识符'chmplist')