博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Swift]iOS开发之初识CoreData
阅读量:4599 次
发布时间:2019-06-09

本文共 3264 字,大约阅读时间需要 10 分钟。

这是一个HitList

import UIKitimport CoreDataclass ViewController: UIViewController,UITableViewDataSource {    var names = [String]()    var people = [NSManagedObject]()        @IBOutlet weak var tableView: UITableView!        override func viewWillAppear(animated: Bool) {        super.viewWillAppear(animated)                let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate                let managedContext = appDelegate.managedObjectContext        let fetchRequest = NSFetchRequest(entityName: "Person")                do {            let results = try managedContext.executeFetchRequest(fetchRequest)            people = results as! [NSManagedObject]        } catch let error as NSError {            print("Could not fetch \(error),\(error.userInfo)")        }    }        override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.                title = "\"The List\""//        tableView.registerClass(UITableView.self, forCellReuseIdentifier: "Cell")    }        @IBAction func addName(sender: AnyObject) {                var alert = UIAlertController(title: "New name", message: "Add a new name", preferredStyle: .Alert)                let saveAction = UIAlertAction(title: "Save", style: .Default) { (action: UIAlertAction!) -> Void in                        let textField = alert.textFields!.first            self.saveName(textField!.text!)            self.tableView.reloadData()        }                let cancelAction = UIAlertAction(title: "Cancel", style: .Default) { (action: UIAlertAction!) -> Void in                    }                alert.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in                    }                alert.addAction(saveAction)        alert.addAction(cancelAction)                presentViewController(alert, animated: true, completion: nil)    }        func saveName(name: String) {        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate        let managedContext = appDelegate.managedObjectContext                let entity = NSEntityDescription.entityForName("Person", inManagedObjectContext: managedContext)                let person = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)                person.setValue(name, forKey: "name")                do {            try managedContext.save()            people.append(person)        } catch let error as NSError {            print("Could not save\(error),\(error.userInfo)")        }    }        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        return people.count    }        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {                let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! as UITableViewCell                let person = people[indexPath.row]        cell.textLabel?.text = person.valueForKey("name") as! String?                return cell    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }}

 

转载于:https://www.cnblogs.com/ybw123321/p/5268947.html

你可能感兴趣的文章
VMware 9.0.1安装Mac OS X Mountain Lion 10.8.2
查看>>
SSL延迟
查看>>
android新手关于左右滑动的问题,布局把<android.support.v4.view.ViewPager/><ImageView/> 放在上面就不行了。...
查看>>
深入理解DIP、IoC、DI以及IoC容器
查看>>
赋值文件
查看>>
Vue 数组 字典 template v-for 的使用
查看>>
蓝牙模块选择经验谈
查看>>
java中==和equals
查看>>
CCActionPageTurn3D
查看>>
python random
查看>>
esp32-智能语音-cli(调试交互命令)
查看>>
netty与MQ使用心得
查看>>
关于dl dt dd 文字过长换行在移动端显示对齐的探讨总结
查看>>
swoolefy PHP的异步、并行、高性能网络通信引擎内置了Http/WebSocket服务器端/客户端...
查看>>
Python学习笔记
查看>>
unshift()与shift()
查看>>
使用 NPOI 、aspose实现execl模板公式计算
查看>>
行为型模式:中介者模式
查看>>
How to Notify Command to evaluate in mvvmlight
查看>>
33. Search in Rotated Sorted Array
查看>>