Amazon.com Widgets NSNumber query
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 02:18:39 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Chapter Study
| | |-+  Chapter 15 - Numbers, Strings, and Collections
| | | |-+  NSNumber query
Pages: [1]   Go Down
Print
Author Topic: NSNumber query  (Read 860 times)
MikeF
Newbie
*
Posts: 47


Email




« on: December 29, 2010, 12:21:27 PM »

Hello
below i've initialized 2 number objects using both allowable methods(as referenced on p 326). Am i right in saying that myInt1 and myInt2  cannot be changed to contain new values after their initialization?

NSNumber *myInt1 = [NSNumber numberWithInt: 100];  

NSNumber *myInt2;
myInt2 = [[NSNumber alloc] initWithInt:200];
   



« Last Edit: December 31, 2010, 07:08:33 AM by MikeF » Logged
TotalLuck
Full Member
***
Posts: 107



WWW Email




« Reply #1 on: December 29, 2010, 07:59:27 PM »

NSNumber is just a typeDef  for an int .  so there is no need to [NSNumber alloc]..


your first  statement is correct
Code: (Objective-C)
NSNumber *myInt1 = [NSNumber numberWithInt: 100];  

after you declare myInt1 in that statement you can reassign it at any time using the same statement as such
Code: (Objective-C)
myInt1 = [NSNumber numberWithInt: 99];  

so not this
Code: (Objective-C)
[color=red][font=Verdana]NSNumber *myInt2;
myInt2 = [[NSNumber alloc] initWithInt:200][/font][/color];

this
Code: (Objective-C)
NSNumber *myInt2= [NSNumber initWithInt:200];

hope that helps..
Logged

Apps available on  iTunes store:
"ADACode"  iPhone and iPad versions, "ADAGuidelines" iPhone & iPad versions,  "Rehabilitation Act of 1973" for iPhone
"APokerTimer" now for iPhone http://bit.ly/h1fAJp
MikeF
Newbie
*
Posts: 47


Email




« Reply #2 on: December 31, 2010, 07:05:58 AM »

Hello,
how can i change the contents of an NSNumber object easily? Is there a setter for this? I've noticed that when i step through my code with the debugger  it seems to happily accept the following lines of code, although it's obviously the wrong thing to do.

All i want to do is change the value from 100 to 200?


NSNumber *myInt1 = [NSNumber numberWithInt: 100];
NSNumber *myInt1 = [NSNumber numberWithInt: 200];



I'm new to the language so please excuse my lack of knowledge.. , but on the face of it i guess I'm struggling to understand the point of NSNumbers as apposed to say 'int' ?.  I realize you can store them arrays, and I'm guessing this fact is crucial for reasons i don't understand yet.
Logged
Jake
Newbie
*
Posts: 14






« Reply #3 on: December 31, 2010, 10:19:54 PM »

Do recall ( from chapter 8 ) that you, as the programmer, are not responsible for releasing objects from class that you did not created. NSNumber is.  What you done is slightly wrong (however, it's almost right) as you wrote this:

Code: (Objective-C)
// The wrong way
NSNumber *myInt1 = [NSNumber numberWithInt: 100];
NSNumber *myInt1 = [NSNumber numberWithInt: 200];

Once you declare an pointer to an object, you do not need to re-declare it again unless it is already destroy. (Actually, I think that if you re-declare it, it cause problems; I'm not sure on this though.)

The more correct way is this:

Code: (Objective-C)
// The correct way 
NSNumber *myInt1 = [NSNumber numberWithInt: 100];
myInt1 = [NSNumber numberWithInt: 200];

As you can see, I declare a pointer call myInt1 pointing to a NSNumber with the integer100. Reassigning a value to myInt1 is fine.

Steve (or some other expert), please tell me if my answer is right or not (as I'm not completely faithful if I answer  correctly)!
« Last Edit: December 31, 2010, 11:00:43 PM by Jake » Logged
Pages: [1]   Go Up
Print
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.