Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the maximum length of an NSString object?

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

Does this change dynamically?

like image 225
Vipin Avatar asked Jun 26 '11 07:06

Vipin


People also ask

What is the maximum length of a Cstring?

Microsoft Specific The maximum length of a string literal allowed in Microsoft C is approximately 2,048 bytes.

What does NSString mean?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.

What is the difference between NSString and string?

NSString is a classSwift is interoperatable with Objective-C and converts some Objective-C types to Swift types. Types that can be converted between Obj-C and Swift are known as bridged types. String and NSString are example of such bridged types and hence you can assign NSString to a String variable.


2 Answers

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.

like image 166
Spencer Uresk Avatar answered Sep 20 '22 13:09

Spencer Uresk


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.

like image 29
sergio Avatar answered Sep 18 '22 13:09

sergio