2016-11-27 3 views
1

신속하게 json 응답을 구문 분석하기 위해 AlamofireObjectMapper를 처음 사용하고 싶습니다. 나는 이런 식으로 매핑 :JSON이 테이블 뷰로 표시됩니다.

class ModelCurrency: Mappable { 

var success : Bool? 
    var terms  : String? 
    var privacy : String? 
    var timestamp : CGFloat? 
    var source : String? 
    var quotes : [Quotes]? 

    init() {} 

    required init?(map: Map) { 

    } 

    func mapping(map: Map) { 

     success<-map["success"] 
     terms<-map["terms"] 
     privacy<-map["privacy"] 
     timestamp<-map["timestamp"] 
     source<-map["source"] 
     quotes<-map["quotes"] 

     print("It json\(terms)") 
    } 
} 

class Quotes : Mappable { 

    var name : String? 
    var val : CGFloat? 

    required init?(map: Map) { 

    } 

    func mapping(map: Map) { 
     name<-map["name"] 
     val<-map["val"] 
    } 
} 

내 컨트롤러

var arrayTable = [String]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.delegate = self 
     tableView.dataSource = self 

     super.viewDidLoad() 
     let URL = "http://www.apilayer.net/api/live?access_key=ad847a0a855c0647590df2b818923025" 

     Alamofire.request(URL).responseObject { (response: DataResponse<ModelCurrency>) in 

      let currency = response.result.value!; 

      for quotes in currency.quotes! { 


       self.arrayTable.append(quotes.name!) 

      } 
     } 

    } 


    override func numberOfSections(in tableView: UITableView) -> Int { 

     return 5} 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     return arrayTable.count } 


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath) as UITableViewCell 


     cell.textLabel?.text = self.arrayTable[indexPath.row] 


     return cell 
     } 

내가있는 tableview에 표시 배열 따옴표를 원한다. 그것을하는 방법?

답변

0

난 당신이 데이터 소스를 arrayTable 업데이트 후 reload 테이블보기에 필요가 있다고 생각 :

... 
for quotes in currency.quotes! { 
    self.arrayTable.append(quotes.name!) 
} 
self.tableView.reloadData() 
... 

게다가, 두 번 super.viewDidLoad()를 호출 할 필요가 없습니다.

+0

매우 감사합니다. –