简体   繁体   English

Swift 中的普通数组 vs 'NSMutableArray'?

[英]Normal array in Swift vs 'NSMutableArray'?

So in Swift, what's the difference between那么在 Swift 中,有什么区别

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.).两者似乎都具有人们可能需要的所有基本功能( .count ,插入/删除对象的能力等)。

NSMutableArray was invented back in the Obj-C days, obviously to provide a more modern solution instead of the regular C-style arrays. NSMutableArray是在 Obj-C 时代发明的,显然是为了提供更现代的解决方案,而不是常规的 C 样式数组。 But how does it compare to Swift's built-in array?但是它与 Swift 的内置数组相比如何呢?

Which one is safer and/or faster?哪个更安全和/或更快?

The most important difference, in my opinion, is that NSMutableArray is a class type and Array is a value type.在我看来,最重要的区别是 NSMutableArray 是一种类类型,而 Array 是一种值类型。 Ergo, an NSMutableArray will be passed as a reference, whereas a Swift Array will be passed by value.因此,NSMutableArray 将作为引用传递,而 Swift Array 将通过值传递。

Furthermore NSMutableArray is a subclass of NSObject whereas Array has no parent class.此外 NSMutableArray 是 NSObject 的子类,而 Array 没有父类。 - this means that you get access to all NSObject methods and other 'goodies' when utilising NSMutableArray. - 这意味着您在使用 NSMutableArray 时可以访问所有 NSObject 方法和其他“好东西”。

An NSMutableArray will not be copied when you amend it, a Swift Array will be.修改时不会复制 NSMutableArray,而会复制 Swift Array。

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.我发现(当使用 UIKit 和 Cocoa touch 时)当我需要一个持久模型时 NSMutableArray 非常棒,而 Array 非常适合性能并丢弃数组。

These are just my initial thoughts, I'm sure someone from the community can offer much deeper insight.这些只是我的初步想法,我相信社区中的某个人可以提供更深入的见解。

Reference Type When :(NSMutableArray)引用类型何时:(NSMutableArray)

Subclasses of NSObject must be class types Comparing instance identity with === makes sense You want to create shared, mutable state NSObject 的子类必须是类类型 将实例标识与 === 进行比较是有意义的 您想要创建共享的、可变的状态

Value Type When: (Swift array)值类型何时:(Swift 数组)

Comparing instance data with == makes sense (Equatable protocol) You want copies to have independent state将实例数据与 == 进行比较是有意义的(Equatable 协议)您希望副本具有独立的状态
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有趣的是,Swift 标准库非常偏爱值类型:原始类型(Int、Double、String 等)是值类型标准集合(Array、Dictionary、Set 等)是值类型

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 ,值得知道的是,在 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.但重要的是要记住,如果您的值类型变得太大,复制的性能成本可能会大于使用引用类型的成本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM