33

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

Does this change dynamically?

Vipin
  • 4,718
  • 12
  • 54
  • 81
  • I edited you post. I just wasn't sure what was meant by "dynamically?", so the last question may not reflect the intent currently. –  Jun 26 '11 at 08:10
  • 1
    It can definitely hold much more than there's RAM available on any iPhone or iPad. In other words, you cannot hit NSString's limits on current iOS devices, but you can run out of memory. – Costique Jun 26 '11 at 08:52
  • i agree with Spencer Uresk answer..... – Himanshu Agnihotri Oct 16 '12 at 14:37

3 Answers3

63

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.

Spencer Uresk
  • 3,730
  • 26
  • 23
  • 3
    I gave +1. However, this is flawed in that it assumes 1 character = 1 byte. –  Jun 26 '11 at 08:12
  • It can't be `NSUIntegerMax` _plus_ `1` -- then `length` would not be able to return the correct value at one end or other of the range. – jscs Jun 26 '11 at 08:12
  • @pst - I tried to be careful and only refer to characters, not bytes. @Josh - You are correct. I'll edit my answer. – Spencer Uresk Jun 26 '11 at 08:15
  • 3
    @pst: `NSString`s nominally store their characters as `unichar`s, which are two bytes; however, they will try to save memory by using only one byte per character, if all their characters are representable in plain ASCII -- see [String Storage](http://developer.apple.com/library/mac/documentation/CoreFoundation/Conceptual/CFStrings/Articles/StringStorage.html#//apple_ref/doc/uid/20001179-CJBEJBHH) – jscs Jun 26 '11 at 08:16
  • 1
    Note: on Mac OS X it is 18,446,744,073,709,551,615 bytes (2^64 - 1). See NSObjCRuntime where there is a separate definition for NSUInteger depending on the target platform. – Abhi Beckert Jul 23 '13 at 03:36
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).

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.

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.

sergio
  • 68,819
  • 11
  • 102
  • 123
  • 1
    The length property is an NSUInteger, so regardless of class clusters that will be the max possible value. It also accepts an NSData object during initialisation which also has an NSUInteger for it's length property. NSUInteger is 32 bit on iOS and 64 bit on OS X. I have successfully worked with strings larger than 4GB on OS X. – Abhi Beckert Jul 23 '13 at 03:39
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.

Manoj
  • 953
  • 2
  • 8
  • 24