在Swift 2.0中,字符串的最大长度是多少?

时间:2022-04-13 21:41:01

I have an application which utilizes a single string. This string contains data loaded from an array and then the string is exported to a text file.

我有一个使用单个字符串的应用程序。此字符串包含从数组加载的数据,然后将该字符串导出到文本文件。

My question is what is the longest length possible for this string, and when does it become a problem that it is getting too long?

我的问题是这个字符串可能的最长长度是什么,它什么时候变得太长了?

2 个解决方案

#1


5  

Following the official Apple documentation:

遵循Apple官方文档:

String is bridged to Objective-C as NSString, and a String that originated in Objective-C may store its characters in an NSString.

String作为NSString桥接到Objective-C,而起源于Objective-C的String可以将其字符存储在NSString中。

Since all devices were capable of running iOS are 32 bit, this means NSUIntegerMax is 2^32.

由于所有能够运行iOS的设备都是32位,这意味着NSUIntegerMax是2 ^ 32。

According to Swift opensource GitHub repo It would seem that its value is 2^64 = 18,446,744,073,709,551,615 ; hexadecimal 0xFFFFFFFFFFFFFFFF for the 64 bit devices, following this code:

根据Swift opensource GitHub repo看起来它的值是2 ^ 64 = 18,446,744,073,709,551,615; 64位设备的十六进制0xFFFFFFFFFFFFFF,遵循以下代码:

#if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif

// + (instancetype)
    //     stringWithCharacters:(const unichar *)chars length:(NSUInteger)length
...
maxLength:(NSUInteger)maxBufferCount
...

TEST: (on iPhone 6)

测试:(在iPhone 6上)

在Swift 2.0中,字符串的最大长度是多少?

#2


3  

String is bridged to Objective-C as NSString, and a String that originated in Objective-C may store its characters in an NSString. Since any arbitrary subclass of NSString can become a String, there are no guarantees about representation or efficiency in this case.

String作为NSString桥接到Objective-C,而起源于Objective-C的String可以将其字符存储在NSString中。由于NSString的任意子类都可以成为String,因此在这种情况下无法保证表示或效率。

What is the maximum length of an NSString object?

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

The hard limit for NSString would be NSUIntegerMax characters. NSUIntegerMax is 2^32 - 1 and NSString can hold a little over 4.2 billion characters.

NSString的硬限制是NSUIntegerMax字符。 NSUIntegerMax是2 ^ 32 - 1,NSString可以容纳超过42亿个字符。

According comments: for iPhone 5S and above since they are 64 Bit. It's 2^(64 - 1)

根据评论:对于iPhone 5S及以上版本,因为它们是64位。这是2 ^(64 - 1)

#1


5  

Following the official Apple documentation:

遵循Apple官方文档:

String is bridged to Objective-C as NSString, and a String that originated in Objective-C may store its characters in an NSString.

String作为NSString桥接到Objective-C,而起源于Objective-C的String可以将其字符存储在NSString中。

Since all devices were capable of running iOS are 32 bit, this means NSUIntegerMax is 2^32.

由于所有能够运行iOS的设备都是32位,这意味着NSUIntegerMax是2 ^ 32。

According to Swift opensource GitHub repo It would seem that its value is 2^64 = 18,446,744,073,709,551,615 ; hexadecimal 0xFFFFFFFFFFFFFFFF for the 64 bit devices, following this code:

根据Swift opensource GitHub repo看起来它的值是2 ^ 64 = 18,446,744,073,709,551,615; 64位设备的十六进制0xFFFFFFFFFFFFFF,遵循以下代码:

#if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif

// + (instancetype)
    //     stringWithCharacters:(const unichar *)chars length:(NSUInteger)length
...
maxLength:(NSUInteger)maxBufferCount
...

TEST: (on iPhone 6)

测试:(在iPhone 6上)

在Swift 2.0中,字符串的最大长度是多少?

#2


3  

String is bridged to Objective-C as NSString, and a String that originated in Objective-C may store its characters in an NSString. Since any arbitrary subclass of NSString can become a String, there are no guarantees about representation or efficiency in this case.

String作为NSString桥接到Objective-C,而起源于Objective-C的String可以将其字符存储在NSString中。由于NSString的任意子类都可以成为String,因此在这种情况下无法保证表示或效率。

What is the maximum length of an NSString object?

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

The hard limit for NSString would be NSUIntegerMax characters. NSUIntegerMax is 2^32 - 1 and NSString can hold a little over 4.2 billion characters.

NSString的硬限制是NSUIntegerMax字符。 NSUIntegerMax是2 ^ 32 - 1,NSString可以容纳超过42亿个字符。

According comments: for iPhone 5S and above since they are 64 Bit. It's 2^(64 - 1)

根据评论:对于iPhone 5S及以上版本,因为它们是64位。这是2 ^(64 - 1)