Welcome, Guest. Please login or register.
Did you miss your activation email?
September 07, 2010, 11:28:54 PM
Home Help Search chat Login Register   
News:   If you like the book and forum please  write a review on Amazon   Recorded lessons available for iPad viewing and download....in process! Consulting and onsite training available!   If you're running Xcode 3.2 (or later), see this post   Follow me on Twitter  @skochan  Forum Guests: register to download (or see) attachments.    Join our mailing list.


+  Official Forum for Programming in Objective-C 2.0 (the iPhone Programming Language) - Stephen Kochan
|-+  Programming in Objective-C 2.0, 2nd ed. (Stephen Kochan)
| |-+  Program Examples
| | |-+  Chapter 15
| | | |-+  Program 15.1
« previous next »
Pages: [1] Go Down Print
Author Topic: Program 15.1  (Read 1109 times)
TotalLuck
Jr. Member
**
Posts: 77



WWW Email
« on: February 15, 2009, 01:36:59 PM »

Code: (Objective-C)
#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSValue.h>
#import <Foundation/NSString.h>

int main(int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc ] init];
NSNumber * myNumber , *floatNumber, *intNumber;
NSInteger            myInt;

//integer value

intNumber = [NSNumber numberWithInt:100];
myInt = [intNumber integerValue];
NSLog(@"%li", (long) myInt);

// long int

myNumber = [NSNumber numberWithLong:0xabcdef];
NSLog(@"%lx",[myNumber longValue]);

//char value

myNumber = [NSNumber numberWithChar: 'X'];
NSLog(@"%c", [myNumber charValue]);

// float value

floatNumber = [NSNumber numberWithFloat: 100.00];
NSLog(@"%g", [floatNumber floatValue]);

//double

myNumber = [NSNumber numberWithDouble: 12345e+15];
NSLog(@"%lg", [myNumber doubleValue]);

//wrong access here

NSLog(@"%i", [myNumber integerValue]);

//test two numbers

if ([intNumber isEqualToNumber:floatNumber] == YES)
NSLog(@"Numbers are equal");
else
NSLog(@"Numbers are not equal");

//test if one number is  < , ==, or > second num

if ([intNumber compare:myNumber] == NSOrderedAscending)
NSLog(@"First number is less than second");
else
NSLog(@"first number is not less than second");

[pool drain];
return 0;
}
[/pre]

Program 15.1 output
Code: (Objective-C)
100
abcdef
x
100
1.2345e+19
0
Numbers are equal
First number is less than second
[/pre]

« Last Edit: February 15, 2009, 01:38:44 PM by TotalLuck » Logged

Apps available on  iTunes store:
http://itunes.apple.com/us/app/ada-code/id375590999?mt=8

Submitted:
http://sites.google.com/site/adacodeipad/
djeans
Newbie
*
Posts: 9


Email
« Reply #1 on: October 04, 2009, 05:27:35 PM »

Having a hard time understanding the following code.

Code: (Objective-C)
  intNumber = [NSNumber numberWithInt:100];  
    myInt = [intNumber integerValue]; 
    NSLog(@"%li", (long) myInt); 

Can anyone explain why this code
Code: (Objective-C)
NSLog(@"%i", [myNumber integerValue]);  

doesn't work? Or why this does?
Code: (Objective-C)
  NSLog(@"%lx",[myNumber longValue]); 

why does it work for longValue, but not for integerValue?

Thanks

Darron
Logged
skochan
Administrator
Hero Member
*****
Posts: 2250



« Reply #2 on: October 04, 2009, 07:00:31 PM »

Note that integerValue returns an NSinteger.  See the discussion of NSInteger on page 324.  It may either be a 32-bit or 64-bit value depending on the compiler options/system architecture.   The safest thing to do is to cast the value to a long as discussed on the page to prevent any assumptions about the size of that data type.

Cheers,

Steve Kochan
Logged
Dorien
Newbie
*
Posts: 3


« Reply #3 on: February 04, 2010, 06:59:45 PM »

So instead of using:
intNumber = [NSNumber numberWithInt: 100];  // create an object from an integer with a value of 100
NSLog(@"%i", [intNumber integerValue];  // retrieve and display the value

The following is used instead:
intNumber = [NSNumber numberWithInt: 100];   // creates an object from an integer with a value of 100
myInt = [intNumber integerValue];   // retrieves the value of 100 and stores it in myInt
NSLog(@"%li", (long) myInt);  // the qualifier long is used so the value is properly passed and displayed no matter the system architecture

Have I got this right?

On a related note, table 15.1 lists numberWithInteger and numberWithInt.  Are these the same methods?  I ask because numberWithInteger is not listed in my NSNumber Foundation Reference Library.
Logged
skochan
Administrator
Hero Member
*****
Posts: 2250



« Reply #4 on: February 08, 2010, 10:43:00 AM »

Yeah, it's in the documentation.  Look here: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html#//apple_ref/occ/clm/NSNumber/numberWithInteger:

If you create an integer using numberWithInt:, you should retrieve its value using intValue, and it can be displayed as an int using %i.

If you create an integer using numberWithInteger:, you should retrieve its value using integerValue, and it should be cast as a long if being passed to NSLog to be displayed using %li.   The cast is needed since the number may be a 32 or 64 bit value based upon the build options for the application and the casting guarantees a 64-bit value will be passed to NSLog to be displayed.

Cheers,

Steve
« Last Edit: February 08, 2010, 11:04:28 AM by skochan » Logged
Dorien
Newbie
*
Posts: 3


« Reply #5 on: February 08, 2010, 03:18:04 PM »

Thanks for the link and the explanation.  Both were very helpful.
Logged
dharma
Newbie
*
Posts: 21


« Reply #6 on: March 12, 2010, 09:16:09 PM »

Sorry to revive this thread, but basically we need the intermediate typedef'ed variable myInt, *just* to be able to cast it as a long later on for the NSLog call, right? Just for making working with integers architecture-independent I guess?

So, why would you ever want to use numberWithInt?

Edit: so in the first post the code
Code: (Objective-C)
intNumber = [NSNumber numberWithInt:100];
should be
Code: (Objective-C)
intNumber = [NSNumber numberWithInteger:100];
right?
« Last Edit: March 12, 2010, 09:18:44 PM by dharma » Logged
skochan
Administrator
Hero Member
*****
Posts: 2250



« Reply #7 on: March 15, 2010, 10:08:57 PM »

Yes, that's a good catch!

The intermediate variable is not needed, just used for the sake of example.  You could write this sequence:

Code: (Objective-C)
NSNumber *intNumber = [NSNumber numberWithInteger: 100];    

NSLog(@"%li", (long) [intNumber integerValue]); 

thus avoiding the intermediate variable assignment.

Cheers,

Steve
Logged
dharma
Newbie
*
Posts: 21


« Reply #8 on: March 15, 2010, 11:21:10 PM »

Thanks for your reply Steve!

By the way, I know others have said it before, but it is *so* cool to have this forum associated with the book and have the writer (you!) actually answering things in person! I'm not aware of any other writer doing such a thing, but I do know some books that certainly could use it Wink Thanks so much for doing this, it really helps me getting more of a grip on Objective-C, it's very reassuring to be able to look things up, seeing other people's solutions, and your suggestions.
Logged
skochan
Administrator
Hero Member
*****
Posts: 2250



« Reply #9 on: March 15, 2010, 11:25:49 PM »

You're welcome!   Cool

Steve
Logged
Pages: [1] Go Up Print 
« previous next »
Jump to:  


Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.