2015-10-20 132 views
0

在我的雨燕代碼來處理推送通知我有這樣的形象:使用未解決的標識符

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    var tableView:UITableView? 
    var items = NSMutableArray() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func viewWillAppear(animated: Bool) { 
     let frame:CGRect;(x:0, y: 100, width: self.view.frame.width, height: self.view.frame.height-100) 
     self.tableView = UITableView(frame: frame) 
     self.tableView?.dataSource = self 
     self.tableView?.delegate = self 
     self.view.addSubview(self.tableView!) 

     let btn = UIButton(frame: CGRect(x: 0, y: 25, width: self.view.frame.width, height: 50)) 

     btn.backgroundColor = UIColor.cyanColor() 
     btn.setTitle("EKLE", forState: UIControlState.Normal) 
     btn.addTarget(self, action: "addData", forControlEvents: UIControlEvents.TouchUpInside) 
     self.view.addSubview(btn) 
    } 
    func addData(){ 

     RestApiManager.sharedInstance.getRandomUser { json -> Void in 
      let results = json["results"] 

      for (index: String, subJson: JSON) in results{ 
       let user: AnyObject = subJson["user"].object 
       self.items.addObject(user) 
       dispatch_async(dispatch_get_main_queue(), { 
        tableView?.reloadData() 
       }) 

      } 

     } 

    } 


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return items.count 
    } 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     var cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell? 

     if cell == nil{ 
      cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "cell") 
     } 
     let user:JSON = JSON(self.items[indexPath.row]) 

     let picURL = user["picture"]["medium"].string 
     let url = NSURL(string: picURL!) 
     let data = NSData(contentsOfURL: url!) 

     cell?.textLabel?.text = user["username"].string 
     cell?.imageView?.image = UIImage(data: data!) 

     return cell! 
    } 

} 

以下是錯誤:

Use of unresolved identifier 'subJson'

符合let user: AnyObject = subJson["user"].object

怎麼會變成這樣固定?

+0

請張貼,而不是屏幕截圖的你的代碼和錯誤!請編輯你的問題。 – Raptor

+0

http://paste.ubuntu.com/12875611/ @Raptor –

+1

爲您添加編碼。 – Raptor

回答

0

如果您使用的是Swift 2(Xcode 7),則循環帶有輸入參數的字典的語法已更改。

現在你應該這樣做:

for (index, subJson):(String, JSON) in results { 
    let user: AnyObject = subJson["user"].object 
    self.items.addObject(user) 
    dispatch_async(dispatch_get_main_queue(), { 
     tableView?.reloadData() 
    }) 
} 

老辦法:

(index: String, subJson: JSON) 

新方法:

(index, subJson):(String, JSON) 
相關問題