xcode NSString 对象的最大长度是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6482641/
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
What is the maximum length of an NSString object?
提问by Christina
What is the maximum sized string that can be held in a NSString object?
NSString 对象中可以容纳的最大字符串是多少?
Does this change dynamically?
这是动态变化的吗?
回答by Spencer Uresk
I would assume the hard limit for NSString would be NSUIntegerMax characters, since NSString's index and size-related methods return an NSUInteger. Since all devices currently capable of running iOS are 32 bit, this means NSUIntegerMax is 2^32 - 1 and NSString can hold a little over 4.2 billion characters.
我假设 NSString 的硬限制是 NSUIntegerMax 字符,因为 NSString 的索引和大小相关的方法返回一个 NSUInteger。由于当前能够运行 iOS 的所有设备都是 32 位的,这意味着 NSUIntegerMax 是 2^32 - 1 并且 NSString 可以容纳超过 42 亿个字符。
As others have pointed out, though, the practical limit is much smaller - on an iOS device especially, you'll run out of memory long before you hit any hard limit in NSString.
不过,正如其他人指出的那样,实际限制要小得多——尤其是在 iOS 设备上,在达到 NSString 中的任何硬限制之前,你的内存早就耗尽了。
回答by sergio
NSString
is actually a class-cluster, so it is highly possible that different concrete classes (say, NSString
vs. NSMutableString
) will make us of different "backing stores" for storing the data. You could even subclass NSString
and provide your own backing store implementation, for specific needs you might have (look at "Subclassing Notes" for NSString).
NSString
实际上是一个类簇,所以很可能不同的具体类(比如NSString
vs. NSMutableString
)会让我们使用不同的“后备存储”来存储数据。您甚至可以子类化NSString
并提供您自己的后备存储实现,以满足您可能有的特定需求(查看NSString 的“子类化注释”)。
As to which backing store is actually used by NSString
, this is an implementation detail that is not documented by Apple, and it could change at any time.
至于 实际使用了哪个后备存储NSString
,这是 Apple 没有记录的实现细节,并且随时可能更改。
For myself, I assume that the maximum length of an NSString
is only limited by available memory. Actually, since available memory can be really huge, there will be some other limit (maybe a performance related one), but I have never incurred in such limit.
对于我自己,我假设 an 的最大长度NSString
仅受可用内存的限制。实际上,由于可用内存可能非常大,因此会有一些其他限制(可能与性能相关),但我从未遇到过这样的限制。
回答by Manoj
It can hold almost as much memory as can be represented by virtual memory system. But I personally feel the maximum length is only restricted to whatever memory available at that time.
它可以容纳几乎与虚拟内存系统所能表示的一样多的内存。但我个人觉得最大长度仅限于当时可用的任何内存。