Converting an int into an NSNumber, then saving to an array.

Discussion in 'iOS Programming' started by Jeremy1026, Aug 20, 2008.

  1. macrumors 68000

    Joined:
    Nov 3, 2007
    #1
    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:

    Code:
    (NSNumber *)numberWithInt:(scoreVar)100;
    
    Code:
    (scoreVar *)numberWithInt:(int)100;
    
    Code:
    (NSNumber *)numberWithInt:(int)100;
    
    No luck though :(
     
  2. macrumors 6502

    Joined:
    Apr 24, 2003
    #2
    Code:
    NSNumber *yourNumber = [NSNumber numberWithInt:your_int_variable];
    
    Should be what you're after. your_int_variable is the int variable.
     
  3. thread starter macrumors 68000

    Joined:
    Nov 3, 2007
    #3
    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:

    Code:
    		[newPerson setScoreName:scoreVarObj];
    
     
  4. macrumors 68040

    Joined:
    Apr 22, 2005
  5. thread starter macrumors 68000

    Joined:
    Nov 3, 2007
    #5
    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

    Code:
    [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.

    Code:
    @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
    
     
  6. macrumors 68040

    Joined:
    Apr 22, 2005
    #6
    Right, so scoreName is an NSString and you are trying to set it to an NSNumber.
     
  7. thread starter macrumors 68000

    Joined:
    Nov 3, 2007
    #7
    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

    Code:
    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
    
     
  8. macrumors 68040

    Joined:
    Apr 22, 2005
    #8
    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.
     

Share This Page