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