【问题标题】:can't use returend result Json in any class不能在任何类中使用返回结果 Json
【发布时间】: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')

标签: json swift swift2


【解决方案1】:

您不能在作用域(大括号集)内定义变量,然后将其返回到作用域之外。 这样做

var chmplist = 0
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"]) // this print Moha
      chmplist = (json["keys"].count) // this print number of keys
    }
}
return chmplist

UPDATE:看到更新后的代码:

championlist 和 champlist 不一样 :)。

【讨论】:

  • 在 void 函数中出现意外的非 void 返回值,,
  • @MohammedAwad,请编辑问题并发布整个函数的代码。
  • done ,,问题出在这里 return self.champlist.count Use of unresolved identifier 'chmplist'
  • self.champlist.count 必须是 self.championlist.count
  • 同样的错误 :'( ,, void 函数中出现意外的非 void 返回值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-12
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 2017-12-09
相关资源
最近更新 更多