2016-09-21 220 views
0

我在项目中实现coredata时遇到问题。它似乎能够保存但不能获取。这里的代码CoreData实现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会是什么? –