NSNumber and NSString in Objective-C With Examples

In this tutorial we’ll discuss NSNumber and NSString data types used in Objective-C. The Foundation Framework defines several classes that provide the standard, object-oriented data structures found in other high level languages

NSNumber

We can’t use primitive data types like int and float directly in Foundation classes. For that NSNumber is used as an object wrapper for these primitives. It’s main job is to store and retrieve primitive values to and from the box. The NSNumber version of BOOL’s, char’s, int’s and double’s can all be created by simply prefixing the corresponding primitive type with the @ symbol; however, unsigned int’s, long’s, and float’s must be appended with the U, L, or F modifiers, as shown below.

It’s possible to box arbitrary C expressions using the @() syntax hence allowing us to convert basic arithmetic calculations in NSNumber objects as shown below :

In the above code doubleValue is a dedicated method to return the primitive datatype from the NSNumber type.

It’s not possible to change its associated value of NSNumber after we create it. They are immutable. In this sense, an NSNumber instance acts exactly like a primitive value. When we need a new double value, we create a new literal and don’t change an existing one.

Comparing Numbers

In general, Objective-C has two types of comparison methods :

  • Pointer comparison uses the == operator to see if two pointers refer to the same memory address (i.e., the same object). It’s not possible for different objects to compare equal with this kind of comparison
  • Value comparison uses methods like isEqualToNumber: to see if two objects represent the same value. It is possible for different objects to compare equal with this kind of comparison

The below snippet shows one such example to compare two NSNumbers :

isEqualToNumber: guarantees that two values will compare equal, even if they are stored in different objects.

NSString

Like NSNumber, NSString is also immutable type. It’s used to represent text in Objective-C. NSString provides built-in support for Unicode, which means that we can include UTF-8 characters directly in string literals.
The most basic way in which NSString object is represented is shown below :

stringWithFormat: class method is useful for generating strings that are composed of variable values. It takes the same kind of format string as NSLog(). Try the following snippet in a new empty project!

Comparing Strings

String comparisons are similar to NSNumber Comparison except the fact that isEqualToString: method is used. For partial comparisons hasPrefix: and hasSuffix: are used.

A compare: method is used for alphabetically sorting strings as shown in the example below:

In the above snippet we’ve displayed the three types of return values and their description as shown below :

  • NSOrderedAscending : receiver < argument
  • NSOrderedSame : receiver == argument
  • NSOrderedDescending : receiver > argument

Combining Strings

Searching Strings

NSString’s search methods return an NSRange struct, which consists of a location and a length field. The location is the index of the beginning of the match, and the length is the number of characters in the match. If no match is found, location will contain NSNotFound.

We’ve used a random string in the above example and the starting index and the length of the substring matched is printed in the NSLog.(We’ve shown that result in the comments. You can try the example in an XCode Project.

NSMutableStrings

The NSMutableString class is a mutable version of NSString. Unlike immutable strings, it’s possible to alter individual characters of a mutable string without creating a new object. The methods discussed above are applicable in NSMutableString too. Although some methods such as stringByAppendingString: will still return a NSString object—not an NSMutableString.

Creating Mutable Strings

stringWithString is used to create a NSMutableString from a literal or an existing NSString object.

The setString: method lets us assign a new value to the instance as follows :

This brings an end to this tutorial. Our aim was to give a brief overview of NSNumber and NSString Data Types. There are plenty of methods that we’ll discuss is the series of iOS Tutorials.

By admin

Leave a Reply

%d bloggers like this: