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.
Related
I try to convert NSString to int,Result dpPoint:0, I want dpPoint:2
dpPointStr = [NSString stringWithFormat:#"%#",[verifyRow valueForKey:#"default_point"]];
NSLog(#"dpPointStr:%#",dpPointStr); //Result dpPointStr:2
int dpPoint = [dpPointStr intValue];
NSLog(#"dpPoint:%i",dpPoint); //Result dpPoint:0
In your case, if value is in the beginning/end of the string you can try this:
int val = [[dpPointStr stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] intValue];
int str_len = [yourStr length]; char tmp[str_len];
[NSString getCString:tmp maxLength:str_len encoding:UTF8Encoding];
int dpPoint = atoi(tmp);
Don't forget to include this: #include
I think this should work just fine.
Also, I think you can fix this by taking the intValue from the:
[verifyRow valueForKey:#"default_point"];
To be consistent with Apple use NSInteger instead, or even better NSNumber for the valueForKey statement and then NSInteger.
Convert NSString to integer
NSString *sampleText = #"5";
int numValue = [sampleText intValue];
NSLog(#"Integer Value: %d", numValue);
In my application I have a value like "25:30" in NSString and I want to assign this value to int value.
If I do like:
int j = [stringval intValue];
hence I got the value "25" to my int value but I want the full value.
Is it possible?
If you mean 25.30 then you need floats, use CGFloat j = [stringval floatValue];
If that is supposed to be minutes & seconds, use NSDateFormatter's dateFromString:.
You can try this
NSString *new = #"25:30";
NSArry *data = [new componentsSeparatedByString:#":"];
int first = [[data objectAtIndex:0] intValue]; \\\ 25
int second = [[data objectAtIndex:1] intValue]; \\\ 30
singelton.categoryId = (int)[categories.categoriesId objectAtIndex:indexPath.row];
NSLog(#"%d", singelton.categoryId);
singelton.categoryId is from type int.
But when I try to print it number is random, but if I do this NSLog(#"%#", singelton.categoryId); the printed value is right.
I need to print it with %d.
Can someone help me?
Use intValue method to get the integer representation of a NSString/NSNumber. Try,
singelton.categoryId = [[categories.categoriesId objectAtIndex:indexPath.row] intValue];
I am assuming the array returns you an NSNumber. So try this out.
int catId = [ ((NSNumber*) [categories.categoriesId objectAtIndex:indexPath.row] ) intValue];
NSLog(#"%d, catId )
try the following:
NSLog(#"%d", [singelton.categoryId intValue]);
The category ID as returned by objectAtIndex: is an object, most probably an NSNumber. By casting it to int you get the address of the object, not the numeric value stored in it. The correct code would be something like this:
singleton.categoryID = [[… objectAtIndex:…] intValue];
Try do do this in this way:
singelton.categoryId = [[categories.categoriesId objectAtIndex:indexPath.row] intValue]
It's because you can't store ints in array - they must be NSNumbers.
There's no way that's coming from an int primitive type when it's a proper object (assuming that objectAtIndex behaves here as elsewhere in Objective-C--i.e., it's not your own method. So, you'll need to ask for the intValue:
[[categories.categoriesId objectAtIndex:indexPath.row] intValue]
if it is an available method. What class is categoriesID?
I want to convert string data to NSInteger.
If the string is a human readable representation of a number, you can do this:
NSInteger myInt = [myString intValue];
[myString intValue] returns a cType "int"
[myString integerValue] returns a NSInteger.
In most cases I do find these simple functions by looking at apples class references, quickest way to get there is click [option] button and double-click on the class declarations (in this case NSString ).
I've found this to be the proper answer.
NSInteger myInt = [someString integerValue];
NSNumber *tempVal2=[[[NSNumberFormatter alloc] init] numberFromString:#"your text here"];
returns NULL if string or returns NSNumber
NSInteger intValue=[tempVal2 integerValue];
returns integer of NSNumber
this is safer than integerValue:
-(NSInteger)integerFromString:(NSString *)string
{
NSNumberFormatter *formatter=[[NSNumberFormatter alloc]init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *numberObj = [formatter numberFromString:string];
return [numberObj integerValue];
}
int myInt = [myString intValue];
NSLog(#"Display Int Value:%i",myInt);
NSString *string = [NSString stringWithFormat:#"%d", theinteger];
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.