2016-09-30 5 views
-4

JSONファイルの内容を[[String:AnyObject]]タイプの辞書の配列に格納しました。 [String:AnyObject]辞書の各項目に新しいキーと値のペアを追加したいとします。私はエラーが発生し続ける: 'AnyObject ?!'型の不変式に代入できない これを回避する方法はありますか?次のようにSwiftの[String:AnyObject]タイプの辞書にキー値のペアを追加するには?

被験者が宣言されている:

var subjects = [[String:AnyObject]]() 

はEDIT:配列

{ "subjects" : { 
     "humanities" : [ 
      {"sno" : "TH5", "code" : 205, "name" : "Mathematics III", "credits" : 4} 
     ], 
     "applied" : [ 
      {"sno" : "TH3", "code" : 203, "name" : "Power Apparatus", "credits" : 4}, 
      {"sno" : "TH4", "code" : 204, "name" : "Electrical Measurements", "credits" : 4} 
     ], 
     "core" : [ 
      {"sno" : "TH1", "code" : 201, "name" : "Electronics I", "credits" : 4}, 
      {"sno" : "TH2", "code" : 202, "name" : "Circuits and Systems", "credits" : 4} 
     ], 
     "theory" : [ 
      {"sno" : "TH1", "code" : 201, "name" : "Electronics I", "credits" : 4, "category" : "C"}, 
      {"sno" : "TH2", "code" : 202, "name" : "Circuits and Systems", "credits" : 4, "category" : "C"}, 
      {"sno" : "TH3", "code" : 203, "name" : "Power Apparatus", "credits" : 4, "category" : "A"}, 
      {"sno" : "TH4", "code" : 204, "name" : "Electrical Measurements", "credits" : 4, "category" : "A"}, 
      {"sno" : "TH5", "code" : 205, "name" : "Mathematics III", "credits" : 4, "category" : "H"} 
     ], 
     "practical" : [ 
      {"sno" : "PR1", "code" : 206, "name" : "Electronics I", "credits" : 2}, 
      {"sno" : "PR2", "code" : 207, "name" : "Power Apparatus", "credits" : 2}, 
      {"sno" : "PR3", "code" : 208, "name" : "Electrical Measurements", "credits" : 2}, 
      {"sno" : "PR4", "code" : 209, "name" : "Machine Drawing", "credits" : 3}, 
      {"sno" : "VS1", "code" : 210, "name" : "Programming I", "credits" : 1} 
     ] 
    }, 
    "totalCredits" : 30, 
    "semester" : 3 
    } 
:code.Soを掲示しないための謝罪、これは[ANYOBJECT] [文字列]の要素の一つであります

このような8つの辞書は「件名」に格納されています。私は別のキーと値のペア "marks"を追加したい: "humanities"、 "applied"などの各要素に0をつける。 これは私が「文系」

for i in 0..<self.subjects.count 
     { 
      for j in 0..<self.subjects[i]["humanities"]!.count 
      { 
       self.subjects[i]["humanities"]![j]["marks"] = 0 
      } 
     } 

のために、例えば、使用していたコードであると、私はエラーを取得するところです。

EDIT 2:追加された追加の宣言

+0

ためにあなたがしようとしているコード.. – Cool7

+0

を追加してください。辞書を 'var'として取得し、キーと値のペアを追加して、その辞書を配列に戻しておく必要があります。 – vadian

+0

@Prashant私はコードを追加しました –

答えて

0

主な問題は、あなたが配列で何をすべきか、同じ内の辞書に値を割り当てないということです。

あなたは、このような辞書内の値にアクセスしている場合:

var dictionaryValues = dictionary.values 

をして、あなたは間違って辞書を更新しようとしているアレイの「dictionaryValues」内の値を変更しようとしています。

辞書のエントリを変更する方法は、関連するキーに従って値を割り当てることです。

だから私、あなたが知っているあなたがあなたを変更したい値のためのキー(s)は、このような新しい値を割り当てることができます。varなく、レットイットそうに自分の辞書を変更、また

let key = "keyForValueYouWishToChange" 
let newValue: AnyObject = 1 

for (index, dictionary) in arrayOfDictionaries.enumerate() { 
    var mutableDictionary = dictionary 
    mutableDictionary[key] = newValue //this is how you assign to dictionaries 
    arrayOfDictionaries[index] = mutableDictionary 
} 

を変更可能です

+0

ああ、愚かな間違い。私は旅行時に私の携帯電話でスタックオーバーフローを使用するので、ときどき厄介なことがあります。 –

0

あなたが本当にしたいことを理解するのは難しいです。 [String : [String : [[String : Any]]]]のような構造体をメモリに残さず、その中身を変更すると、おそらくこのエラーが発生します。あなたはこの構造から "抽出"して、それに取り組まなければなりません。 JSONに戻したい場合は、もう一度シリアル化する必要があります。あなたのスタートを与えるために、私はあなたのJSONから一人の被験者を取得する方法を紹介します :

// Mockup String 
let jsonString = "{ \"subjects\" : { \"humanities\" : [ {\"sno\" : \"TH5\", \"code\" : 205, \"name\" :  \"Mathematics III\", \"credits\" : 4} ], \"applied\" : [ {\"sno\" : \"TH3\", \"code\" : 203, \"name\" : \"Power  Apparatus\", \"credits\" : 4}, {\"sno\" : \"TH4\", \"code\" : 204, \"name\" : \"Electrical Measurements\",  \"credits\" : 4} ], \"core\" : [ {\"sno\" : \"TH1\", \"code\" : 201, \"name\" : \"Electronics I\", \"credits\" :  4}, {\"sno\" : \"TH2\", \"code\" : 202, \"name\" : \"Circuits and Systems\", \"credits\" : 4} ], \"theory\" : [  {\"sno\" : \"TH1\", \"code\" : 201, \"name\" : \"Electronics I\", \"credits\" : 4, \"category\" : \"C\"},  {\"sno\" : \"TH2\", \"code\" : 202, \"name\" : \"Circuits and Systems\", \"credits\" : 4, \"category\" : \"C\"},  {\"sno\" : \"TH3\", \"code\" : 203, \"name\" : \"Power Apparatus\", \"credits\" : 4, \"category\" : \"A\"},  {\"sno\" : \"TH4\", \"code\" : 204, \"name\" : \"Electrical Measurements\", \"credits\" : 4, \"category\" :  \"A\"}, {\"sno\" : \"TH5\", \"code\" : 205, \"name\" : \"Mathematics III\", \"credits\" : 4, \"category\" :  \"H\"} ], \"practical\" : [ {\"sno\" : \"PR1\", \"code\" : 206, \"name\" : \"Electronics I\", \"credits\" : 2},  {\"sno\" : \"PR2\", \"code\" : 207, \"name\" : \"Power Apparatus\", \"credits\" : 2}, {\"sno\" : \"PR3\",  \"code\" : 208, \"name\" : \"Electrical Measurements\", \"credits\" : 2}, {\"sno\" : \"PR4\", \"code\" : 209,  \"name\" : \"Machine Drawing\", \"credits\" : 3}, {\"sno\" : \"VS1\", \"code\" : 210, \"name\" : \"Programming  I\", \"credits\" : 1} ] }, \"totalCredits\" : 30, \"semester\" : 3 }" 

// use a function to directly retrieve a single subject 
func getSubject(subject: String) -> [String: Any]? { 

    // use some guards to keep the nesting depth minimal 

    // read the json string 
    let data = jsonString.data(using: .utf8) 
    guard data != nil else { 
     return nil 
    } 

    // try to parse the json 
    let parsedJson = try? JSONSerialization.jsonObject(with: data!) as? [String: Any] 
    guard parsedJson != nil else { 
     return nil 
    } 

    // read the subjects 
    if let jsonDictionary = parsedJson! { 
     let subjects = jsonDictionary["subjects"] as? [String: Any] 
     guard subjects != nil else { 
      return nil 
     } 

     // get a single subject and interpret it as array 
     if let singleSubject = subjects![subject] as? [Any] { 
      // check if it contains elements 
      if singleSubject.count > 0 { 
       // return the first (and only) entry as dictionary 
       return singleSubject[0] as? [String: Any] 
      } 
     } 
    } 

    return nil 
} 

if var humanities = getSubject(subject: "humanities") { 

    // add a new entry 
    humanities["marks"] = 0 

    // print out all entries to check 
    for (key, value) in humanities { 
     print("\(key): \(value)") 
    } 
} 

プリント:

name: Mathematics III 
marks: 0 
sno: TH5 
code: 205 
credits: 4 
+0

let定数にデータ全体を保存しました。辞書の配列の個々の要素を、クラスの先頭に宣言された変数に保存しました。 –

関連する問題