What is the maximum length of an NSString object?

Christina picture Christina · Jun 26, 2011 · Viewed 19.9k times · Source

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

Does this change dynamically?

Answer

Spencer Uresk picture Spencer Uresk · Jun 26, 2011

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.

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.