[英]Swift - Issue with data types
I am loading some data into NSUserDefaults on application startup and when the user views the according View loading the data out into a TableView. 我在应用程序启动时将某些数据加载到NSUserDefaults中,当用户查看相应的View时,会将数据加载到TableView中。 I'm having an issue with the data types though and always seem to get an error whichever way I try.
我在使用数据类型时遇到了问题,无论尝试哪种方式,似乎总是会出现错误。
Where am I going wrong. 我要去哪里错了。 I am skipping some code and only giving the important parts.
我跳过了一些代码,只给出了重要的部分。 I keep getting errors like "Can't assign Bool to AnyObject"
我不断收到诸如“无法将Bool分配给AnyObject”之类的错误
// startup function which loads the data
var categorys : [[String:Bool]] = [["All": true]]
for (index: String, category: JSON) in output {
categorys.append([category["name"].string!: true])
}
NSUserDefaults.standardUserDefaults().setObject(categorys, forKey: "categorys")
// view controller
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var categorys: [[String:Bool]] = []
func buildTableView() {
if let categorys = NSUserDefaults.standardUserDefaults().arrayForKey("categorys") {
for category in categorys {
self.categorys.append([category["name"] as! String: category["selected"] as! Bool])
}
// Was trying to just assign straight away but fails
// self.categorys = categorys
}
}
// then later on I want to be able to do the following
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
cell.textLabel?.text = self.categorys[indexPath.row]["name"] as? String
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if (self.categorys[indexPath.row]["selected"] as? Bool == true) {
self.categorys[indexPath.row]["selected"] = "false"
}
}
}
I should probably also mention i was trying a different datatype before which is more logical for my usage but also had similar issues. 我可能还应该提到我正在尝试使用其他数据类型,在此之前对于我的用法来说更合逻辑,但也存在类似的问题。
["name": [category["name"].string!, "selected": true]]
I should also mention this is data for my tableview and I want to update the boolean value for if the cell is selected to not. 我还应该提到这是我的tableview的数据,如果单元格未选中,我想更新布尔值。
In order to use Bool
in NSUserDefaults
, you must use setBool: forKey:
: 为了在
NSUserDefaults
使用Bool
,必须使用setBool: forKey:
::
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "key")
otherwise it's problematic and you have to do workarounds, like storing the Bool
in NSNumber
or String
. 否则,这是有问题的,您必须执行解决方法,例如将
Bool
存储在NSNumber
或String
。
You said in the comment that you've got an error about "finding nil", but it's not really the same as an error about "Can't assign Bool to AnyObject"... 您在评论中说,您遇到了有关“找到nil”的错误,但实际上与关于“无法将Bool分配给AnyObject”的错误并不完全相同。
For your nil
error, you have to replace those forced typecasts: 对于您的
nil
错误,您必须替换那些强制类型转换:
category["name"] as! String
with optional binding: 具有可选的绑定:
if let name = category["name"] as? String {
}
Now it will fail but not crash if a property is nil, and will properly append to the array of dictionaries: 现在,如果属性为nil,它将失败但不会崩溃,并将正确地附加到字典数组中:
let category = ["name":"me", "selected":true]
var categorys: [[String:Bool]] = []
if let name = category["name"] as? String, let selected = category["selected"] as? Bool {
categorys.append([name:selected])
} else {
// oops, a value was nil, handle the error
}
Same for your other operations, don't use forced casts: 与其他操作相同,请勿使用强制转换:
if let myBool = self.categorys[indexPath.row]["selected"] where myBool == true {
self.categorys[indexPath.row]["selected"] = false
}
And since self.categorys[indexPath.row]["selected"]
is a Bool type, don't assign a string like "false" but the actual false
value. 而且由于
self.categorys[indexPath.row]["selected"]
是Bool类型,因此请不要分配字符串“ false”,而是实际的false
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.