PDA

View Full Version : Converting an int into an NSNumber, then saving to an array.




Jeremy1026
Aug 20, 2008, 04:23 PM
I have everything set up to save to the array, but since NS(Mutable)Array's cant store int's, I have to convert my int to an NSNumber, this is where I am getting stuck. I can't figure out how to convert the int.

I tried:


(NSNumber *)numberWithInt:(scoreVar)100;


(scoreVar *)numberWithInt:(int)100;


(NSNumber *)numberWithInt:(int)100;


No luck though :(



neoserver
Aug 20, 2008, 04:37 PM
NSNumber *yourNumber = [NSNumber numberWithInt:your_int_variable];


Should be what you're after. your_int_variable is the int variable.

Jeremy1026
Aug 20, 2008, 04:56 PM
NSNumber *yourNumber = [NSNumber numberWithInt:your_int_variable];


Should be what you're after. your_int_variable is the int variable.

When I do that, I get the following error when attempting to save it to the array:

"warning: passing argument 1 of 'setScoreName:' from distinct Objective-C type"

Here is my code for setting the array value:


[newPerson setScoreName:scoreVarObj];

admanimal
Aug 20, 2008, 05:11 PM
What exactly are newPerson and scoreVarObj?

Jeremy1026
Aug 20, 2008, 05:34 PM
What exactly are newPerson and scoreVarObj?

newPerson where in the array my objects are stored. scoreVarObj is the NSNumber created by the int.

This is the full code of where I save the data


[newPerson setFirstName:@"Jeremy"]; NSLog(@"%@", newPerson.firstName);
[newPerson setScoreName:scoreVarObj]; NSLog(@"%@", newPerson.scoreName);
[personArray insertObject:newPerson atIndex:[personArray count]];


I have the encoding/decoding in its own NSObject File. Here is the implimentation of that file.


@implementation score
@synthesize firstName, scoreName;

- (id) init {
if(self = [super init]) {
firstName = [[NSString alloc] init];
scoreName = [[NSString alloc] init];
}

return self;
}

- (void) dealloc {
[firstName release];
[scoreName release];
[super dealloc];
}

- (id) initWithCoder:(NSCoder *)coder {
firstName = [[coder decodeObjectForKey:@"firstName"] retain];
scoreName = [[coder decodeObjectForKey:@"scoreName"] retain];
return self;
}


- (void) encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:firstName forKey:@"firstName"];
[encoder encodeObject:scoreName forKey:@"scoreName"];
}


@end

admanimal
Aug 20, 2008, 07:47 PM
Right, so scoreName is an NSString and you are trying to set it to an NSNumber.

Jeremy1026
Aug 20, 2008, 08:47 PM
Ok, I fixed it so that everytime scoreName is mentioned, its mentioned as an NSNumber. But I get an error when I try to add the NSNumbers to the array. The error from the debugger is


2008-08-20 21:39:18.951 iDice[35026:20b] *** -[NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x465520
2008-08-20 21:39:18.951 iDice[35026:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x465520'
2008-08-20 21:39:18.952 iDice[35026:20b] Stack: (
2461323595,
2499793147,
2461352778,
2461346124,
2461346322,
817180501,
11576,
818069166,
818076920,
816177049,
816209730,
2472167774,
2460826437,
2460826872,
829005112,
829005309,
816175835,
816221412,
8504,
8358

admanimal
Aug 20, 2008, 08:56 PM
The error is exactly what it is telling you- NSNumber does not have a function called isEqualToString, but you are trying to call isEqualToString on some NSNumber somewhere.