Convert NSString value to int - objective-c

That may sound odd.
I got an NSString value NSString * numb = [self.dataDict valueForKey:#"id"]; and i know, that is it some kind of integer (for example, i need that integer to comparison - if val less or equal then something). I need to know what integer is it.
What i've tried:
NSNumber *numba = [self.dataDict valueForKey:#"id"];
NSLog output - numba is 2038735264
And actually that was 428.
is there any way to achieve the point? Thanks!
That is piece of responseObject:
(
{
id = 3;
dog = "\U041a\U0430\U043a\U043e\U0439-\U0442\U043e \U043c\U0443\U0434\U0430\U043a \U043d\U0430\U043a\U0440\U0443\U0442\U0438\U043b";
image = "cute_dog/116.jpg";
score = 586;
},
{
id = 115;
dog = "\U0422\U0430\U043d\U044f \U041a\U043b\U044e\U043a\U0432\U0438\U043d\U0430";
image = "cute_dog/115.jpg";
score = 481;
},

There are a number of methods you can use to convert an NSString to a number. What numeric type would you like?
NSString *string = self.dataDict[#"id"];
int intValue = string.intValue;
NSInteger integerValue = string.integerValue;
long long longLongValue = string.longLongValue;

Trying something like this.
NSNumber *numba = [NSNumber numberWithInt[self.dataDict valueForKey:#"id"]];
//For string
NSString *stringValue = [numba stringValue];
//For integer
NSInteger integer = [numba integerValue];

Related

Converting decimal number to binary Objective-C

Hi I have made an IOS app that converts binary, hexadecimal and decimal values. It all works fine except for my decimal to binary conversion. Here is what I have. It returns 0s and 1s but far too many. Can anyone tell me why this is or help me with a better method?
NSString *newDec = [display text]; //takes user input from display
NSString *string = #"";
NSUInteger x = newDec;
int i = 0;
while (x > 0) {
string = [[NSString stringWithFormat:#"%u", x&1] stringByAppendingString:string];
x = x>> 1;
++i;
}
display.text = string; //Displays result in ios text box
Try this:
NSUInteger x = [newDec integerValue];
And next time don't ignore the Compiler's "Incompatible pointer to Integer conversion" hint...
Explanation: Afaik, assigning an object to an int, actually assigns the address of the object to that integer, not the content of the string (which is what you want).

Objective-C: Improve function

I´m new to developing apps and I would like to have some hints about the code I have here:
- (IBAction)button_increase_click:(id)sender {
int number = [self.label_content.text intValue];
number+=1;
NSString *increased_value = [NSString stringWithFormat:#"%d",number];
int count = [increased_value length];
while (count<4) {
increased_value = [NSString stringWithFormat:#"%#%#", #"0",increased_value];
count = [increased_value length];
}
self.label_content.text = increased_value;
}
What I need to do is to increase the value of "label_content" by 1 and fill it with leading zeros until it has reached 4 digits. eg "0001" "0013" "0132".
So how can I improve the above code and take care of its readability?
Thank you for helping me out.
The method can look like this:
- (IBAction)button_increase_click:(id)sender {
int number = [self.label_content.text intValue];
number++;
self.label_content.text = [NSString stringWithFormat:#"%04d", number];
}
Update For increasing readability use camel case for ivar method and other names. It's standard for iOS.
- (IBAction)increaseValue:(id)sender {
int number = [self.contentLabel.text intValue];
number++;
self.contentLabel.text = [NSString stringWithFormat:#"%04d", number];
}
See Apple's String Format Specifiers Documentation.
A better number formatter:
int number = 4;
[NSString stringWithFormat:#"%04d",number];
NSLog(#"number: %04d", number);
NSLog output:
number: 0004

Correct format for number (CoreData)?

In CoreData I set type:
Integer 16
and inserted int like this:
[NSNumber numberWithInt:9]
and problem is when I get result from CoreData result is not 9, result is: 114726928
Does andbody know's why?
try the following:
NSInteger number = 9;
NSNumber *numberObject = [NSNumber numberWithInteger:number];
NSInteger numberFromNSNumber = [numberObject integerValue]; // it will contains the 9
it should be fine.

NSString intValue deforming actual number

I was making a basic method that takes a Flickr image URL and returns the image's ID.
I'm passing the method the NSString #"http://farm6.staticflickr.com/5183/5629026092_c6762a118f".
The goal is to return the int: 5629026092, which is in the image's URL and is the image's ID.
Here is my method:
-(int)getImageIDFromFlickrURL:(NSString *)imageURL{
NSArray *objectsInURLArray = [imageURL componentsSeparatedByString:#"/"];
NSString *lastObjectInFlickrArray = [objectsInURLArray lastObject];
NSArray *dirtyFlickrIdArray = [lastObjectInFlickrArray componentsSeparatedByString:#"_"];
NSString *flickIDString = [dirtyFlickrIdArray objectAtIndex:0];
NSLog(#"flickr id string: %#",flickIDString);
int flickrID = [flickIDString intValue];
NSLog(#"id: %i",flickrID);
return flickrID;
}
The output in the console is:
2012-05-26 13:30:25.771 TestApp[1744:f803] flickr id string: 5629026092
2012-05-26 13:30:25.773 TestApp[1744:f803] id: 2147483647
Why is calling intValue deforming the actual number?
Use long long instead, your number is greater than int can handle (max being 2147483647 as you can see in your second log)
Your value is too big to represent in 32 bits. The biggest value you can store in a signed 32 bit integer (int) is 2147483647. For unsigned ints, it's 4294967295. You need to convert to a long long integer to represent a number as big as 5629026092.
You'll probably need to create a number formatter for that. I'm no expert on number formatters, and always have to dig out the documentation to figure out how to use them.
I just tried it, and this code works:
NSString *numberString = #"5629026092";
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
NSNumber *number = [formatter numberFromString: numberString];
long long value = [number longLongValue];
NSLog(#"%# = %qi", numberString, value);
[formatter release];
You could also convert the string to a C string and use scanf, come to think of it.
Easy ^^: INT_MAX Maximum value for a variable of type int. 2147483647
I found this to be a convenient way to do it:
NSString *flickIDString = [dirtyFlickrIdArray objectAtIndex:0]; // read some huge number into a string
// read into a NSNumber object or a long long variable. you choose
NSNumber *flickIDNumber = flickIDString.longLongValue;
long long flickIDLong = flickIDString.longLongValue;

Get a long value from an NSString

I need to get a long value from an NSString.
For example, I have this:
NSString *test = #"5437128";
and I need :
long myLong = 5437128;
How I can get this long value from the NSString?
Thank you, it will help me!
long longVariable = [test longLongValue];
See NSString documentation..
Use the NSScanner like in the following code:
unsigned x;
[[NSScanner scannerWithString: s2] scanHexInt: &x];
when typing the scanHexInt stop at scan and see yourself which one you need - there are many possibilities to get values from strings....
You might want to use scanlonglong... having declared your variable as long
use this code :
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
long number = [[numberFormatter numberFromString:string] longValue];
[numberFormatter release];
i use the following in case you don't want to lose the fractions value of your number
double x =[stringValue doubleValue];
instead of
double x = [stringValue longLongValue];
if we assume that we have the following string value
and we want to convert it to double we have 2 ways
NSString * stringValue = #"31.211529225111";
way #1
double x = [stringValue longLongValue];
result will be : x = 31
way #2
double x =[stringValue doubleValue];
result will be : x = 31.211529225111001