Problem with int in Objective C - iphone

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?

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);

Display NSNumber In Label

I am trying to display an NSNumber from an array with keys into an UILabel. Here is my current code: marblesNeeded.text = [[[records objectAtIndex:0] valueForKey: #"marblesneeded"] intValue];
I also get the error:
warning: Semantic Issue: Incompatible integer to pointer conversion assigning to 'NSString *' from 'int'
Thanks
You need to create an NSString to set the text of a UILabel.
marblesNeeded.text = [NSString stringWithFormat:#"%i",[[[records objectAtIndex:0] valueForKey: #"marblesneeded"] intValue]];
In the format %i denotes that you will be providing an integer value after the format.
Edit:
As some comments have noted NSNumber does have a stringValue, it does work but is not my personal preference because it gives you little control as to the format of the string. Consider this example.
NSNumber *number = [NSNumber numberWithFloat:3.25];
NSLog(#"%#",number.stringValue); // Will print 3.25
NSLog(#"%#",[NSString stringWithFormat:#"%i",number.intValue]); // Will print 3
Since the question envolved printing an intValue this more explicit format may be necessary.
marblesNeeded.text = [[[records objectAtIndex:0] valueForKey: #"marblesneeded"] stringValue];

Objective c Setting picker value from NSDictionary

Another, probably simple problem that, having tried for a good few hours now I'm pretty stumped on. Simply, I want to set the value of a picker from a NSDictionary, I don't mind! Every way I have tried pretty much gives me the warning Passing argument 1 of selectRow inComponent animated' makes integer from pointer witout a cast. Which makes sense, though I seem to be failing miserable at fixing it! Any help would be greatly appreciated! Snippet of code below...
NSArray *myValue = [parsedJson objectForKey:#"Details"];
NSEnumerator *myEnumerator = [myValue objectEnumerator];
NSDictionary* myItem;
int i = 0;
while (myItem = (NSDictionary*)[myEnumerator nextObject])
{
[myPickerView selectRow:[myItem objectForKey:#"Value"] inComponent:i animated:YES];
i++;
}
Assuming your value responds to intValue message (e.g. number is stored in NSNumber or NSString) then the following should work:
[myPickerView selectRow:[[myItem objectForKey:#"Value"] intValue]
inComponent:i animated:YES];
The selectRow parameter is expecting an integer, try this:
[myPickerView selectRow:[[myItem objectForKey:#"Value"] integerValue] inComponent:i animated:YES];

Convert NSMutableArray into integer

i am working on xml parsing and getting three item numbers from the server 1
2 3, i have taken the three numbers into nsmutable array and assign them in delegate value mutable array, now i want to pass one number at a time into another function to get response from the server, so someone please let me know that how to convert NSMutable array value into integer while parsing into another function.
do like this
for(int i=0;i<[yourArrayFromXmlParsing count];i++)
{
int a=[[yourArrayFromXmlParsing objectAtIndexPath:i] intValue];
[obj function:a];
}
If u have added each number into the array u can access them by using the index of the array.
[[myArray objectAtIndex:i] intValue];
Assuming inside your array, you have NSNumber objects:
for (NSNumber *aNumber in yourArrayFromXmlParsing) {
resultFromServer = [self fooMethod:[aNumber intValue]];
}

Convert NSString to Integer in objective - c

I have an array that stores each digit of a phone number as a string. I then pass the phone number digit string as an argument for objectAtIndex: method of NSArray like this: [myArray objectAtIndex: [myString intValue]]; The compiler says I need to cast the String but I am already doing that. What is wrong?
Update:
Here's my actual line of code:
NSMutableArray *tmp = [[NSMutableArray alloc] initWithArray:[charHolder objectAtIndex:[[phoneNumDigits objectAtIndex:i]intValue]]];
This is where the error is, phoneNumDigits is an array of each digit of the phone number, charHolder is the array holding the array of letters associated with each digit.
Your question is a little confusing to me. Here is how I understand what you want to do:
You have an NSArray named phoneNumDigits. This array contains a few NSString objects. Each string is something like #"1" or #"4" and represents a single digit of a phone number.
Now you want to convert each of those digit strings to int or NSInteger and want to store these integers in another array.
If I understood you correctly, here is my answer:
You cannot exactly do what you want, because you can't put a simple data type like an int or a float into an NSArray.
That's why there is the wrapper class NSNumber. You can package a simple int in an NSNumber and store this NSNumber in an NSArray.
So to get your string digits from the phoneNumDigits into the tmp array you could use this code:
for (NSString *digitAsString in phoneNumDigits)
{
NSNumber *digitAsNumber = [NSNumber numberWithInt:[digitAsString intValue]];
[tmp addObject:digitAsNumber];
}
To get the ints out of the tmp-NSArray you would use
int digit = [[tmp objectAtIndex:idx] intValue];
I hope this helps, but I'm not sure I understand what you want to do here. I could be completely missing the point. Maybe you could share some more code.
NSMutableArray *tmp = [[NSMutableArray alloc] initWithArray:[charHolder objectAtIndex:[[phoneNumDigits objectAtIndex:i]intValue]]];
objectAtIndex returns a generic object (id). It has no idea that the object in the array is a string as it could be anything. So you need to cast it. Or to increase readabilty, create variables for them. Eg:
int phoneNumberDigit = [phoneNumDigits objectAtIndex:i];
NSArray *chars = [charHolder objectAtIndex:phoneNumberDigit];
NSMutableArray *tmp = [[NSMutableArray alloc] initWithArray:chars];