ios NSArray 的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2512040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:03:21  来源:igfitidea点击:

Size of an NSArray

iosobjective-carrays

提问by Florent

How do you get the size of an NSArrayand print it in the console using NSLog?

您如何获取 an 的大小NSArray并使用 将其打印在控制台中NSLog

回答by Alex Wayne

int size = [array count];
NSLog(@"there are %d objects in the array", size);

回答by Dave DeLong

An answer to another answer:

另一个答案的答案:

You can't get the size of the array in megabytes, at least not without doing some tricky, unsupported* C voodoo. NSArrayis a class cluster, which means we don't know how it's implemented internally. Indeed, the implementation used can change depending on how many items are in the array. Moreover, the size of the array is disjoint from the size of the objects the array references, since those objects live elsewhere on the heap. Even the structure that holds the object pointers isn't technically "part" of the array, since it isn't necessarily calloc'd right next to the actual NSArrayon the heap.

您无法获得以兆字节为单位的数组大小,至少在不做一些棘手的、不受支持的* C 巫术的情况下无法获得。NSArray是一个类簇,这意味着我们不知道它在内部是如何实现的。实际上,所使用的实现可以根据数组中有多少项而改变。此外,数组的大小与数组引用的对象的大小不相交,因为这些对象位于堆的其他地方。甚至保存对象指针的结构在技术上也不是数组的“一部分”,因为它不一定就在堆上calloc的实际旁边NSArray

If you want the size of the array struct itself, well that's apparently only 4 bytes:

如果您想要数组结构本身的大小,那么显然只有 4 个字节:

NSLog(@"Size: %d", sizeof(NSArray));

Prints:

印刷:

2010-03-24 20:08:33.334 EmptyFoundation[90062:a0f] Size: 4

(Granted, that's not a decent representation, since NSArrayis probably just an abstract interface for another kind of object, usually something like an NSCFArray. If you look in NSArray.h, you'll see that an NSArrayhas no instance variables. Pretty weird for something that's supposed to hold other objects, eh?)

(当然,这不是一个体面的表示,因为NSArray它可能只是另一种对象的抽象接口,通常类似于 an NSCFArray。如果您查看NSArray.h,您会看到 anNSArray没有实例变量。对于应该有的东西来说很奇怪拿着其他物体,嗯?)

*By "unsupported" I mean "not recommended", "delving into the inner mysticism of class clusters", and "undocumented and private API, if it even exists"

*“不受支持”是指“不推荐”、“深入研究类集群的内部神秘主义”和“未记录的私有 API,如果它存在的话”

回答by crafterm

Size can be determined by sending 'count' to the NSArray instance, and printing to console can be done via NSLog(), eg:

大小可以通过向 NSArray 实例发送 'count' 来确定,并且可以通过 NSLog() 打印到控制台,例如:

NSArray * array = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];
NSLog(@"array size is %d", [array count]);

回答by Chris Wagner

Take a look at this post for your array size question

看看这篇文章你的数组大小问题

Length of an Array in Objective C

目标 C 中数组的长度

Use NSLog to write to the console...

使用 NSLog 写入控制台...

NSLog(@"The array size is %@", arraySize);

回答by Ankit garg

In Swift 4

在斯威夫特 4

let a = ["a","b"]

让 a = ["a","b"]

a.count //2

a.count //2