2017-03-17 2 views
-1

L'erreur "Utilisation ambiguë de 'indice'" est en cours lorsque vous ne construisez que sur un vrai iPhone. Ne pas avoir de problèmes lors de l'utilisation du simulateur. Voici mon code:Utilisation ambiguë de 'indice' Erreur de compilation Swift 3

let url=URL(string:myUrl) 
    do { 
     let allContactsData = try Data(contentsOf: url!) 
     let allContacts = try JSONSerialization.jsonObject(with: allContactsData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String : AnyObject] 
     if let arrJSON = allContacts["data"] { 
      for index in 0...arrJSON.count-1 { 

       let aObject = arrJSON[index] as! [String : AnyObject] 
       if(ChooseSubject.mineFagKoder.contains(aObject["subject"] as! String)){ 
        ids.append(aObject["id"] as! String) 
        names.append(aObject["name"] as! String) 
        subjects.append(aObject["subject"] as! String) 
        descriptions.append(aObject["description"] as! String) 
        deadlines.append(aObject["deadline"] as! String) 
       } 
      } 
     } 
+0

Je pense que le problème peut venir de [String: AnyObject]. Je ne suis pas un expert de Swift, mais en lisant j'ai eu quelques informations sur des scénarios similaires. Vérifiez cela http://stackoverflow.com/questions/33642059/ambiguous-use-of-subscript-in-swift ou même http://stackoverflow.com/questions/33592699/ambiguous-use-of-subscript-xcode- 7-1 – Balanced

+0

Quelle ligne exactement? 'Let aObject = arrJSON [index] as! [String: AnyObject] 'J'imagine que c'est parce que vous n'avez pas dit au compilateur que' arrJSON' est un tableau, donc vous ne pouvez pas faire 'arrJSON [index]'. – Larme

+0

Cette ligne: let aObject = arrJSON [index] as! [String: AnyObject]. Comment puis-je savoir que arrJSON est un tableau? –

Répondre

0

tout d'abord le type de dictionnaire JSON à Swift 3 est [String:Any]. La raison ambiguous use est que le compilateur ne connaît pas le type de allContacts["data"]. C'est évidemment un tableau mais vous devez le dire au compilateur. Et s'il vous plait, n'utilisez pas du tout l'index de style C laid pour les boucles dans Swift. Si vous avez besoin de index et object dans une boucle de répétition, utilisez enumerated().

if let arrJSON = allContacts["data"] as? [[String : Any]] { 
    for aObject in arrJSON { 
     if ChooseSubject.mineFagKoder.contains(aObject["subject"] as! String) { ...