4

There is no nonatomic keyword in swift, Why nonatomic not required in swift as it exist in objective c.

Mahendra Y
  • 1,941
  • 20
  • 26

1 Answers1

5

In Swift the nonatomic is the default (and only) choice, so it is not required, unlike Objective-C where atomic is the default but often not the desired behaviour.

As for why Swift does not offer atomic, well, I guess it has not been seen as a necessary feature by the language designers. Of course you can implement atomic properties with synchronisation, mutexes, semaphores, etc. These solutions are more verbose, but also allow making a truly thread-safe class, unlike just making all properties atomic in Objective-C.

Arkku
  • 41,011
  • 10
  • 62
  • 84
  • 2
    +1 _"but often not the desired behaviour"_ i have seen `atomic NSArray/NSDictionary` code too many times, where it was magically expected to guard the array operations – Pavel Zdenek Oct 26 '16 at 09:22
  • And "atomic" doesn't _really_ protect you against race conditions - if two threads set a property at the same time, the fact that they set it won't crash if you use atomic, but you still don't know what is going to happen. Yes, you need @synchronized (or some alternative) plus use of your brain to make code thread safe. – gnasher729 Oct 26 '16 at 09:48