1

I migrated from swift 1.0 to swift 1.2 and I noticed that the bitwise NOT operator ( ~ ) not longer works in this simple exemple:

   var open : Bool = false
   open = ~open

'~' is unavailable use '!' operator instead

is it a new way to do now ? Any help would be greatly appreciated

thesecretmaster
  • 1,950
  • 1
  • 27
  • 39
Armanoide
  • 1,248
  • 15
  • 31
  • 2
    For a boolean it's equal to the exclamation mark, use it instead, makes more sense (Well you have to because that's Swift 2) – Kametrixom Jul 07 '15 at 00:13
  • @Kametrixom that weird it can't inverse bit for boolean but your solution work thanks. – Armanoide Jul 07 '15 at 00:23
  • 3
    I think it's because a boolean doesn't have to be represented as a 1 or 0. It's abstracted away, so that a boolean can be any of true/false, on/off, etc. it just represents two states. That's why I think they removed the ~, because it doesn't make sense to inverse bits, because a boolean doesn't represent bits – Kametrixom Jul 07 '15 at 00:48

1 Answers1

0

For some reason, Swift once supported bitwise operators on the Bool type.

In a high level language like Swift featuring a Bool data type which can only be one of "true" or "false," you shouldn't use bitwise operators like ~ (NOT), | (OR) and & (AND). Use conditional operators ! (NOT), || (OR) and && (AND).

Related: What is the difference between logical and conditional AND, OR in C#?

It's a different language, but luckily the implementation of these operators is practically identical among most languages with C-like syntax.

Community
  • 1
  • 1
Rikki Gibson
  • 4,136
  • 23
  • 34