Int does not conform to protocol 'StringLiteralConvertible'

I am trying to parse json in a weather app but fell into a trap that I cannot skip.

I am getting the error: "The type" int "does not conform to the" StringLiteralConvertible "protocol in the following code. Ive tried casting jsonResult [" main "], but instead it gave the error" The postfix operand must be an optional type, type is AnyObject ". I need to omit the Array somehow and how, if so, should I do it? I searched so much for this, but couldn't find any help in other posts. The code is as follows.

func updateWeatherInfo(latitude: CLLocationDegrees, longitude: CLLocationDegrees) {
    Alamofire.request(.GET, AlongRequest)
        .responseJSON { (_, _, JSON, error) in
            self.updateUISuccess(JSON as NSArray!)

func updateUISuccess(jsonResult: NSArray) {
    self.loading.text = nil
    self.loadingIndicator.hidden = true

    if let tempResult = ((jsonResult["main"] as NSArray)["temp"] as? Double)



source to share

1 answer

It would be easier to give a definitive answer if you provide the JSON you are trying to parse, but the error message you are getting is clear.

This error occurs because you are trying to access what you declared as a NSArray

string-indexed instance twice on this line:

if let tempResult = ((jsonResult["main"] as NSArray)["temp"] as? Double)



is declared as a parameter NSArray

and then you submit jsonResult["main"]

to NSArray

before trying to index it with ["temp"]

. The problem here is that NSArray

(and Swift's built-in arrays) only use subtype integer based operations. The error says that where the Swift compiler expects Int

you have provided a string literal.

To fix this, you need to go in one of two directions. If the structure you are trying to access has these string keys, then you should use NSDictionary

instead NSArray

in both cases. If not, and it is an integer indexed array, you must use integers.



All Articles