Nsnumber out of scope problem - iphone

I am trying to make NSNumber *percent to get the integer value percentint but it keeps making it out of scope.. The Nslog logs are like this:
The value of integer num is 4
The value of integer num is with NsNumber
78910432
My code is this:
In my header file:
int percentint;
NSNumber *percent;
#property(nonatomic,retain) NSNumber *percent; //tried without using this too
In my .m file:
#synthesize percent; //tried without using this too
percentint=4;
NSLog(#"The value of integer num is %i", percentint);
percent= [NSNumber numberWithInt:percentint];
percent= [[NSNumber alloc] initWithInt:percentint]; //tried without using this too.
[percent autorelease]; //tried without using this too.
NSLog(#"The value of integer num is with NsNumber %i", percent);

Try:
percentint=4;
NSLog(#"The value of integer num is %i", percentint);
self.percent= [NSNumber numberWithInt:percentint];
NSLog(#"The value of integer num is in array %#", self.percent);
NSLog(#"The value of integer num is in array %d", [self.percent intValue]);
leaving the #synthesize percent; in.

User #Felz is correct. in order to get the value of a NSNumber, you must retrieve it with a method call:
int percentInt = 95;
NSNumber *percent = [NSNumber numberWithInt:percentInt];
int myint = [percent intValue];
What you did instead, was print out the pointer address for percent
NSLog(#"Percent Value: %d",[percent intValue]);
NSLog(#"Percent Address: 0x%X",percent);
Remember that NSNumber *percent means percent is a pointer not a value.

Like the other user insinuated, NSNumber is not an integer as you are trying to call it in the last NSLog. to get the int value out of a NSNumber, use [percent intValue].
Another side note: you don't need to initialize percent twice. The first call numberWithInt: is like doing an alloc/init/release.
Also, never release an object before you are done with it.

Related

Xcode Gives Strange Output From NSArray

When I run this code, the output is some 1084848 to the console. I can't figure out why such odd output... here is the code.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *array = [[NSMutableArray alloc] init];
int someNumber = 3;
[array addObject:[NSNumber numberWithInt:someNumber]];
NSLog(#"%i" , [array objectAtIndex:0]);
[pool drain];
return 0;
}
the "%i" format specifier expects an integer, not an Object.
Try NSLog(#"%i" , [[array objectAtIndex:0] intValue]);
XCode is probably giving you a warning on this line: something like "Conversion specifies type 'int', but argument has type 'id'".
Here's the pseudocode of your program:
//
// Inside of your main function....
//
// Set up the Autorelease pool and then create an array
//
// Declare an int
//
// Add the int to an array, while wrapping it in an NSNumber
//
// Log the value of the first object in the array, using the int formatter
//
// Clean up and return
//
You are logging the first object in the array, but NSArray cannot hold a primitive that's not wrapped in an Objective-C object.
To better understand your code, try changing these lines of code:
int someNumber = 3;
[array addObject:[NSNumber numberWithInt:someNumber]];
Expand them a little. Try this:
int someNumber = 3;
NSNumber *aNumber = [NSNumber numberWithInt:someNumber];
[array addObject:aNumber];
So, you've correctly wrapped the int in an NSNumber, but you're not unwrapping it. You need to ask your NSNumber for the int that it holds like so:
[[array objectAtIndex:0] intValue];
Or, to do the logging in one line:
NSLog(#"%i" , [[array objectAtIndex:0] intValue]);
The characters "%i" is called a "formatter". Different kinds of values require different formatters. When you are using an Objective-C object, you use "%#". For an NSInteger or int, you'd use %i. For a float, you'd use "%f". The point is that you need to either unwrap that number, or use the Objective-C formatter for strings.
A quick note about that weird value you were getting earlier: That's a memory address in RAM. It's the closest thing you're going to get when you use an incorrect formatter. In some cases, using the wrong formatter will cause an EXC_BAD_ACCESS. You were "lucky" and got a weird value instead of a dead program. I suggest learning about strings and formatters before you move on. It will make your life a lot easier.
When you use %i for integer values then you should give arguments as integer as below
NSLog(#"%i" , [[array objectAtIndex:0] intValue]);
But when you want object to be displayed then you must use %# which identifies object in general case as below:
NSLog(#"%#", array);

nsmutablearray float object returns a zero

In the root model I have:
[self.rPrices replaceObjectAtIndex:0
withObject:[NSNumber numberWithFloat:(float)nR4]];
NSLog(#"%.2f", [self.rPrices objectAtIndex:0]);
where rPrices is NSMutableArray.
nR4 is not zero but the above NSLog(...); displays zero.
Thanks
Try this
NSLog(#"%.2f", [[self.rPrices objectAtIndex:0] floatValue]);
or alternativly just print the NSNumber as an object
NSLog(#"%#", [self.rPrices objectAtIndex:0]);
NSNumber is an object. So you can't print it using float format specifier "%f".
You can use "%#" or get the float value from it using -floatValue and print it using "%f".

Getting the contents of a text field as a number

I have a textfield in my app, whre the user can enter any number (I have set to Number pad)
I want to store this as an integer in a variable
I am writing
int numOfYears = [numOfYearsFld text];
But for some reason it is taking it incorrectly e.g. if user enters 10, it taakes as 10214475
Am I doing something wrong ?
intValue returns an int.
integerValue returns an NSInteger.
NSInteger numOfYears = [[numOfYearsFld text] integerValue];
[numOfYearsFld text] will be a NSString*. Try:
int numOfYears = [[numOfYearsFld text] intValue];
Try:
int numOfYears = [[numOfYearsFid text] intValue];
or, preferably
NSInteger numOfYears = [[numOfYearsFid text] integerValue];
intValue returns an int and integerValue returns an NSInteger.

Problem when converting NSString to NSNumber in iPhone

I am having problem when converting string (YaxisData) to NSNumber. I have to return a NSNumber for Core-plot to get the graph done but its not working. Here's the sample code
-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
NSNumber *num = [NSNumber numberWithDouble:[[YaxisData objectAtIndex:index] doubleValue]];
return num;
}
num returns junk data such as -1 or 993494949494 but when I log the double value of number, it prints the correct value. I am not able to return this double value as the function signature requires only the NSNumber to be returned.
NSLog(#"Number: %f", [num doubleValue]);
I am stuck here and would really appreciate any help in this regard. Thanks!
Would this give a better result somehow?
NSString *aString = [YaxisData objectAtIndex:index];
NSLog(#"%#", aString);
double value = [aString doubleValue];
NSLog(#"%f", value);
NSNumber *number = [NSNumber numberWithDouble:value];
NSLog(#"%#", number);
If not, could you show the results of the NSLog()'s?
I know that in practice it seems the same code, yet one might not be so sure that -objectAtIndex: always returns a string. It might be a localization issue as well (commas and dots for decimal separator might get mixed up, this would definitely mess up your results. In case of a localization issue, check the following link:
How to convert an NSString into an NSNumber

How can I access the int values of an object in an NSMutableArray?

I try to access some values in a NSMutableArray I created, but I only get some numbers (address?) if I try to access them.
I was able to initialize an array and can add and change objects with
[myNSMutableArray addObject:[NSNumber numberWithInt:10]]
and
[myNSMutableArray replaceObjectAtIndex:0 withObject:[NSNumber numberWithInt:47]
I also can print the value at index [0] with
NSLog(#"%#", [myNSMutableArray objectAtIndex:0]);
and I get 47 as expected.
But how can I access the integer value of the object in the array so I can save it tomyIntValue?
int myIntValue = [[myNSMutableArray objectAtIndex:0] intValue];
Just have a look at the NSNumber class reference