Filtering array of strings - dealing with unclear "type Bool?" error

I'm trying to create an auto-suggestion popover on a textField. In order to do that, I need to filter a string array based on characters already typed in textField (orangePlayerName). The data comes from a Core Data fetch (autoCompleteSuggestions.fetchNames()), to which I attach a .filter and .hasPrefix as follows:

ForEach( 
autoCompleteSuggestions.fetchNames().filter{ $0?.hasPrefix(orangePlayerName) }, id: \.self) {...}

                                    

I get the following error:

Cannot convert value of type 'Bool?' to closure result type 'Bool'

Insert '!' [Fix]

If I insert the '!', then I get

Cannot force unwrap value of non-optional type 'Bool'

Remove '!' [Fix]

which is a fun Kafkian infinite loop of unfixable errors! Which I don't understand!

I appreciate any help on this.

(Also, too, if there is a better way to accomplish my goal, let me know. Searchable wouldn't work here because if the name is new, the user needs to be able to add their name. The reason I want to have the autosuggest is so that you don't get duplicates on variations of name of same person - like "Alejandro" or "alejandro" or "ale". If you start typing and see your name in the popover, you can just click on it and you are done.)

Answered by Claude31 in 718710022

You don't provide enough information.

Could you show fetchNames func ? Does it return optional String ?

You say orangePlayerName is a TextField. Is it a String or a TextField or an optional String? How is it computed ?

Where do you insert ! ??

Maybe you should try:

autoCompleteSuggestions.fetchNames().filter{ ($0 ?? "").hasPrefix(orangePlayerName) }

But without more information, that's just a guess.

Accepted Answer

You don't provide enough information.

Could you show fetchNames func ? Does it return optional String ?

You say orangePlayerName is a TextField. Is it a String or a TextField or an optional String? How is it computed ?

Where do you insert ! ??

Maybe you should try:

autoCompleteSuggestions.fetchNames().filter{ ($0 ?? "").hasPrefix(orangePlayerName) }

But without more information, that's just a guess.

Filtering array of strings - dealing with unclear "type Bool?" error
 
 
Q