我正在尝试访问CoreData并将其放入TableView,因此我将数据放入Mutable数组中,但当我尝试将其访问到TableView时,我收到了此错误。
我的代码如下:
import UIKit
import CoreData
class TableView: UIViewController, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
var desc_arr: [String] = []
var amt_arr: [String] = []
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext
let request = NSFetchRequest(entityName: "Expense")
request.returnsObjectsAsFaults = false
do {
let results = try context.executeFetchRequest(request)
if results.count > 0 {
for result in results as! [NSManagedObject] {
if let desc = result.valueForKey("desc") as? String {
print(desc)
desc_arr.append(desc)
}
if let amt = result.valueForKey("amt") as? String{
print(amt)
amt_arr.append(amt)
}
}
}
} catch {
print("Fetch Failed")
}
print(desc_arr)
print(amt_arr)
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return desc_arr.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel?.text = desc_arr[indexPath.row]
return cell
}
}
}