2017-04-06 2 views
-2

저는 신속한 답변을 드리고자합니다. 나는이 코드를 시도하고 빠른 3에서 URL의 데이터를 분석하려고 해요 :alamofire json parsing swift 3

Alamofire.request("https://jsonplaceholder.typicode.com/todos").responseJSON { response in 

     if let JSON = response.result.value { 
      print("JSON: \(JSON)") 
     } 
    } 

을 그리고 아래로 JSON을 가지고 :

JSON: (
    { 
    completed = 0; 
    id = 1; 
    title = "delectus aut autem"; 
    userId = 1; 
}, 
    { 
    completed = 0; 
    id = 2; 
    title = "quis ut nam facilis et officia qui"; 
    userId = 1; 
}) 

사람은 어떻게 모든 구문 분석하는 나를 도울 수 이 값을 사전 또는 배열에 저장하고이를 신속한 3 차원 테이블 뷰에 나타낼 수 있습니까? 여기

override func numberOfSections(in tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return object.count 
} 
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return object.property.count 
} 
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) 

    cell.textLabel?.text = property.[indexPath] 

    return cell 
} 
+0

tableviewarrayData을 사용할 수 있습니다 당신에게'tableView' –

+0

단순한 테이블보기의 코드를 표시합니다. 값을 표시하기 위해 1 섹션에 4 행이 있습니다. 도와 주시면 고맙습니다. –

+0

그런 다음 질문에 해당 코드를 추가하여 TableCell에 표시 할 정보를 선택하십시오. –

답변

0

class YourData: NSObject { 

    var completed: Bool 
    var id: String 
    var title: String 
    var userId: String 


    init(dict: NSDictionary) { 
     completed = dict["completed"] as? Bool ?? false 
     id = dict["id"] as? String ?? "" 
     title = dict["title"] as? String ?? "" 
     userId = dict["userId"] as? String ?? "" 
     super.init() 
    } 
} 

구문 분석이 시도 이런 식으로 같은 모델을 만들기 내의 tableview를 구현하기위한 코드입니다.

let arrayData : [YourData] = [] 

if let JSON = response.result.value as? [Dictionary] { 
    for dict in JSON { 
     let data = YourData(dict: dict) 
     arrayData.append(data) 
    } 
} 

이제

+1

줄에서 3 : "에 대한 dict에 대한 Json", 오류 : 종류 '모든'프로토콜 '시퀀스'에 부합하지 않습니다. –

+0

응답 업데이트 확인하십시오. 확인하십시오. –

+0

** 사용하지 마십시오 'NSDictionary' Swift에서! ** 당신은 강한 타입 정보를 버립니다. – vadian

관련 문제