NSString对象的最大长度是多少?

时间:2021-12-06 21:40:38

What is the maximum sized string that can be held in a NSString object?

NSString对象中可以保存的最大大小字符串是多少?

Does this change dynamically?

这会动态变化吗?

3 个解决方案

#1


61  

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的任何硬限制之前很久就会耗尽内存。

#2


8  

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与NSMutableString)将使我们有不同的“后备存储”来存储数据。您甚至可以子类化NSString并提供您自己的后备存储实现,以满足您可能具有的特定需求(查看NSString的“Subclassing Notes”)。

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.

对于我自己,我假设NSString的最大长度仅受可用内存的限制。实际上,由于可用内存可能非常庞大,因此会有一些其他限制(可能是与性能相关的限制),但我从未发生过这样的限制。

#3


3  

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.

它可以容纳几乎与虚拟内存系统一样多的内存。但我个人认为最大长度仅限于当时可用的内存。

#1


61  

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的任何硬限制之前很久就会耗尽内存。

#2


8  

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与NSMutableString)将使我们有不同的“后备存储”来存储数据。您甚至可以子类化NSString并提供您自己的后备存储实现,以满足您可能具有的特定需求(查看NSString的“Subclassing Notes”)。

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.

对于我自己,我假设NSString的最大长度仅受可用内存的限制。实际上,由于可用内存可能非常庞大,因此会有一些其他限制(可能是与性能相关的限制),但我从未发生过这样的限制。

#3


3  

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.

它可以容纳几乎与虚拟内存系统一样多的内存。但我个人认为最大长度仅限于当时可用的内存。