blog

Home / DeveloperSection / Blogs / NSString Class of Foundation Framework

NSString Class of Foundation Framework

Tarun Kumar2174 02-Nov-2015

In iOS, NSString is one of the most commonly used classes in Foundation Framework. Technically this means subclasses of NSString , since it is a class cluster and is never used directly and each concrete subclass of NSString must override at least two of the methods defined by this class:


-length, and -characterAtIndex: first one will returns the length of the string, and second one is a unicode(32-bit) character at a specified index.

Note that the internal format of the string may be in any format. The class cluster design allows 8 bit, 16 bit, and 32 bit strings to all be stored internally when a given string does not include any characters from outside the set that can be expressed with these. The programmer can be largely oblivious to this and use these strings interchangeably.  The NSString subclass will transparently handle any conversion required. Although these are the only methods that need to be overridden, most of the methods in NSString will call getCharacters: range:, which writes a substring into a buffer provided by the caller. Subclasses that implement this directly, rather than using the superclass implementation that repeatedly calls -characterAtIndex:, will be much faster. Note that this method name begins with the get prefix. It return a value into the space which is provided by the caller. Contrast this with the length method, which does not have the get prefix, and just returns the length.

It is possible to create our own subclass of NSString, it is generally a better option to compose objects without subclassing.  An example of this in the Foundation framework is NSAttributedString. This responds to -stringValue messages to return the string for which it stores attributes, but cannot be used directly in place of a string.

NSString has one public subclass (which is also a class cluster) for representing strings that can be modified: NSMutableString. it adds methods for modifying characters. Only seven new methods are added by this class, with six being defined in terms of the one primitive method: replaceCharactersInRange:withString:

The NSString class has a lot of methods, many of them are to do with path handling.  One of the problems that OS X developers encountered a lot in the early days was the fact that Mac OS and OPENSTEP had different ways of representing paths.

Mac OS used a multi-routed file hierarchy with one file for each disk, with path components separated by colons. OPENSTEP used a UNIX - style file hierarchy, with a single root and path components separated by slashes. Mac OS X applications often had to deal with both. Fortunately, this was a problem that NeXT had already encountered.  Open-Step applications were able to run on Solaris, OPENSTEP, and Windows.  Windows file paths were similar in structure to classic Mac OS paths. NSString has a set of methods for adding and deleting path components and splitting paths apart in a way that is independent of the underlying filesystem representation. It is good practice to use these, rather than manually constructing paths. Recent versions of OS X have begun to move away from using file paths entirely, with a lot of methods now using URLs in the file://namespace instead of file paths.

There are fewer methods on NSString for dealing with these; however, the NSURL class provides a lot more.


Updated 13-Mar-2018

Leave Comment

Comments

Liked By