【发布时间】:2014-06-09 20:27:19
【问题描述】:
我正在尝试使用任何可能有效的方法(+=、追加、插入)向我的数组(声明为 var)添加一个项目,但是我不断收到错误消息' “AnyObject[]”类型的不可变值只有名为“append”的变异成员。
这里是错误发生的地方:
func testSave(item : NSString, date : NSString){
itemsArray.append(item)
更新:这是完整代码:
import UIKit
class ToDoListTableViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate {
var itemsArray = NSUserDefaults .standardUserDefaults().arrayForKey("items")
var dateArray = NSUserDefaults .standardUserDefaults().arrayForKey("dates")
override func viewDidLoad() {
super.viewDidLoad()
NSUserDefaults .standardUserDefaults().setObject("test", forKey: "items")
NSUserDefaults .standardUserDefaults().setObject("test", forKey: "dates")
self.itemsArray = NSUserDefaults .standardUserDefaults().arrayForKey("items")
self.dateArray = NSUserDefaults .standardUserDefaults().arrayForKey("dates")
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// #pragma mark - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
if itemsArray{
return itemsArray.count}
else{
return 0}
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
//variable type is inferred
/*var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell
if !cell {
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
}
//we know that cell is not empty now so we use ! to force unwrapping
cell!.textLabel.text = self.itemsArray[indexPath.row] as String
cell!.detailTextLabel.text = self.dateArray[indexPath.row] as String
*/
let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:"Cell")
if itemsArray{
println("Working")
cell.textLabel.text = itemsArray[indexPath.row] as String
cell.detailTextLabel.text = dateArray[indexPath.row] as String
}
return cell
}
@IBAction func addItem(sender : UIBarButtonItem) {
var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: {(action: UIAlertAction!) in
var stringText = alert.textFields[0].text
var dateText = alert.textFields[0].text
self .testSave(stringText, date: dateText)
}))
alert.addTextFieldWithConfigurationHandler(nil)
self.presentViewController(alert, animated: true, completion: nil)
}
func testSave(item : NSString, date : NSString){
itemsArray.append(item)
/* NSUserDefaults .standardUserDefaults().setObject(item, forKey: "items")
/* NSUserDefaults .standardUserDefaults().setObject(stringText, forKey: "items")
NSUserDefaults .standardUserDefaults().setObject(dateText, forKey: "dates")
NSUserDefaults.standardUserDefaults().synchronize()
*/
self.dateArray = NSUserDefaults .standardUserDefaults().arrayForKey("dates")
self.tableView .reloadData() */
}
func alertviewClick(){
}
}
【问题讨论】:
-
从错误看来
itemsArray是不可变的。是用let还是var定义的? -
看来您的问题的原因在于显示的代码之外。您能否显示更多代码,特别是 itemsArray 如何进入范围 - 它是 iVar 吗?从另一个函数收到?本地申报?
-
我已经用完整的代码更新了我的答案: