2016-09-21 8 views
0

プロジェクトでコアデータを実装する際に問題があります。保存することはできますが、フェッチしないように思われます。ここで私はまた、結果の行にエラーが発生しますCoreData implementation swift 3

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 
    let entity = NSEntityDescription.entity(forEntityName: "Utente", in: context) 
    let item = NSManagedObject(entity: entity!, insertInto: context) 
    var utente: Profilo? = nil 
    let vc = CustomTabBarController() as UIViewController 
    let vc1 = LoginController() as UIViewController 
    // Configure Fetch Request 

    do { 
     let request: NSFetchRequest<NSFetchRequestResult> = Profilo.fetchRequest() 
     let result = try context.fetch(request) as Profilo 
     utente = result as Profilo 
     print() 
     if utente?.type != nil { 
      if utente?.type == "students"{ 
       print("students") 
        present(vc, animated: true, completion: nil) 
      } 
      if utente?.type == "parents"{ 
       print("parents") 
       present(vc, animated: true, completion: nil) 

      } 
      if utente?.type == "teachers"{ 
       print("teachers") 
       present(vc, animated: true, completion: nil) 

      } 
     } else { 
      print("variable type empty") 
      present(vc1, animated: true, completion: nil) 

     } 
    } catch { 
     let fetchError = error as NSError 
     print(fetchError) 
    } 

コードだ: はタイプの引数リスト(NSFetchRequest)を呼び出す「フェッチ」することはできません

+0

コアデータに保存してもらえますか?自分で問題がありますか?ありがとう! – user3739902

+0

私はちょうど以下の答えをコピーしましたが、今私はモデルを変更したので、それは動作しません。私はそれを再び働かせる方法を考えている –

+0

私は私のcoredataの仕事をすることができたので、もし私がここに回答として投稿することができます。 – user3739902

答えて

1

私が同意したように、私はコアデータ関数を投稿していますが、これは最良の方法ではないかもしれませんが、私のプロジェクトでは機能します。私の主体は目標と呼ばれます。

// MARK: - Core data funcitons 

func loadCoreData(){ 
    goalsArray.removeAll() 

    //let request = NSFetchRequest<Goals>(entityName:"Goals") 
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 

do{ 
let fetchRequest : NSFetchRequest<Goals> = Goals.fetchRequest() as! NSFetchRequest<Goals> 
let sortDescriptor = NSSortDescriptor(key: "id", ascending: false) 
fetchRequest.sortDescriptors = [sortDescriptor] 

let result = try context.fetch(fetchRequest) 
    for result in result { 
     goalsArray.append(result) 
     print("Fetched result goaltitle is \(result.goalTitle!)") 
    } 
    tableView.reloadData() 

print("I've fetched the results") 
} 
catch { 
    fatalError("Sthh") 
    } 
} 



func countCoreDataObjects()-> Bool{ 
      //let request = NSFetchRequest<Goals>(entityName:"Goals") 
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 

    do{ 
     let fetchRequest : NSFetchRequest<Goals> = Goals.fetchRequest() as! NSFetchRequest<Goals> 
     let result = try context.fetch(fetchRequest) 
     if result.count == 0 { 
      return false 
     } else {return true 

     } 
    } 
    catch { 
     fatalError("Sthh") 
    } 

} 


func saveToCD(object: goalClassForPassing){ 

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 
    let entity = NSEntityDescription.entity(forEntityName: "Goals", in: context) 
    let goal = NSManagedObject(entity: entity!, insertInto: context) as! Goals 



    goal.goalTitle = object.goalName 
    goal.id = String(describing:Date()) 
    goal.imagePath = object.imagePath 

    do { 
     try context.save()} 
    catch { 
     fatalError("couldn't save to core data") 
    } 


    goal.id = String(describing: Date()) 
    goalObjectToSaveToCD?.id = String(describing: NSDate()) 
    print(goalObjectToSaveToCD?.goalTitle) 
    print("Saved \(goal.goalTitle) - \(goal.id) - \(goal.imagePath) to Core Data") 
    loadCoreData() 
} 
2

構文をすることになっている

let request: NSFetchRequest<Profilo> = Profilo.fetchRequest() 
let result = try context.fetch(request) as! [Profilo] // returns always an array. 

デフォルトの初期化子CustomTabBarController()LoginController()は機能しません。

+0

なぜ機能しませんか? –

+0

デフォルトイニシャライザは、ストーリーボードで設計されたものとは異なるコントローラの新しい空のインスタンスを作成します。 – vadian

+0

ええ、私はストーリーボードを使用していません。条件が満たされれば、私が見せたいものです。 sintaxは何ですか? –