NSArray 大小和可变性

发布于 2024-12-17 18:30:25 字数 299 浏览 3 评论 0原文

在 Java 中,我通常会初始化特定大小的数组,然后在代码运行时添加和替换对象。在 Objective C 中,我无法使用 NSArray 做到这一点。我从 Java 移植过来的代码通常必须使用 NSMutableArray,我认为它的执行效率低于 NSArray。 (关于 NSMutableArray 如何存储其成员的最佳猜测是使用链接列表,但我可能是错的)

Objective C 是否有任何类型的数组具有固定大小并允许在数组内进行更改?为什么不能用 NSArray 替换某个对象处的对象?我可以用 C 数组做到这一点,为什么 Objective C 不行呢?

In Java, I commonly initialize arrays that are a specific size and then add and replace objects as my code goes on. In Objective C, I can't do that with an NSArray. Code that I have ported over from Java often has to use NSMutableArray, which I assume perform less efficent than NSArray. (My best guess as to how NSMutableArray stores it's members is using a linked list, but I could be wrong)

Are there any types of arrays for Objective C that have fixed sizes and allow changes within the array? Why is it not possible to replace objects at a certain object with NSArray? I can do this with C arrays, why not Objective C?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

生死何惧 2024-12-24 18:30:25

您的假设是错误的,NSMutableArray 不会比普通的 NSArray 慢。

NSMutableArray 不会使用链表,它将使用动态数组。除了需要调整大小的插入部分之外,它的速度与普通数组一样快。但由于它们之间的距离呈指数级增长,因此如果您不调整大小,它会摊销到几乎相同且相同的值。

它与Java的ArrayList基本相同。

Your assumption is wrong, NSMutableArray won't be any slower than a plain NSArray.

NSMutableArray is not going to use a linked list, it will use a dynamic array. It is every bit as fast as a plain array except on the insertions where it needs to resize. But since those get exponentially further apart, it amortizes to nearly the same, and identical if you don't hit a resize.

It's basically the same as Java's ArrayList.

滥情空心 2024-12-24 18:30:25

在 Objective-C 中,许多类都有可变和不可变的变体。不可变的 NSArray 可能会带来性能或内存方面的优势,但如果您需要一个可以修改的数组,只需使用 NSMutableArray 即可。没有理由惊慌或担心。这就是他们存在的目的。

您可以使用预期的容量来初始化可变数组,但您不会永远受限于该容量 - 如果需要,数组将超出容量。

int numberOfItems = ...
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfItems];

NSArray 可用的任何优化优势都取决于它的不可变性。因此,除了语法之外,当您实际需要使用的对象是可变的(例如,您需要替换您的情况下的对象)时,您无法意识到不可变对象的好处。

In Objective-C, many classes have mutable and immutable variants.There may be performance or memory benefits to immutable NSArrays, but if you require an array that can be modified, simply use an NSMutableArray. There's no reason to be alarmed or concerned. That's what they're there for.

You can initialize mutable arrays with intended capacities, though you are not forever bound to that capacity - the array will grow beyond capacity if necessary.

int numberOfItems = ...
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfItems];

Any optimization benefit available to NSArray is contingent on the fact that it is immutable. So syntax aside, you can't realize the benefits of an immutable object when what you need to actually use is mutable (eg, you need to replace objects in your case).

花开柳相依 2024-12-24 18:30:25

不要认为 NSMutableArray 对您来说太慢。简介,不要猜测。

您还可以查看 NSPointerArray 如果您正在开发 Mac OS X 应用程序。它在 iOS 上不可用。

Don't assume NSMutableArray is too slow for you. Profile, don't speculate.

You might also check out NSPointerArray if you're developing a Mac OS X app. It's not available on iOS.

纸伞微斜 2024-12-24 18:30:25

如果你想替换某个对象,你必须使用 NSMutableArray 。 创建数组并指定数组的大小。

 + (id)arrayWithCapacity:(NSUInteger)numItems   

or

- (id)initWithCapacity:(NSUInteger)numItems

并使用NSMutableArray 的

You have to use NSMutableArray if you want to replace a certain object. And create the array using

 + (id)arrayWithCapacity:(NSUInteger)numItems   

or

- (id)initWithCapacity:(NSUInteger)numItems

of NSMutableArray and specify the size of the array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文