2016-06-23 34 views
5

La funzione di seguito viene utilizzata per ottenere i dati per TableviewSwift 3 Core Data recuperare problema

func fetchProfiles() -> Array<User> { 
    var users: Array<User> = [] 
    let fetchRequest: NSFetchRequest<Profile> = Profile.fetchRequest() 
    let fetchedData = try! context.fetch(fetchRequest) 
    if (!fetchedData.isEmpty) { 
     print(fetchedData) 
     for i in 0...fetchedData.count { 
      var user: User = User() 
      user.userName = fetchedData[i].profileName 
      user.userSurname = fetchedData[i].profileSurname 
      user.userPhoto = fetchedData[i].profilePhoto 
      users.append(user) 
     } 
     return users 
    } 
    else { 
     return users 
    } 
} 

"Utente" è un semplice struct. "Profilo" è un'entità in Dati principali. Creo una matrice di strutture per usarle per le celle nella tabella. Il codice non ha errori (per xCode). Quando non ci sono dati recuperati, salta aggiungendo array, ma quando c'è qualche info, applicazione si blocca con l'errore:

fatal error: NSArray element failed to match the Swift Array Element type

+2

Prova a sostituire questa riga nella richiesta di recupero, *** let fetchedData = try! context.executeFetchRequest (fetchRequest) as! [AnyObject] *** – Suresh

+0

@Suresh xCode dice: "Impossibile convertire il valore di tipo NSFetchRequest in attesa NSFetchRequest " –

+0

Sostituisci let let fetchRequest: NSFetchRequest = Profile.fetchRequest(); line with *** let fetchRequest: NSFetchRequest = NSFetchRequest (entityName: "Profile") *** qui, "Profile" dovrebbe essere il nome dell'entità, assicurati di passarlo come stringa – Suresh

risposta

1

Per ragioni sconosciute, ci sono stati problemi con l'auto generato intestazioni di entità Core Data. Ho appena cancellato tutti i file dalla cartella:

/Users/user/Library/Developer/Xcode/DerivedData/MyApp/Build/Intermediates/MyApp.build/Debug-iphonesimulator/MyApp.build/DerivedSources/CoreDataGenerated

poi pulito il mio progetto con

command+shift+k

controllato il mio modello di dati di base per alcuni possibili problemi e ricostruito l'intero progetto. Magia.