2016-03-27 18 views
1

kullanırken hata Şu anda Alamofire ve SwiftyJSON kullanıyorum. Bu öğretici izleyin http://ashishkakkad.com/2015/10/how-to-use-alamofire-and-swiftyjson-with-swift-swift-2-ios-9-xcode-7/Alamofire

Kodum:

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 
    } 
} 

Ama bu hat giderken kaza:

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

benim Pod dosyası:

# 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 

Ben Xcode 7.2, Swift kullanmak 2.1. Herhangi bir yardım takdir edilecektir, teşekkürler. Aksi Yukarıdaki kod ile

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) 
     } 
    } 

:

+1

Zorunluluk ile Alamofire'ın isteğe bağlı sonucunu 'responseData.result.value!' Ile açarak derleyiciye şu komutu veriyorsunuz: * "Hiçbir değer olmadığında lütfen uygulamamı kilitlerim." * Uygulamanızın tam olarak ne söylediği ... // Çözüm, ücretsiz çevrimiçi Swift belgelerinin bu bölümünü okuyor: "İsteğe bağlı".] (Https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics. html # // apple_ref/doc/uid/TP40014097-CH5-ID330) – Moritz

cevap

2

Alamofire ve SwiftyJSON Eğer bu bir benzeri JSON yanıtı işleyicisi yanıt işleyicisi değiştirebilir opsiyonel kuvvet-unwrapping önlemek için çok iyi anlamak isteğe bağlı istemem gerekiyor, kuvvet-unwrapping @Eric kendi yorumunda söyledi olarak tavsiye edilmez.

Umarım bu size yardımcı olur.

+0

Tavsiyeniz için teşekkürler. – Khuong

İlgili konular