我声明了一个全局变量_dataArray:
var _dataArray : Array<AnyObject> = Array();
然后定义一个函数:
func fetchCoreData(userName: String){
var context : NSManagedObjectContext = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "GoodModel")
request.returnsObjectsAsFaults = false
request.predicate = NSPredicate(format: "uid = %@",_userUid )
let _dataArray = context.executeFetchRequest(request, error: nil)!
if(_dataArray.count == 0){
println("We need to parse JSON")
self.parseJSON()
}else{
println("Display from CoreData, we do not need to parse JSON!")
println("_dataArray.count:%d",_dataArray.count)
self.tableView.reloadData()
}
}
从控制台,我可以看到这个_dataArray中有20个对象。 但在以下函数中,_dataArray的计数为0.为什么??????????
@IBAction func endorseButton(sender: AnyObject, forEvent event: UIEvent) {
println(_dataArray.count)
let touches = event.allTouches()
let touch: AnyObject? = touches?.anyObject()
let currentTouchPosition = touch?.locationInView(self.tableView)
let indexPath = self.tableView.indexPathForRowAtPoint(currentTouchPosition!)?
if let ip = indexPath {
var data:NSManagedObject = _dataArray[ip.row] as NSManagedObject
}
}
答案 0 :(得分:0)
问题在于这一行:
let _dataArray = context.executeFetchRequest(request, error: nil)!
您声明本地 _dataArray
。
您应该省略let
:
_dataArray = context.executeFetchRequest(request, error: nil)!