0

I try to compile on device but i get this error. Any help?. In the simulator works perfectly.

I get an ambiguous use of subscript error in the following code and was hoping somebody else has encountered this and know the fix.

 case .Success:
                if response.response?.statusCode == 200 {
                      print ("Respuesta 200")
                    if let value = response.result.value {

                        let respuestaJSON = JSON(value)
                        let objsonUSUARIOS = respuestaJSON["d"].object
                        let arrayUsuarios = objsonUSUARIOS["results"]!
                        //print ("Usuarios: ",String(arrayUsuarios))

                        for  i in 0 ..< arrayUsuarios!.count{
                            let boletines = boletinJSON()

                            if  let item = arrayUsuarios![i] as? [String: AnyObject]{
                                )

                                if let person = item["Title"] as? String
                                {
                                    boletines.name = person

                                }

                                if let person = item["Portada"] as? String
                                {
                                    boletines.imagen = person

                                }

                                if let person = item["Created"] as? String
                                {
                                    boletines.fecha = person
                                }

                                if let person = item["AttachmentFiles"] as? [String: AnyObject] {
                                    if let itemAttach = person["__deferred"] as? [String: AnyObject]{
                                        if let itemdeferred = itemAttach["uri"] as? String {
                                            boletines.urldescarga = itemdeferred
                                        }
                                    }
                                }

                                self.boletin.append(boletines)
                                self.view.hideToastActivity()

                            }

                        }



                    }
                      self.tableView.reloadData()

                    //  self.view.hideToastActivity()
                }

2 Answers 2

3

Inform the compiler what the intermediary object objsonUSUARIOS is of type

let objsonUSUARIOS = respuestaJSON["d"].object

After the above statement, the compiler does not know what kind of object he is dealing with. So make sure that you can actually do all the casting as below

let objsonUSUARIOS = respuestaJSON["d"].object as! Dictionary
let arrayUsuarios = objsonUSUARIOS["results"]! as! Array
Sign up to request clarification or add additional context in comments.

6 Comments

ok thanks But in this line " let arrayUsuarios = objsonUSUARIOS["results"]! as! NSDictionary" i get "Could not cast value of type '__NSCFArray' (0x10a907d68) to 'NSDictionary'" when i changed "object" for " .object as! NSDictionary"
Cast it to Array. let arrayUsuarios = objsonUSUARIOS["results"]! as! NSArray
Please do not suggest NSArray / NSDictionary. Use Swift native types
@FireUser, Use Dictionary and Array instead of NSDictionary and NSArray respectively. @Vadian Thanks mate.
Array is a generic container that holds values of a specific type. So you can have an Array<Int> that holds integers, or an Array<String>, or according to your requirement. You can also solve the problem by using NSArray, as NSArray doesn’t use generics.
|
2

The problem is that you have not specified the type of object arrayUsuarios is Array, so try to explicit type cast the arrayUsuarios Array

let arrayUsuarios = objsonUSUARIOS["results"] as! [[String: AnyObject]]

5 Comments

perfect but i get this advert: Conditional cast from '[String : AnyObject]' to '[String : AnyObject]' always succeeds
Instead of if let item = arrayUsuarios![i] as? [String: AnyObject]{ just write like this item = arrayUsuarios![i]
i changed but i get Cannot force unwrap value of non-optional type '[[String : AnyObject]]'
I change to "let arrayUsuarios = objsonUSUARIOS["results"] as! [[String: AnyObject]]" and "if let item = arrayUsuarios![i] " but not working.
No need to write if let just write directly let item = arrayUsuarios![i]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.