sdadsad sdadsad - 24 days ago 11
Swift Question

JSON to NSMutableArray stopped working in Swift 2.3 iOS 10

I have a code to consume a REST Service but it stop working after update to swift 2.3 and iOS 10.
I'm getting error in JSON to NSMutableArray Conversion:

class func getStores(latitude:Float, longitude: Float, completion : (Array<Store>?, NSError?) -> ()) {
let latString: String = "\(latitude)"
let lonString: String = "\(longitude)"
var listStores: Array<Store> = []
let urlFull : String = ConstantHelper.baseURLStore
Alamofire.request(.GET, urlFull, parameters: ["latitude":latString, "longitude": lonString])
.responseJSON { response in
switch response.result {
case .Success:
if let repoJSON = response.result.value {
print(repoJSON)
let jsonArray = repoJSON as? NSMutableArray
for item in jsonArray! {
guard let store = Store(json: item as! JSON) else
{
print("Issue deserializing model")
return
}
listStores.append(store)
}
completion(listStores, nil)
}
break
case .Failure(let error):
completion(nil, error)
break
}
}
}


EDIT:

The error is "fatal error: unexpectedly found nil while unwrapping an Optional value"

It means that the variable jsonArray is nil. But its nil only in swift 2.3. Because the conversion json to NSMutableArray don't work more. I need to change the way I convert...but how?

There's the repoJSON value

(
{
cdState = SP;
cdStore = 100;
cdZipCode = "04029200 ";
mnStore = IBI;
noAddress = "AV IBIRAPUERA,";
noAddressAditional = " 3103 - LOJAS 4-5";
noAddressNeighborhood = " 3103";
noCity = "S\U00e3o Paulo ";
noStore = "IBI-M-C-SHOPPING IBIRAPUERA";
vlLatitude = "-23.610113";
vlLongitude = "-46.666505";
},
{
cdState = SP;
cdStore = 105;
cdZipCode = "05724900 ";
mnStore = JSU;
noAddress = "AV GIOVANNI GRONCHI, 5819 - LUC001";
noAddressAditional = "<null>";
noAddressNeighborhood = "VILA ANDRADE";
noCity = "S\U00e3o Paulo ";
noStore = "JSU-C-C-SHOPPING JARDIM SUL";
vlLatitude = "-23.630835";
vlLongitude = "-46.7359";
},
{
cdState = SP;
cdStore = 107;
cdZipCode = "05145000 ";
mnStore = TTE;
noAddress = "AV. RAIMUNDO PEREIRA DE MAGALH\U00c3ES, 1465 \U0096 LOJA \U00c2NCORA 1000";
noAddressAditional = "<null>";
noAddressNeighborhood = "JD \U00cdRIS";
noCity = "S\U00e3o Paulo ";
noStore = "TTE-PP-C-SHOPPING TIET\U00ca PLAZA";
vlLatitude = "-23.506182";
vlLongitude = "-46.718449";
},
{
cdState = SP;
cdStore = 115;
cdZipCode = "03153001 ";
mnStore = CEN;
noAddress = "AV.DR.FRANCISCO MESQUITA, 1000";
noAddressAditional = "<null>";
noAddressNeighborhood = "VILA PRUDENTE";
noCity = "S\U00e3o Paulo ";
noStore = "CEN-PP-C-CENTRAL PLAZA SHOPPING";
vlLatitude = "-23.593389";
vlLongitude = "-46.585574";
}
)

Answer

I just updated my code with

let jsonArray = repoJSON as! NSArray

Instead of

let jsonArray = repoJSON as? NSMutableArray

and it works great!