Coding Study Notes:Foundation- NSNumber
Page 322 in the Kochan, Programming in Objective-C 2.0
· create objects so that values declared as C data types can be worked with as objects
· Use NSNUmber class to create objects from different data types
· Early example with integer value uses two lines, but later examples do conversion with one line of code, embedding the message/receiver syntax in along with the assignment statement
· program 15.1 from the text
// Working with Numbers
//#import <Foundation/Foundation.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSValue.h>
#import <Foundation/NSString.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSNumber *myNumber, *floatNumber, *intNumber;
NSInteger myInt; // why doesn't myInt need to be a pointer? Because it's used for conversion?
intNumber = [NSNumber numberWithInteger: 100];
// oh but wait, bet I can do the same thing in NSLog as I do with the other guys
NSLog (@" Let's try to put message receiver into NSLog %li", [intNumber integerValue]);
myInt = [intNumber integerValue]; // for some reason intNumber has to get converted using integerValue
NSLog (@"\n * * * *");
NSLog (@"\n Before conversion The integer object was %li", intNumber );
//I think we are casting the interger value as a long
NSLog (@" %li", (long) myInt);
// * * * long value
myNumber = [NSNumber numberWithLong: 0xabcdef];
NSLog (@"\n A long value %1x", [myNumber longValue]);
// * * * * char value
myNumber = [NSNumber numberWithChar: 'X'];
NSLog (@"\n A character value %c", [myNumber charValue]);
//float value
floatNumber = [NSNumber numberWithFloat:100.00];
NSLog (@"\n A float number %g", [floatNumber floatValue]);
//double
myNumber = [NSNumber numberWithDouble:12345e+15];
NSLog (@"%1g", [myNumber doubleValue]);
// wrong access
NSLog (@"%i", [myNumber integerValue]); // no error, I have to know what I'm doing uh oh
// Test two numbers for equality
if ([intNumber isEqualToNumber: floatNumber]== YES)
// NSLog (@"Numbers are equal %li, %g," [intNumber integerValue] [floatNumber floatValue]);
// NSLog (@"Numbers are equal %i" , intNumber ); runs clean - prints address of pointer
{
NSLog (@"Numbers are equal " );
// page 325
// NSLog format is [ my-NSNumber-declared-object retrieval-instance-method-for-NSNumber]
NSLog (@" \n this integer %li", [intNumber integerValue]);
NSLog (@" \n and a float I'm gonna show next " );
NSLog (@"\n This float number %g", [floatNumber floatValue]);
//&&&&&
NSLog (@" \n %li", (long) myInt);
}
else
NSLog (@"Numbers are not equal");
// Test if one Number is <, ==, or > second number
if ([intNumber compare: myNumber] == NSOrderedAscending)
NSLog (@"First number is less than second");
NSLog (@"First number is gonna print, then Second number \n");
NSLog (@" \n this integer %li", [intNumber integerValue]);
NSLog (@"%1g", [myNumber doubleValue]);
[pool drain];
return 0;
}
/*
run
[Switching to process 5458]
Running…
2011-06-06 22:02:40.622 NumberObjectsVer2[5458:a0f] Let's try to put message receiver into NSLog 100
2011-06-06 22:02:40.626 NumberObjectsVer2[5458:a0f]
* * * *
2011-06-06 22:02:40.626 NumberObjectsVer2[5458:a0f]
Before conversion The integer object was 4296052080
2011-06-06 22:02:40.627 NumberObjectsVer2[5458:a0f] 100
2011-06-06 22:02:40.627 NumberObjectsVer2[5458:a0f]
A long value abcdef
2011-06-06 22:02:40.628 NumberObjectsVer2[5458:a0f]
A character value X
2011-06-06 22:02:40.629 NumberObjectsVer2[5458:a0f]
A float number 100
2011-06-06 22:02:40.629 NumberObjectsVer2[5458:a0f] 1.2345e+19
2011-06-06 22:02:40.630 NumberObjectsVer2[5458:a0f] 0
2011-06-06 22:02:40.630 NumberObjectsVer2[5458:a0f] Numbers are equal
2011-06-06 22:02:40.631 NumberObjectsVer2[5458:a0f]
this integer 100
2011-06-06 22:02:40.631 NumberObjectsVer2[5458:a0f]
and a float I'm gonna show next
2011-06-06 22:02:40.632 NumberObjectsVer2[5458:a0f]
This float number 100
2011-06-06 22:02:40.632 NumberObjectsVer2[5458:a0f]
100
2011-06-06 22:02:40.633 NumberObjectsVer2[5458:a0f] First number is less than second
2011-06-06 22:02:40.633 NumberObjectsVer2[5458:a0f] First number is gonna print, then Second number
2011-06-06 22:02:40.634 NumberObjectsVer2[5458:a0f]
this integer 100
2011-06-06 22:02:40.634 NumberObjectsVer2[5458:a0f] 1.2345e+19
Debugger stopped.
Program exited with status value:0.
*/
No comments:
Post a Comment