Changes

Jump to: navigation, search

Understanding Objective-C Number Objects

3,505 bytes added, 16:13, 5 November 2009
no edit summary
The full list of creation and initialization methods is as follows:
numberWithBool<br>numberWithChar<br>numberWithDouble<br>numberWithFloat<br>numberWithInt<br>numberWithInteger<br>numberWithLong<br>numberWithLongLong<br>numberWithShort<br>numberWithUnsignedChar<br>numberWithUnsignedInt<br>numberWithUnsignedInteger<br>numberWithUnsignedLong<br>numberWithUnsignedLongLong<br>numberWithUnsignedShort<br>
Because these are class methods, rather than instance methods, they are called on the class rather than on an existing object. For example, to create an NSNumber object called myNumber myFloat, configured to hold a float value, we could write the following code:
<pre>
NSNumber *myFloat;
myFloat = [NSNumber numberWithFloat: 10.09];
</pre>
 
Once executed, the above code will create a new NSNumber object and assign the floating point value of 10.09 to it.
 
== Getting the Value of a Number Object ==
 
The current value stored in a number object can be obtained using one of a number of retrieval instance methods. The type of value retrieved depends on the method used. The full list of retrieval instance methods is as follows:
 
boolValue<br>
charValue<br>
decimalValue<br>
doubleValue<br>
floatValue<br>
intValue<br>
integerValue<br>
longLongValue<br>
longValue<br>
shortValue<br>
unsignedCharValue<br>
unsignedIntegerValue<br>
unsignedIntValue<br>
unsignedLongLongValue<br>
unsignedLongValue<br>
unsignedShortValue<br>
 
For example, we can extend our example to retrieve and display the value stored in our number object as follows:
 
<pre>
NSNumber *myFloat;
float floatvalue;
 
myFloat = [NSNumber numberWithDouble: 10.09];
 
floatvalue = [myFloat floatValue];
 
NSLog (@"Value = %f", floatvalue);
</pre>
 
Note that the method used to retrieve the value stored in a number object must match the method used to store the object. An attempt, for example, to retrieve a char from an object initialized as double will provide an unexpected result.
 
== Comparing Number Objects ==
 
To compare the values stored in two number objects it is necessary to use either the ''isEqualToNumber'' or ''compare'' methods. These are both instance variables, and as such are called on one object instance, passing through the second object as an argument.
 
''isEqualToNumber'' returns a Boolean value depending on whether the two objects contain the same numbers. For example:
 
<pre>
NSNumber *myFloat1;
NSNumber *myFloat2;
 
myFloat1 = [NSNumber numberWithDouble: 10.09];
myFloat2 = [NSNumber numberWithDouble: 10.08];
 
if ([myFloat1 isEqualToNumber: myFloat2])
NSLog (@"Numbers are equal");
else
NSLog (@"Numbers are not equal");
</pre>
 
The ''compare'' method is used when you need to know whether one number is less than, greater than or equal to another when those numbers are held in number objects.The method returns the result of the comparison in the form of an NSComparisonResult enumeration. Possible settings are ''NSOrderedSame'' if the numbers are equal, ''NSOrderedAscending'' is the value stored in the first object is less than the number stored in the second and ''NSOrderedDescending'' if the opposite is true:
 
<pre>
myFloat1 = [NSNumber numberWithDouble: 10.09];
myFloat2 = [NSNumber numberWithDouble: 10.08];
NSComparisonResult result;
result = [myFloat1 compare: myFloat2];
 
if (result == NSOrderedSame)
NSLog(@"Numbers are equal");
else if (result == NSOrderedAscending)
NSLog(@"Float1 is less than Float2");
else if (result == NSOrderedDescending)
NSLog(@"Float1 is greater than Float2");
</pre>
 
== Getting the Number Object Value as a String ==
 
In order to convert the value stored in a number object to a string, the ''stringValue'' instance method is provided by the NSNumber class. This method returns the current value as a string object (for more information on string objects refer to [[Working with String Objects in Objective-C]]):
 
<pre>NSNumber *myFloat;
 
NSString *myString;
myFloat = [NSNumber numberWithDouble: 10.09];
 
myString = [myFloat stringValue];
 
NSLog (@"Number as string is %@", myString);
</pre>

Navigation menu