0

This is my model with Country And Brand entities.

enter image description here

I'm trying to get all brands with specified country name. Code looks like this:

    var brands: [Brand]!
    let brandRequest = Brand.createFetchRequest()
    brandRequest.predicate = NSPredicate(format: "country.name == %@", countryName!)

    if let fetchedBrands = try? container?.viewContext.fetch(brandRequest) {
        if fetchedBrands!.count > 0 {
            brands = fetchedBrands!
        }
    }

Everything is in database. Brands and Country are related properly, but fetchedBrands is "nil".

What am I doing wrong ?

emerog
  • 69
  • 3
  • 10
  • Why do you ignore a potential error? And why is the `container` optional and `brands` implicit unwrapped optional ? – vadian Dec 10 '17 at 10:22
  • @vadian thx for suggestion. It realize me that I'm forgot initialize container... – emerog Dec 10 '17 at 10:34

0 Answers0