iOSでデータベースを利用するにはSwiftData(Swift製SQLiteラッパー)を使うといい。
下記の記事を参考にさせてもらったのだが、swift1.2に対応していないところを修正する。
http://qiita.com/osamu1203/items/b8868733111cabf16418
MasterViewController.swift冒頭の記述
import UIKit class MasterViewController: UITableViewController { var detailViewController: DetailViewController? = nil var objects = NSMutableArray() let sampleModel = SampleModel() //SampleModelのオブジェクト作成
修正
import UIKit class MasterViewController: UITableViewController { var objects = [AnyObject]() let sampleModel = SampleModel() //SampleModelのオブジェクト作成
同じく MasterViewController.swift内の
func insertNewObjectの記述
func insertNewObject(sender: AnyObject) { let now = NSDate.date() let id = sampleModel.add(now) let dic = ["ID":id, "data":now] objects.insertObject(dic, atIndex: 0) let indexPath = NSIndexPath(forRow: 0, inSection: 0) self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) }
修正
func insertNewObject(sender: AnyObject) { let now = NSDate() let id = sampleModel.add(now) let dic = ["ID":id, "data":now] objects.insert(dic, atIndex: 0) let indexPath = NSIndexPath(forRow: 0, inSection: 0) self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) }
SampleModel.swift内の
func getAll() -> NSMutableArray { var result = NSMutableArray()
修正
func getAll() -> [AnyObject] { var result = [AnyObject]()
この
func getAll()内の
result.addObject(dic)
修正
result.insert( dic, atIndex : 0 )
MasterViewController.swift内
4つあるoverride func tableViewの4つ目
if editingStyle == .Delete { sampleModel.delete(objects[indexPath.row]["ID"] as Int) objects.removeObjectAtIndex(indexPath.row)
修正
if editingStyle == .Delete { sampleModel.delete(objects[indexPath.row]["ID"] as! Int) objects.removeAtIndex(indexPath.row)
他のiOS関係の記事はこちら
A valid provisioning profile for this executable was not found
コメント