Hokkaido Hokkaido - 2 years ago 165
Swift Question

In Swift 2.0 what is the maximum length of a string?

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?

Answer Source

Following the official Apple documentation:

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

Since all devices were capable of running iOS are 32 bit, this means NSUIntegerMax is 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:

typedef long NSInteger;
typedef unsigned long NSUInteger;
typedef int NSInteger;
typedef unsigned int NSUInteger;

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

TEST: (on iPhone 6)

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download