swift - 如何获取 customTableViewCell 中 UITextField 的字符串并将其保存到 coreData?

标签 swift xcode core-data tableview

let  save  = UIApplication.shared.delegate as! AppDelegate
var price =  " "
// config cell here
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "priceCell", for: indexPath) as! PriceTableViewCell
    cell.priceTextField.text = numberFromKeyPad
    price = cell.priceTextField.text!
    return cell
}

这里将价格保存到 coreData

@IBAction func saveButtonClicked(_ sender: UIButton) {
    save.check.price = Int64 (price)!//here i have this error :Fatal error: Unexpectedly found nil while unwrapping an Optional val
    PersistentService.saveContext()
    print(NSHomeDirectory())
}

核心数据中的这个错误是什么?

最佳答案

Ok Declare the Price variable like 

var price:String?

    before assigning the value to price check whether pricetextfield contains the value or not

if let priceValue = cell.priceTextField.text {
   price =  priceValue
}
and again before calling save.check.price = Int64 (price)! check for value

if let price = price {
   save.check.price = price
}

关于swift - 如何获取 customTableViewCell 中 UITextField 的字符串并将其保存到 coreData?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50540567/

相关文章:

ios - 以编程方式强制终止 iPhone 设备上的另一个应用程序

iOS - NSDateFormatter dateFromString 仅在一种情况下返回 nil

ios - 如何在 iOS RSS 应用程序中显示解析后的 XML 中的图像?

xcode - 静态库未在 Link Binary 中与 Cocoapods 链接

objective-c - 如何在 CAShapeLayer 上添加外发光

ios - CoreData 将对象添加到一对多关系错误

ios - 表格 View 行中的 UISwitch

swift - 核心数据编码模式

iphone - 核心数据不断出现 sigabrt 错误

ios - UIBarButtonItem setBackButtonTitlePositionAdjustment 不起作用