Swift: removing an object from NSOrderedSet

1k Views Asked by At

enter image description hereI found that a workaround to remove an object from NSOrderedSet is to remove it from its mutable copy. After fixing XCode's warnings that's the code I have:

let tempSet: NSMutableOrderedSet? = playset!.musicItems!.mutableCopy as? NSMutableOrderedSet
        tempSet!.remove(musicItem)
        playset!.musicItems = tempSet

But it gives me an error EXC_BAD_INSTRUCTION. How can I fix it?

1

There are 1 best solutions below

0
Sulthan On BEST ANSWER

mutableCopy is a method, it must be called as a method, add () after it:

playset!.musicItems!.mutableCopy() as? NSMutableOrderedSet

You are currently trying to cast the method () -> Any? itself to an NSMutableOrderedSet, not the result of calling that method. Hence the warning.

The cast will never succeed and tempSet will always stay nil, crashing on the next !.