Skip to content

Instantly share code, notes, and snippets.

@masakid
Created August 9, 2015 07:48
Show Gist options
  • Save masakid/b1388f362f83ef7f78aa to your computer and use it in GitHub Desktop.
Save masakid/b1388f362f83ef7f78aa to your computer and use it in GitHub Desktop.
詰まっている箇所(updateWithNameのNSPredicateで実行時例外)
//
// CheckItemManager.swift
// KadaiCheck
//
import UIKit
import CoreData
class CheckItemManager: NSObject {
//Managerクラス
//役割
//Entityと通してCoreDataからデータを取得するSQL発行の役割
//返却する際はEntity, [Entity]で返す
//AppDelegateの初期設定
private var appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
private var managedContext: NSManagedObjectContext?
private var entity: NSEntityDescription?
//エンティティー名の設定
private let entityName: String = "CheckItem"
//initで起動オブジェクトの設定
override init(){
if let
localManagedContext = appDelegate!.managedObjectContext,
localEntity = NSEntityDescription.entityForName(entityName, inManagedObjectContext: localManagedContext)
{
self.managedContext = localManagedContext
self.entity = localEntity
}
}
//データ追加処理
//引数で追加データを渡して、内部で登録処理実施
func insert(id: Int16, name: String, isCheck: Bool){
if let itemObject = NSManagedObject(entity: self.entity!, insertIntoManagedObjectContext: managedContext) as? CheckItem {
itemObject.id = id
itemObject.name = name
itemObject.isCheck = isCheck
var error: NSError?
if !managedContext!.save(&error) {
println("保存に失敗...")
}else{
println("保存に成功!")
}
}
}
func insertAll(){
}
//データ更新処理
//1件に対して更新処理実施
//idに一致するレコードのみ更新する
func updateWithName(id: Int16, name: String, isCheck: Bool){
var fetchRequest = NSFetchRequest(entityName: self.entityName)
var error: NSError?
fetchRequest.predicate = NSPredicate(format: "id = %@", id)
let fetchResults = managedContext!.executeFetchRequest(fetchRequest, error: &error)
if let results = fetchResults as? [CheckItem] {
for obj in results {
obj.name = name
obj.isCheck = isCheck
}
if !managedContext!.save(&error) {
println("更新に失敗...")
}else{
println("更新に成功!")
}
}
}
//データ検索処理
//全件データを読み込んで表示
//こちらも削除のNSPredicateを参考にして、検索処理は実装する
func selectAllData() -> [CheckItem]?{
let fetchRequest = NSFetchRequest(entityName: self.entityName)
var error: NSError?
let fetchResults = managedContext!.executeFetchRequest(fetchRequest, error: &error)
if let results = fetchResults as? [CheckItem] {
for obj in results {
println("id = \(obj.id), name = \(obj.name), isCheck = \(obj.isCheck)")
}
return results
} else {
return nil
}
}
//対象データを検索して返却
func selectDataWithId(id: Int16) -> [CheckItem]?{
let fetchRequest = NSFetchRequest(entityName: self.entityName)
var error: NSError?
fetchRequest.predicate = NSPredicate(format: "id = %@", id)
let fetchResults = managedContext!.executeFetchRequest(fetchRequest, error: &error)
if let results = fetchResults as? [CheckItem] {
for obj in results {
println("id = \(obj.id), name = \(obj.name), isCheck = \(obj.isCheck)")
}
return results
} else {
return nil
}
}
//データ削除処理
//検索して削除している。参考にするならここ!
func deleteWhereId(id : Int16){
let fetchRequest = NSFetchRequest(entityName: self.entityName)
fetchRequest.predicate = NSPredicate(format: "id == %@", id)
var error: NSError?
let fetchResults = managedContext!.executeFetchRequest(fetchRequest, error: &error)
if let results = fetchResults as? [NSManagedObject] {
for obj in results {
managedContext!.deleteObject(obj)
}
}
if !managedContext!.save(&error) {
println("削除に失敗...")
}else{
println("削除に成功!")
}
}
//全データ削除処理
func deleteAll(){
let fetchRequest = NSFetchRequest(entityName: self.entityName)
var error: NSError?
let fetchResults = managedContext!.executeFetchRequest(fetchRequest, error: &error)
if let results = fetchResults as? [NSManagedObject] {
for obj in results {
managedContext!.deleteObject(obj)
}
}
if !managedContext!.save(&error) {
println("削除に失敗...")
}else{
println("削除に成功!")
}
}
func updateAllSeqId(){
let fetchRequest = NSFetchRequest(entityName: self.entityName)
var error: NSError?
let fetchResults = managedContext!.executeFetchRequest(fetchRequest, error: &error)
if let results = fetchResults as? [CheckItem] {
for (index, obj) in enumerate(results) {
obj.id = Int16(index)
}
if !managedContext!.save(&error) {
println("更新に失敗...")
}else{
println("更新に成功!")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment