context = ( UIApplication.sharedApplication().delegate as! AppDelegate ).managedObjectContext
var row:AnyObject = NSEntityDescription.insertNewObjectForEntityForName("USERS", inManagedObjectContext: context!)
if(name.text.isEmpty){
UIAlertView(title: "提示", message: "姓名不能为空", delegate: nil, cancelButtonTitle: "确认").show()
name.becomeFirstResponder() //得到焦点
return
}
row.setValue(name.text, forKey: "name")
if let _age = age.text.toInt() { //可以根据此种方法判断是否是int类型
var _age:Int = age.text.toInt()!
if(_age < 0){
UIAlertView(title: "提示", message: "年龄必须为正数", delegate: nil, cancelButtonTitle: "确认").show()
age.becomeFirstResponder()
return
}
row.setValue(_age, forKey: "age")
}else{
UIAlertView(title: "提示", message: "年龄必须为正数", delegate: nil, cancelButtonTitle: "确认").show()
age.becomeFirstResponder()
return
}
context?.save(nil)
context = ( UIApplication.sharedApplication().delegate as! AppDelegate ).managedObjectContext!
dataarry = context.executeFetchRequest( NSFetchRequest(entityName: "USERS"), error: nil)!
11:更新数据:
@IBAction func update(sender: AnyObject) {
data.setValue(_uname.text, forKey: "name")
data.setValue(_uage.text.toInt(), forKey: "age")
data.managedObjectContext?.save(nil)
dismissViewControllerAnimated(true, completion: nil)
12:删除
/ Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
//tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
context.deleteObject(dataarry[indexPath.row] as! NSManagedObject)
context.save(nil);
refreshData()
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
import UIKit
import CoreData
class ViewController: UIViewController {
@IBOutlet weak var age: UITextField!
@IBOutlet weak var name: UITextField!
var context: NSManagedObjectContext!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
context = ( UIApplication.sharedApplication().delegate as! AppDelegate ).managedObjectContext
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func saveAction(sender: AnyObject) {
var row:AnyObject = NSEntityDescription.insertNewObjectForEntityForName("USERS", inManagedObjectContext: context!)
if(name.text.isEmpty){
UIAlertView(title: "提示", message: "姓名不能为空", delegate: nil, cancelButtonTitle: "确认").show()
name.becomeFirstResponder() //得到焦点
return
}
row.setValue(name.text, forKey: "name")
if let _age = age.text.toInt() { //可以根据此种方法判断是否是int类型
var _age:Int = age.text.toInt()!
if(_age < 0){
UIAlertView(title: "提示", message: "年龄必须为正数", delegate: nil, cancelButtonTitle: "确认").show()
age.becomeFirstResponder()
return
}
row.setValue(_age, forKey: "age")
}else{
UIAlertView(title: "提示", message: "年龄必须为正数", delegate: nil, cancelButtonTitle: "确认").show()
age.becomeFirstResponder()
return
}
context?.save(nil)
age.resignFirstResponder() //第一响应事件取消,消除键盘操作
}
}