我成功地将图像数组保存为codata中的nsdata。 我的问题是 - 如何检查特定索引(2)是否存在图像? 这是我的检索数据的代码
var tableImageArray = NSData
func ShowCoreData(){
// core data - show
let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context:NSManagedObjectContext = appDel.managedObjectContext
var request = NSFetchRequest(entityName: "SavedIdea")
request.returnsObjectsAsFaults = false;
do {
var result:NSArray = try context.executeFetchRequest(request)
if (result.count > 0) {
self.tableTitleArray = result.valueForKey("heading") as! [String]
print(tableTitleArray)
self.tableDetailArray = result.valueForKey("detail") as! [String]
self.tableTypeArray = result.valueForKey("type") as! [String]
self.tableHoursArray = result.valueForKey("time") as! [String]
self.tableImageArray = result.valueForKey("image") as! [NSData]
// print(" image array nsdata -- \(tableImageArray)")
}else{
// print("0 result appear....error")
}
tableView.reloadData()
}catch {
let fetchError = ("returned error is \(error as! NSError)")
print(fetchError)
}
}
我在tableImageArray中将整个数组作为NSdata然后假设索引2处没有图像或值,但tableImageArray的索引1,3具有值。这在这一行给我BAD_Exc错误 - if tableImageArray [indexPath.row] .length>每当找不到图像时为0
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! as! OTPTableViewCell
cell.LabelTitle.text = tableTitleArray[indexPath.row]
cell.LabelDetail.text = tableDetailArray[indexPath.row]
cell.LabelType.text = tableTypeArray[indexPath.row]
cell.LabelHours.text = String(tableHoursArray[indexPath.row])
// print(tableImageArray[indexPath.row])
if tableImageArray[indexPath.row].length > 0 {
cell.ImgView.image = UIImage(data: tableImageArray[indexPath.row])//(named: tableImageArray[indexPath.row])
}else{
cell.imageWidthConstraints.constant = 0
}
return cell
}