1

I've spent a few hours trying to get a fetch to work in my film sheet. I need to open film's View of my colectionview Items. I could follow different guide and post but it always give me an empty array. I'm newbie, sorry for my question but I need your help.

Here's my code:

var taskArrayScheda : NewFilm?

override func viewDidLoad() {
super.viewDidLoad()

self.fetchData()

if(self.taskArrayScheda != nil) {

let schedaOk = taskArrayScheda
mostraDatiNellaScheda(schedaOk!)

} else { print("errore array vuoto") }


}

func mostraDatiNellaScheda(_ sched:NewFilm) {

// get title
titoloScheda.text = sched.titolo
}

func fetchData() {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext


let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "NewFilm")

do {
taskArrayScheda = try context.fetch(NewAnime.fetchRequest())
💥ERROR ::::: Cannot assign value of type '[Any]' to type 'NewFilm?'

} catch {
print(error)
}
}    
1
  • Unrelated but why do you create two different fetch requests? By the way the used one doesn't match the type of the property (beside the array issue). Commented Feb 27, 2019 at 16:40

1 Answer 1

1

The fetch() returns an array. But currently you assign the fetch() result to the single object var taskArrayScheda.

You'll need something like:

var taskArrayScheda: [NewFilm]?

Then you should do:

taskArrayScheda = try context.fetch(NewAnime.fetchRequest()) as? [NewFilm]

I assume here that NewAnime is a subclass of NewFilm, which seems to make sense looking at these two class names.

Sign up to request clarification or add additional context in comments.

5 Comments

so I can change with code var taskArray = [NewFilm]()
@peg101 Yes, if you want to define a NewAnime array variable and set it to an empty array at the same time.
ok now I have error in this Func func mostraDatiNellaScheda(_ sched:[NewFilm]) { titoloScheda.text = sched.titolo Value of type '[NewAnime]' has no member 'titolo' }
NewFilm is my Entity
@peg101 shed is an array of NewFilm. So you must select one of the elements with something like if let film = sched.first { titoloScheda.text = film.titolo }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.