0

I am Facing Problem while Using AlamofireObjectMaper in Alamofire for fetching Array of my Categorey Object

My Object :

import Foundation
import ObjectMapper

class Category: Mappable {

    public var iDCategory: Int?
    public var nameCategory: String?
    public var img: String?

    func mapping(map: Map) {
        iDCategory <- map["IDCategory"]
        nameCategory <- map["NameCategory"]
        img <- map["img"]
    }

    required init?(map: Map) {

    }

}

and my call :

import Foundation
import UIKit
import Alamofire

class ControllerSearch: UIViewController {

    override func viewDidLoad() {

        Alamofire.request("http://pazirandegan.farhadarghavan.ir/api/ApiCategory")
            .responseArray { (response: DataResponse<[Category]>) in
                for a in response.value!{
                    print(a.nameCategory!)
                }

        }

    }
}

JsonArray:

[{"IDCategory":2,"NameCategory":"رستوران و کافه","img":"/Upload/Images/eb0c3cd1-d911-43bb-bf59-9b736a98c49e.PNG"},{"IDCategory":3,"NameCategory":"پوشاک و البسه","img":"/Upload/Images/5da2b69a-3063-4b34-a74b-64ba8fb166c6.jpg"},{"IDCategory":4,"NameCategory":"پزشکی و سلامت","img":"/Upload/Images/a8bb9506-7c31-4ed0-b05b-41d9a760ab43.jpg"},{"IDCategory":5,"NameCategory":"فروشگاه و زنجیره ای","img":null},{"IDCategory":6,"NameCategory":"لوازم خانگی","img":null},{"IDCategory":7,"NameCategory":"زیبایی","img":null},{"IDCategory":8,"NameCategory":"فرهنگ و هنر","img":"/Upload/Images/43958739-0f41-489f-882c-65c2e2fd6eb1.jpg"},{"IDCategory":9,"NameCategory":"خودرو","img":null},{"IDCategory":10,"NameCategory":"کالای دیجیتال","img":null}]

No Errors are there and Output is Perfect, But my App Crashes

Note : When Alamofire responseString is Used Everything Works Fine.

Whats the Problem.

Abhirajsinh Thakore
  • 1,806
  • 2
  • 13
  • 23
  • Please include your actual response to see why it cannot be parsed. And don't force unwrap `response.value` or `a.nameCategory`, one of those two is causing the issue. – Dávid Pásztor Aug 25 '17 at 15:22
  • @DávidPásztor i puted json array in question and i tested without for loop and still crash – Farhad Arghavan Aug 25 '17 at 15:29
  • So your code looks like this and it still crashes? `.responseArray { (response: DataResponse<[Category]>) in print(response) }` Set up an exception breakpoint in debugger and check the error. – Dávid Pásztor Aug 25 '17 at 15:42
  • @DávidPásztor i get this error "fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) " – Farhad Arghavan Aug 25 '17 at 16:26
  • I would give it a try to see if the problem might happen due to the arabic characters, so if you can, test your code with a JSON response mimicking this one, but without the arabic characters. – Dávid Pásztor Aug 25 '17 at 17:44
  • @DávidPásztor thank you, plz help to fix this :) if it not working i must parse json manualy, i dont like do it – Farhad Arghavan Aug 25 '17 at 20:22
  • Did you check if its working with non arabic characters? – Dávid Pásztor Aug 25 '17 at 22:03
  • @DávidPásztor yes and its working without crash – Farhad Arghavan Aug 25 '17 at 22:13
  • Have a look at highest voted answer for [this](https://stackoverflow.com/questions/39699642/alamofire-4-and-special-characters-in-json) question, I think you have the same problem, that your JSON is not .utf8 encoded – Dávid Pásztor Aug 25 '17 at 22:19

0 Answers0