Learn Courses My Dashboard

How to determine where optional value is coming from?

I have the following code:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        // Get a cell
        let cell = tableView.dequeueReusableCell(withIdentifier: "MealPlanCell", for: indexPath) as! MealPlanCell
        
        cell.coverImageView.image = nil
        
        // Get the mealPlan that the tableView is asking about
        let recipeURLInTable = recipeURL[indexPath.row]
        
        // Customize the cell
        cell.displayRecipe(recipeURLInTable)
        print("recipeURLInTable1234: \(recipeURLInTable)")
        
        // Return the cell
        return cell
        
    }

When I print("recipeURLInTable1234: \(recipeURLInTable)"), the printed values have Optional in the String:

recipeURLInTable1234: RecipeURL(recipeURL: Optional("https://www.ambitiouskitchen.com/seriously-the-best-healthy-turkey-chili/"))
recipeURLInTable1234: RecipeURL(recipeURL: Optional("https://www.bonappetit.com/recipe/bas-best-bolognese"))

I have no idea where these optionals are coming from. This is how I’m declaring recipeURL in the first place: private var recipeURL = [RecipeURL](). I don’t seem to have any optional variables anywhere, and I tried unwrapping everything with ! too.

Any thoughts as to where this “Optional” could be coming from in the printed string?

What does your recipeURL Variable look like?

You’re making this variable recipeURLInTable that you’re printing, but where’s the recipeURL come from?

@mikaelacaron This is the data type:

struct RecipeURL {
    var recipeURL: String!
}

And this is where the data is getting pulled from (Firestore):

func getRecipes() {
        
        let db = Firestore.firestore()

        if currentUser != nil {
            
            db.collection("recipes").document().getDocument { snapshot, error in
                self.recipeQuery = db.collection("recipes")
                self.queryRecipe()
            }
        }
    }

func queryRecipe() {
        
        recipeQuery.getDocuments { snapshot, error in

            if error == nil && snapshot != nil {
                
                var recipeURLs = [RecipeURL]()
                
                for doc in snapshot!.documents {
                    
                    let recipeURLFromFirestore = RecipeURL(
                        recipeURL: doc["recipeURL"] as? String)
                    
                    recipeURLs.append(recipeURLFromFirestore)                    
                }
                
                DispatchQueue.main.async {
                    self.delegateRecipeURLs?.recipeURLsRetrieved(recipeURLs: recipeURLs)
                }
            }
        }
    }

String! is syntactic sugar for Optional<String>!. So your recipeURL is an Optional, even though you have implicitly unwrapped it. That’s where your Optionals are coming from.

2 Likes

Thank you @roosterboy! That was correct. I changed it to the following:

struct RecipeURL {
    var recipeURL = ""
}