Ahmet Kocaman Ahmet Kocaman - 4 months ago 7
Swift Question

I can not get the json values for loop in swift

I'm new to swift. I received a json I want to break into the value of this for loop. My code is below.

func test(){
Alamofire.request(.POST, "url", parameters:nil)
.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization

if let JSON = response.result.value {
print("JSON: \(JSON)")
let mansetArray = response.result.value!["mansetler"] as! NSArray

for var i = 0; i < mansetArray.count; ++i {
let item = mansetArray[i] as? [String: AnyObject]
let name = item!["news_title"] as? [String: AnyObject] //eror
print(item) //print: nil, nil, nil, nil, nil, nil, nil, nil


format as:

[ ]mansetler
{ } 0
news_id : "value1"
news_title : "value2"
news_photo_headline : "value3"
headline_category_title : "value4"
news_date : "09 June 2016, Thursday"
news_readed_count : "36"

Thanks for help.


You need to do the following changes.

if let JSON = response.result.value as? [String:AnyObject] 

typecast objects as per your JSON

let name = item!["news_title"] as? String