0

Why did Apple introduce Array when we already have NSArray and NSMutable arrays in Swift?

We can do every operation that is done in array by using NSMutableArray, then what is the use of declaring new collection type in Swift?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
shelly
  • 9
  • 1
  • NSArray and NSMutableArray are from Foundation, they're not part of Swift. So of course, as a programming language, Swift has its own way to use collections. NSArray and Array are not the same and they don't work the same way. – Eric Aya Aug 23 '16 at 12:55
  • but all the operations that we can perform using NSMutable array we can do using Arrays in swift....Then what is actual role of introducing it..... – shelly Aug 23 '16 at 13:00
  • I just told you. NSMutableArray is *not* part of Swift. It's part of Foundation, that you can import manually or that is automatically imported via Cocoa or UIKit. Remove this import and there's no NSArray/NSMutableArray available anymore. – Eric Aya Aug 23 '16 at 13:02
  • thnqew for the information...but can u plz tell me in which case or how Array is better than NSMutableArray – shelly Aug 23 '16 at 13:06
  • You're welcome. This is a matter of opinion, but here's mine: *always* use Swift's methods and collections. Only use Foundation's ones if you absolutely have no choice, like if you're working on a codebase extensively using Foundation - otherwise always use Swift stuff. Many reasons for this, the main one being that Swift is type safe. – Eric Aya Aug 23 '16 at 13:09
  • thnqew 4 ur time eric,but i am asking in the terms of performance...why and how its better.. i got your point that we should use only Swift stuff.... but still did not get ma answer – shelly Aug 23 '16 at 13:18
  • Some interesting performance comparisons here https://yalantis.com/blog/is-swift-faster-than-objective-c/ And that's just example, there is a ton of information about it, just google. – konrad.bajtyngier Aug 23 '16 at 13:24

0 Answers0