2016-10-19 5 views
0
の不変式に割り当てることができません
func buttonAction(sender:radioButton!) { 
    let tempGetTestDesc = dspResult[sender.indexPathSection!]["tests"] as! NSMutableDictionary 
    let temp = tempGetTestDesc["tests"]as! NSMutableArray 
    var tem = temp[sender.indexPathRow!] as! Dictionary<String, AnyObject> 
switch sender.tag{ 
    case 110: 
     tem["sideValue"] = "Left" 
    case 111: 
     tem["sideValue"] = "Right" 
    case 112: 
      tem["sideValue"] = "Both" 
    case 113: 
     tem["contrastValue"] = "Y" 
    default: 
     break 
    } 

上記のコードからエラーは発生しません。ただし、dspResult内の値は更新されません。それはtemの値を更新するだけです。だから私は値を更新するために、次のコードを使用します。タイプ

(((dspResult[sender.indexPathSection!]["tests"] as! NSMutableDictionary)["tests"]as! NSMutableArray)[sender.indexPathRow!] as! Dictionary<String, AnyObject>)["contrastValue"] = "Y" 

ただし、動作しません。タイプ

+0

'(((dspResult [sender.indexPathSection!]!NSMutableDictionaryとして[ "テスト"])!NSMutableArrayのように "テスト"])[sender.indexPathRow!]として!辞書<文字列、ANYOBJECT>) ["contrastValue"] = "Y" 'Yikes thatsたくさんの力を解くthats – JAL

+0

どうすればdspResultの値を更新できますか? – henry

答えて

0

の不変式に代入することはできません理由は、あなたがDictionaryとしてTEM変数をアンラップで、迅速でDictionaryは、手段、任意の変数の代入は、新しい辞書オブジェクトが作成される原因になりますし、変数に割り当てることを、value typeです参照割り当ての代わりに。

func buttonAction(sender:radioButton!) { 
    let tempGetTestDesc = dspResult[sender.indexPathSection!]["tests"] as! NSMutableDictionary 
    let temp = tempGetTestDesc["tests"]as! NSMutableArray 
    var tem = temp[sender.indexPathRow!] as! NSMutableDictionary // <--- Instead of Dictionary 
switch sender.tag{ 
    case 110: 
     tem["sideValue"] = "Left" 
    case 111: 
     tem["sideValue"] = "Right" 
    case 112: 
      tem["sideValue"] = "Both" 
    case 113: 
     tem["contrastValue"] = "Y" 
    default: 
     break 
    } 

次のコードをコピーして、遊び場に貼り付けることができます。

let a = NSMutableDictionary(dictionary: ["a": 1]) 
let aa = NSMutableArray(array: [a]) 
var b = aa[0] as! Dictionary<String, AnyObject> // <-- b is a new copy of a dictionary, so change the value in b, will not impact to the variable a 
b["a"] = 2 
print(a["a"]!) 

let c = aa[0] as! NSMutableDictionary // <--- c is reference to the origin dictionary a, so changing the value in c will change the value in a 
c["a"] = 2 
print(a["a"]!)