This is a conditional statement. Think of it like an if
statement:
if indexPath.row.number == selectedItem {
cell.deselectStyle()
} else {
cell.dedeselectStyle()
}
If the condition is true, the code between ?
and :
will be executed. If not, the code after the :
will be called. You should know that the ?
has nothing to do with Optionals.
In your case, selectedItem
seems to be nil
. Therefore, you need to either only execute the code if selectedItem
is not nil
, you could use an if let
statement:
if let selectedItem = selectedItem {
indexPath.row.number == selectedItem ? cell.deselectStyle() : cell.dedeselectStyle()
}
Or, you could insert a default value that will be used instead of selectedItem
if it is nil
:
indexPath.row.number == (selectedItem ?? false) ? cell.deselectStyle() : cell.dedeselectStyle()
The code above will use either the value of selectedItem
, or false
, if selectedItem
is nil
. You can leave out the parentheses, I just put them there for better visualization.
I hope this helps :)