2016-03-27 19 views
1

Ich verwende jetzt Alamofire und SwiftyJSON. Ich folge diesem Tutorial http://ashishkakkad.com/2015/10/how-to-use-alamofire-and-swiftyjson-with-swift-swift-2-ios-9-xcode-7/Fehler bei der Verwendung von Alamofire

Mein Code:

import UIKit 
import Alamofire 
import SwiftyJSON 

class ViewController: UIViewController, UITableViewDataSource { 

    @IBOutlet var tblJSON: UITableView! 
    var arrRes = [[String:AnyObject]]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     tblJSON.dataSource = self 

//  Alamofire.request(.GET, "http://api.androidhive.info/contacts/").response { (request, response, data, error) -> Void in 
//   print(response) 
//   let outputString = NSString(data: data!, encoding: NSUTF8StringEncoding) 
//   print(outputString) 
//  } 

     Alamofire.request(.GET, "http://api.androidhive.info/contacts/").responseJSON { (responseData) -> Void in 
      let swiftyJsonVar = JSON(responseData.result.value!) 

      if let resData = swiftyJsonVar["contacts"].arrayObject { 
       self.arrRes = resData as! [[String:AnyObject]] 
      } 
      if self.arrRes.count > 0 { 
       self.tblJSON.reloadData() 
      } 
     } 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return arrRes.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tblJSON.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell 
     var dict = arrRes[indexPath.row] 
     cell.textLabel?.text = dict["name"] as? String 
     cell.detailTextLabel?.text = dict["email"] as? String 
     return cell 
    } 
} 

Aber es an dieser Linie abgestürzt:

let swiftyJsonVar = JSON(responseData.result.value!) 

meine Pod-Datei:

# Uncomment this line to define a global platform for your project 
platform :ios, '8.0' 
# Uncomment this line if you're using Swift 
use_frameworks! 

target 'UsingAlamofireAndSwiftyJSON' do 
    pod 'Alamofire', '~> 3.2.1' 
    pod 'SwiftyJSON', :git => 'https://github.com/SwiftyJSON/SwiftyJSON.git' 
end 

Ich verwende Xcode 7.2, Swift 2.1. Irgendwelche Hilfen würden geschätzt werden, danke.

+1

Wenn Sie das optionale Ergebnis von Alamofire mit "responseData.result.value" erzwingen, sagen Sie dem Compiler: "Bitte stürzen Sie meine App ab, wenn kein Wert vorhanden ist." * Ihre App macht genau das, was ihr gesagt wurde ... // Die Lösung ist zu lesen [dieses Kapitel der kostenlosen Online-Swift-Dokumentation: "Optionals".] (Https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics. html # // apple_ref/doc/uid/TP40014097-CH5-ID330) – Moritz

Antwort

2

Alamofire und SwiftyJSON verstehen es sehr gut, die Kraft-Abwickeln des optionalen vermeiden Sie die Antwort-Handler wie diese auf die JSON-Antwort-Handler ändern können:

Alamofire.request(.GET, "http://api.androidhive.info/contacts/").responseJSON { response in 

     switch(response.result) { 
     case .Success(let value):  
      let json = JSON(value) 
     case .Failure(let error): 
      print(error.description) 
     } 
    } 

Mit dem obigen Code, den Sie nicht tun müssen das optionale, das force-unwrapping entpacken, das ist nicht empfehlenswert, wie @Eric in seinem Kommentar sagte.

Ich hoffe, das hilft Ihnen.

+0

Vielen Dank für Ihren Rat. – Khuong

Verwandte Themen