2016-02-12 41 views
0

我我有兩個數據模型,並希望從加載的json代碼值,以填補他們所有的價值,它具有獨特的輸入值的數組。但在節能領域後,我得到了相同的平均數組(原始數組中等於最後一個值)。,同時保持在列表中的境界 - 它同樣

import Foundation 
import RealmSwift 

class Data: Object { 
    dynamic var city_n: String = "" 
    var templst = List<temp>() 
    dynamic var update_d = NSDate() 

    override static func primaryKey() -> String? { 
     return "city_n" 
    } 
} 
import Foundation 
import RealmSwift 

class temp: Object { 
     dynamic var t: String = "" 
} 

和填充它們

Alamofire.request(.GET, URL , parameters: param) 
       .responseJSON { response in 
        switch response.result { 
         case .Success: 
          if let value = response.result.value { 
           let json = JSON(value) 
           onlineLW.city_n = json["city"]["name"].stringValue 
           onlineLW.update_d = NSDate() 
           var temp_arr: [String] = [] 
           for (_,subJson):(String, JSON) in json["list"] { 
            ot.t = subJson["main"]["temp"].stringValue 
            onlineLW.templst.append(ot) 
            temp_arr.append(subJson["main"]["temp"].stringValue) 
           } 
           for var i = 1; i < temp_arr.count; i++ { 
            onlineLW.templst[i].t = temp_arr[i] 
            print("onlineLW.templst[i].t = \(onlineLW.templst[i].t)") 
            print("temp_arr[i] = \(temp_arr[i])") 
//         onlineLW.templst.append(ot) 
           } 

           print(onlineLW.templst) 
           try! self.realm.write { 
            self.realm.add(onlineLW, update: true) 
           } 
           load.flag = true 
         } 
         case .Failure(let error): 
          print(error) 
        } 

輸出的功能是相同的元素的列表。有什麼問題可以解決?謝謝。

執行結果:

onlineLW.templst[i].t = 279.59 
temp_arr[i] = 279.59 
onlineLW.templst[i].t = 278.13 
temp_arr[i] = 278.13 
onlineLW.templst[i].t = 275.09 
temp_arr[i] = 275.09 
onlineLW.templst[i].t = 272.77 
temp_arr[i] = 272.77 
onlineLW.templst[i].t = 275.26 
temp_arr[i] = 275.26 
onlineLW.templst[i].t = 278.3 
temp_arr[i] = 278.3 
onlineLW.templst[i].t = 281.9 
temp_arr[i] = 281.9 
onlineLW.templst[i].t = 282.85 
temp_arr[i] = 282.85 
onlineLW.templst[i].t = 282.39 
temp_arr[i] = 282.39 
onlineLW.templst[i].t = 277.75 
temp_arr[i] = 277.75 
onlineLW.templst[i].t = 278.42 
temp_arr[i] = 278.42 
onlineLW.templst[i].t = 276.82 
temp_arr[i] = 276.82 
onlineLW.templst[i].t = 278.82 
temp_arr[i] = 278.82 
onlineLW.templst[i].t = 278.55 
temp_arr[i] = 278.55 
onlineLW.templst[i].t = 278.94 
temp_arr[i] = 278.94 
onlineLW.templst[i].t = 280.73 
temp_arr[i] = 280.73 
onlineLW.templst[i].t = 279.79 
temp_arr[i] = 279.79 
onlineLW.templst[i].t = 277.639 
temp_arr[i] = 277.639 
onlineLW.templst[i].t = 276.363 
temp_arr[i] = 276.363 
onlineLW.templst[i].t = 274.554 
temp_arr[i] = 274.554 
onlineLW.templst[i].t = 274.191 
temp_arr[i] = 274.191 
onlineLW.templst[i].t = 273.961 
temp_arr[i] = 273.961 
onlineLW.templst[i].t = 273.637 
temp_arr[i] = 273.637 
onlineLW.templst[i].t = 276.294 
temp_arr[i] = 276.294 
onlineLW.templst[i].t = 277.662 
temp_arr[i] = 277.662 
onlineLW.templst[i].t = 275.412 
temp_arr[i] = 275.412 
onlineLW.templst[i].t = 273.323 
temp_arr[i] = 273.323 
onlineLW.templst[i].t = 272.665 
temp_arr[i] = 272.665 
onlineLW.templst[i].t = 272.789 
temp_arr[i] = 272.789 
onlineLW.templst[i].t = 272.563 
temp_arr[i] = 272.563 
onlineLW.templst[i].t = 274.97 
temp_arr[i] = 274.97 
onlineLW.templst[i].t = 279.417 
temp_arr[i] = 279.417 
onlineLW.templst[i].t = 279.376 
temp_arr[i] = 279.376 
onlineLW.templst[i].t = 276.51 
temp_arr[i] = 276.51 
onlineLW.templst[i].t = 273.889 
temp_arr[i] = 273.889 
onlineLW.templst[i].t = 271.158 
temp_arr[i] = 271.158 
Data { 
    city_n = Paris; 
    update_d = 2016-02-12 09:27:31 +0000; 
    templst = List<temp> (
     [0] temp { 
      t = 271.158; 
     }, 
     [1] temp { 
      t = 271.158; 
     }, 
     [2] temp { 
      t = 271.158; 
     }, 
     [3] temp { 
      t = 271.158; 
     }, 
     [4] temp { 
      t = 271.158; 
     }, 
     [5] temp { 
      t = 271.158; 
     }, 
     [6] temp { 
      t = 271.158; 
     }, 
     [7] temp { 
      t = 271.158; 
     }, 
     [8] temp { 
      t = 271.158; 
     }, 
     [9] temp { 
      t = 271.158; 
     }, 
     [10] temp { 
      t = 271.158; 
     }, 
     [11] temp { 
      t = 271.158; 
     }, 
     [12] temp { 
      t = 271.158; 
     }, 
     [13] temp { 
      t = 271.158; 
     }, 
     [14] temp { 
      t = 271.158; 
     }, 
     [15] temp { 
      t = 271.158; 
     }, 
     [16] temp { 
      t = 271.158; 
     }, 
     [17] temp { 
      t = 271.158; 
     }, 
     [18] temp { 
      t = 271.158; 
     }, 
     [19] temp { 
      t = 271.158; 
     }, 
     [20] temp { 
      t = 271.158; 
     }, 
     [21] temp { 
      t = 271.158; 
     }, 
     [22] temp { 
      t = 271.158; 
     }, 
     [23] temp { 
      t = 271.158; 
     }, 
     [24] temp { 
      t = 271.158; 
     }, 
     [25] temp { 
      t = 271.158; 
     }, 
     [26] temp { 
      t = 271.158; 
     }, 
     [27] temp { 
      t = 271.158; 
     }, 
     [28] temp { 
      t = 271.158; 
     }, 
     [29] temp { 
      t = 271.158; 
     }, 
     [30] temp { 
      t = 271.158; 
     }, 
     [31] temp { 
      t = 271.158; 
     }, 
     [32] temp { 
      t = 271.158; 
     }, 
     [33] temp { 
      t = 271.158; 
     }, 
     [34] temp { 
      t = 271.158; 
     }, 
     [35] temp { 
      t = 271.158; 
     }, 
     [36] temp { 
      t = 271.158; 
     } 
    ); 
} 

回答

1

它看起來像你在你的第一個for循環的每個迭代追加ot到列表中。由於ot是引用類型(它是在該特定情況下一個類)時,在下一迭代改變其t值它改變了t所有先前所附值。爲了解決這個問題我會改變你的第一個for循環是這樣的:

for (_,subJson):(String, JSON) in json["list"] { 
    let newOt = temp() 
    newOt.t = subJson["main"]["temp"].stringValue 
    onlineLW.templst.append(newOt) 
    temp_arr.append(subJson["main"]["temp"].stringValue) 
} 

以上爲循環創建在每次迭代新temp並將其追加到您的列表,所以這應該解決您的問題。如果這樣可以解決這個問題,你甚至可以完全刪除你的第二個for循環,也拆除temp_arr。希望他的幫助。

相關問題