1

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?

Elena Rubilova
  • 367
  • 4
  • 16
  • Are you sure tempSet has the musicItem? Check with breakpoints or put an if statement before tempset – SpaceDust__ Jun 16 '17 at 16:32
  • Any reason why you're not using a Swift `Set`? – JAL Jun 16 '17 at 16:32
  • 2
    Not related but consider to use less question and exclamation marks. Not everything is supposed to be optional. @JAL Swift lacks a native ordered set type. – vadian Jun 16 '17 at 16:39
  • @vadian Ah missed that detail. Likely a dupe of this: [Diagnosing EXC_BAD_INSTRUCTION in Swift standard library](https://stackoverflow.com/q/24189738/2415822) – JAL Jun 16 '17 at 16:46
  • the issue is that I don't know the type to use for mutableCopy (how to convert), I attached screenshot – Elena Rubilova Jun 16 '17 at 16:59

1 Answers1

1

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 !.

Sulthan
  • 128,090
  • 22
  • 218
  • 270