Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Normal array in Swift vs 'NSMutableArray'?

So in Swift, what's the difference between

var arr = ["Foo", "Bar"] // normal array in Swift

and

var arr = NSMutableArray.array() // 'NSMutableArray' object

["Foo", "Bar"].map {
    arr.addObject($0)
}

other than being different implementations of the same thing. Both appear to have all the basic features that one might need (.count, the ability to insert/remove objects etc.).

NSMutableArray was invented back in the Obj-C days, obviously to provide a more modern solution instead of the regular C-style arrays. But how does it compare to Swift's built-in array?

Which one is safer and/or faster?

like image 477
notadam Avatar asked Dec 14 '22 20:12

notadam


2 Answers

The most important difference, in my opinion, is that NSMutableArray is a class type and Array is a value type. Ergo, an NSMutableArray will be passed as a reference, whereas a Swift Array will be passed by value.

Furthermore NSMutableArray is a subclass of NSObject whereas Array has no parent class. - this means that you get access to all NSObject methods and other 'goodies' when utilising NSMutableArray.

An NSMutableArray will not be copied when you amend it, a Swift Array will be.

Which one is best really depends on your application.

I find (when working with UIKit and Cocoa touch) that NSMutableArray is great when I need a persistent model, whereas Array is great for performance and throw away arrays.

These are just my initial thoughts, I'm sure someone from the community can offer much deeper insight.

like image 56
Woodstock Avatar answered Jan 06 '23 21:01

Woodstock


Reference Type When:(NSMutableArray)

Subclasses of NSObject must be class types Comparing instance identity with === makes sense You want to create shared, mutable state

Value Type When: (Swift array)

Comparing instance data with == makes sense (Equatable protocol) You want copies to have independent state
The data will be used in code across multiple threads (avoid explicit synchronization)

Interestingly enough, the Swift standard library heavily favors value types:Primitive types (Int, Double, String, …) are value types Standard collections (Array, Dictionary, Set, …) are value types

Aside from what is illustrated above, the choice really depends on what you are trying to implement. As a rule of thumb, if there is no specific constraint that forces you to opt for a reference type, or you are not sure which option is best for your specific use case, you could start by implementing your data structure using a value type. If needed, you should be able to convert it to a reference type later with relatively little effort.

Conclusion:

Reference types incur more memory overhead, from reference counting and also for storing its data on the heap.

It's worth knowing that copying value types is relatively cheap in Swift,

But it’s important to keep in mind that if your value types become too large, the performance cost of copying can become greater than the cost of using reference types.

like image 21
Jaydeep Vyas Avatar answered Jan 06 '23 22:01

Jaydeep Vyas