CoreData實踐(四)——查詢資料
我在上一篇部落格中講解了如何往SQLite資料庫中插入資料,現在我們將要進行查詢。
(1)程式碼實現如下:
import UIKit
import CoreData
class UsersTableViewController: UITableViewController {
var dataArr:Array<AnyObject>! = []
var context:NSManagedObjectContext!
override func viewDidLoad() {
super.viewDidLoad()
context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var f = NSFetchRequest(entityName: "Users")
dataArr = context.executeFetchRequest(f, error: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return dataArr.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
var name = dataArr[indexPath.row].valueForKey("name") as! String
var age = dataArr[indexPath.row].valueForKey("age") as! Int
var label = cell.viewWithTag(101) as! UILabel
label.text = "姓名:\(name); 年齡:\(age)"
return cell
}
}
(2)執行程式,結果如下:
。
github主頁:https://github.com/chenyufeng1991 。歡迎大家訪問!
相關文章
- Iceberg 資料治理及查詢加速實踐
- SQL Server實戰四:查詢資料庫的資料SQLServer資料庫
- Swift實踐:使用CoreData儲存多種資料類的通訊錄Swift
- 資料庫學習(四)連線查詢資料庫
- 資料儲存:CoreData
- SSH:hiberate實現資料的查詢(單查詢和全查詢)
- Elasticsearch從0到千萬級資料查詢實踐(非轉載)Elasticsearch
- PB級資料實時查詢,滴滴Elasticsearch多叢集架構實踐Elasticsearch架構
- 千萬級資料深分頁查詢SQL效能最佳化實踐SQL
- MySQL - 資料查詢 - 簡單查詢MySql
- Mysql 慢查詢優化實踐MySql優化
- 實踐006-elasticsearch查詢之1-URI Search查詢Elasticsearch
- Java ——MongDB 插入資料、 模糊查詢、in查詢Java
- 資料庫高階查詢之子查詢資料庫
- [資料結構] 二分查詢 (四種寫法)資料結構
- RESTFul資料查詢REST
- MYSQL查詢資料MySql
- indexedDB 查詢資料Index
- AMS 資料查詢
- WebSocket原理與實踐(四)--生成資料幀Web
- FFmpeg應用實踐之命令查詢
- Excel資料庫轉MySQL,實現查詢Excel資料庫MySql
- iOS資料持久化儲存-CoreDataiOS持久化
- 資料庫基礎查詢--單表查詢資料庫
- elasticsearch查詢之大資料集分頁查詢Elasticsearch大資料
- iOS CoreData (一) 增刪改查iOS
- CoreData - 簡單 增刪改查
- 實踐007-elasticsearch查詢之2-Request Body與DSL查詢Elasticsearch
- Jemter查詢資料庫資料庫
- Flask——資料的查詢Flask
- 求助:資料庫查詢資料庫
- ThinkPHP 資料庫查詢PHP資料庫
- 資料庫排序查詢資料庫排序
- 查詢資料庫大小資料庫
- shell查詢prometheus資料Prometheus
- MySQL資料庫基礎——多表查詢:子查詢MySql資料庫
- 20240719資料庫關聯查詢、條件查詢資料庫
- 如何使用 Milvus 向量資料庫實現實時查詢資料庫
- 02-CoreData 的增刪改查