Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

AbhishekApple

macrumors member
Original poster
Aug 5, 2010
86
0
i have declared int variable totRecint as global but it is not getting assigned with value
(showing garbage memory location)????

but the string variable totRecstr gets a value from the array

Code:
@implementation SearchViewController

NSString *totRecstr;
int totRecint;

- (void)requestFinished:(ASIHTTPRequest *)request
{
NSArray *arryPage = [[request responseString] componentsSeparatedByString:@","];


		totRecint = (int)[arryPage objectAtIndex:0];
		
		totRecstr = [arryPage objectAtIndex:0];
}
......
@end
 
Code:
totRecint = (int)[arryPage objectAtIndex:0];

You can't do that. ints are primitives, not objects and therefore cannot be stored inside an NSArray without a wrapper (ie: NSNumber). What you are doing here is setting totRecint to the value of the pointer at index 0. You need to convert the returned value to an int first before you can perform the assignment. Hint: look at the "Getting Numeric Values" section in the NSString documentation.

Edit: Mixed up NSInteger and NSNumber, need more coffee. Thanks ranguvar
 
ok.

Code:
[arryPage objectAtIndex:0];

returns you a pointer to an NSObject. if you cast this pointer to int you will get the memory address the pointer points to written into the int variable. that's why you see "garbage" values in your int variable;

do this instead:

Code:
totRecint = [[arryPage objectAtIndex:0] intValue];
 
Assuming your variable is Counter:

1.) in your .h file put the following

int counter;

2.) in the .m file put the following code in the viewdidload IBACTION

in counter = 0;
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.