无法分配给“AnyObject?!”类型的不可变表达式
Cannot assign to immutable expression of type ' AnyObject?!'
我做了一些搜索,但仍然无法弄清楚如何解决该错误。
基本上我正在阅读 Json 文件中的书单,然后更新它。阅读部分没问题,但尝试更新时发生错误("Cannot assign to immutable expression of type ' AnyObject?!'")。
var url = NSBundle.mainBundle().URLForResource("Book", withExtension: "json")
var data = NSData(contentsOfURL: url!)
var booklist = try! NSJSONSerialization.JSONObjectWithData(data!, options: []) as! NSMutableArray
for boo in booklist {
if (boo["name"] as! String) == "BookB" {
print (boo["isRead"]) //see Console Output
boo["isRead"] = "true" //this gets error "Cannot assign to immutable expression of type ' AnyObject?!'"
}
Json文件Book.json如下:
[
{"name":"BookA","auth":"AAA","isRead":"false",},
{"name":"BookB","auth":"BBB","isRead":"false",},
{"name":"BookC","auth":"CCC","isRead":"false",},
]
书单有预期值,请参阅控制台输出:
(
{
name = BookA;
auth = AAA;
isRead = false;
},
{
name = BookB;
auth = BBB;
isRead = false;
},
{
name = BookC;
auth = CCC;
isRead = false;
}
)
并且对于 print (boo["isRead"])
,控制台结果是 Optional(false)
,这是正确的。
Booklist已经是一个NSMutableArray,我也尝试改成
var booklist = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSMutableArray
但这并没有帮助。
也参考了,改成下面同样报错:
var mutableObjects = booklist
for var boo in mutableObjects {
if (boo["name"] as! String) == "BookB" {
print (boo["isRead"]) //see Console Output
boo["isRead"] = "true" //this gets error "Cannot assign to immutable expression of type ' AnyObject?!'"
}
在这种情况下,谁能建议如何更新 BookB 的书单中的 isRead。或者更好的是如何更新 Book.json 文件。
在你的情况下,你遇到了这个错误两次:
- 在您正确编辑的
for
循环中
- 因为编译器不知道
boo
的类型(它只是 NSMutableArray
的一个元素)
要解决这个问题,您可以这样写:
for var boo in mutableObjects {
if var theBoo = boo as? NSMutableDictionary {
if (theBoo["name"] as! String) == "BookB" {
print (theBoo["isRead"]) //see Console Output
theBoo["isRead"] = "true" //this gets error "Cannot assign to immutable expression of type ' AnyObject?!'"
}
}
}
或者,您通过以下方式向编译器提示 boo
的类型:
guard let theBoos = mutableObjects as? [Dictionary<String, AnyObject>] else {
return
}
for var theBoo in theBoos {
if (theBoo["name"] as! String) == "BookB" {
print (theBoo["isRead"]) //see Console Output
theBoo["isRead"] = "true" //this gets error "Cannot assign to immutable expression of type ' AnyObject?!'"
}
}
我做了一些搜索,但仍然无法弄清楚如何解决该错误。 基本上我正在阅读 Json 文件中的书单,然后更新它。阅读部分没问题,但尝试更新时发生错误("Cannot assign to immutable expression of type ' AnyObject?!'")。
var url = NSBundle.mainBundle().URLForResource("Book", withExtension: "json")
var data = NSData(contentsOfURL: url!)
var booklist = try! NSJSONSerialization.JSONObjectWithData(data!, options: []) as! NSMutableArray
for boo in booklist {
if (boo["name"] as! String) == "BookB" {
print (boo["isRead"]) //see Console Output
boo["isRead"] = "true" //this gets error "Cannot assign to immutable expression of type ' AnyObject?!'"
}
Json文件Book.json如下:
[
{"name":"BookA","auth":"AAA","isRead":"false",},
{"name":"BookB","auth":"BBB","isRead":"false",},
{"name":"BookC","auth":"CCC","isRead":"false",},
]
书单有预期值,请参阅控制台输出:
(
{
name = BookA;
auth = AAA;
isRead = false;
},
{
name = BookB;
auth = BBB;
isRead = false;
},
{
name = BookC;
auth = CCC;
isRead = false;
}
)
并且对于 print (boo["isRead"])
,控制台结果是 Optional(false)
,这是正确的。
Booklist已经是一个NSMutableArray,我也尝试改成
var booklist = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSMutableArray
但这并没有帮助。
也参考了
var mutableObjects = booklist
for var boo in mutableObjects {
if (boo["name"] as! String) == "BookB" {
print (boo["isRead"]) //see Console Output
boo["isRead"] = "true" //this gets error "Cannot assign to immutable expression of type ' AnyObject?!'"
}
在这种情况下,谁能建议如何更新 BookB 的书单中的 isRead。或者更好的是如何更新 Book.json 文件。
在你的情况下,你遇到了这个错误两次:
- 在您正确编辑的
for
循环中 - 因为编译器不知道
boo
的类型(它只是NSMutableArray
的一个元素)
要解决这个问题,您可以这样写:
for var boo in mutableObjects {
if var theBoo = boo as? NSMutableDictionary {
if (theBoo["name"] as! String) == "BookB" {
print (theBoo["isRead"]) //see Console Output
theBoo["isRead"] = "true" //this gets error "Cannot assign to immutable expression of type ' AnyObject?!'"
}
}
}
或者,您通过以下方式向编译器提示 boo
的类型:
guard let theBoos = mutableObjects as? [Dictionary<String, AnyObject>] else {
return
}
for var theBoo in theBoos {
if (theBoo["name"] as! String) == "BookB" {
print (theBoo["isRead"]) //see Console Output
theBoo["isRead"] = "true" //this gets error "Cannot assign to immutable expression of type ' AnyObject?!'"
}
}