2016-07-23 8 views
1

、私は次のエラーあいまいな使用私は私のiPhone上で以下のコードを実行しようとすると

rrr.swift:356:72: Ambiguous use of 'subscript' 

を取得する奇妙なことは、私はアプリを実行したい場合にのみ、それが起こるですしかし、シミュレータ上では、それは正常に動作します。

エラーの原因となる行は3行あります。私は下のコードを大文字で記述しました。 JSON配列内の配列内のプロパティにアクセスしようとしましたが、間違った方法で行っていますが、修正方法はわかりません。

これらのプロパティはどのようにサブスクリプトエラーなしで取得できますか?

self.restApi.getSchoolDetails(schoolId) {responseObject, error in 
     // use responseObject and error here 

     self.schoolDetailsCollection = NSDictionary(dictionary: responseObject! as! [String : AnyObject]) 
     print(self.schoolDetailsCollection) 

     if let response = responseObject as? NSDictionary { 
      //parse the response object 
      self.schoolDetailsList = (response as? NSDictionary)! 

      //assigning all values to shareData class 
      self.shareData.sco_id = response["result"]!["sco_id"] as!NSInteger 
      self.shareData.address = response["result"]!["address"] as!String 
      self.shareData.name = response["result"]!["name"] as! String 
      print("school name") 
      print(self.shareData.name) 
      self.shareData.intro = response["result"]!["intro"] as! String 
      self.shareData.sell_point = response["result"]!["sell_point"] as! String 
      self.shareData.city = response["result"]!["city"] as! String 
      self.shareData.cou_id = response["result"]!["cou_id"] as! String 

      //get images from the nested array in the json array 
      /THESE THREE LINES CAUSE THE ERROR SUBSRCIPT 
      self.shareData.image1 = response["result"]!["images"][0]! as! String 
      self.shareData.image2 = response["result"]!["images"]![1] as! String 
      self.shareData.image3 = response["result"]!["images"]![2] as! String 

      print(self.shareData.image1) 
      print(self.shareData.image2) 
      print(self.shareData.image3) 



      //open next controller after all info has been set correctly 
      //info is being passed by Singleton class Sharedata 
      if let COBezierDemoViewController = self.storyboard!.instantiateViewControllerWithIdentifier("COBezierDemoViewController") as? COBezierDemoViewController { 
       self.presentViewController(COBezierDemoViewController, animated: true, completion: nil) 
      } 

     } 

    } 

} 

JSONファイル:あなたは、この行でそのエラーをヒントとして

{ 
result =  { 
    address = "223 Vincent St, West Perth WA 6005, Australia"; 
    city = Perth; 
    "cou_id" = AU; 
    "cur_id" = ""; 
    environment = R; 
    financed = "<null>"; 
    images =   (
     "Phoenix_Academy_1.jpeg", 
     "Phoenix_Academy_3.jpeg", 
     "Phoenix_Academy_2.jpeg" 
    ); 
    intro = "Our language school in Perth has a modern campus and a spacious garden. The language school is located 10 minutes north from the city center. You can reach the city center and the famous bea 

答えて

1

self.shareData.image1 = response["result"]!["images"][0]! as! String 

エラー:

エラーは、あなたが実装している[0]添字がありますAnyObjectで

  1. 意気消沈AnyObject as? [String] // Stringはイメージ名ですこれはnilまたはオプションArray<String?>を返します。次のいずれか

    response["result"]!["images"] // this is returning Optional<AnyObject> which do not have subscript option. 
    

    正しい方法は本にする必要があります。

Without downcasting, you will get error: Ambiguous use of subscript.

let images = response["result"]? ["images"] as? [String] 
    images?.flatMap({ $0 }).forEach { 
    print($0) // Phoenix_Academy_1.jpeg, Phoenix_Academy_3.jpeg, Phoenix_Academy_2.jpeg 
    } 
+0

Thxをたくさん。うまく動作し、私をより良く理解するのに役立ちます –

関連する問題