Pregunta

I have a custom NSFormatter linked to 4 NSTextFields. When I change the values of my text fields manually, everything works fine. But when I change it through a combo box. I get an error looking like this:

-[__NSCFNumber length]: unrecognized selector sent to instance 0xc0c3

I've noticed that the application continuously sends this error and that the instance is always the same (0xc0c3). Also, when my NSTextFields aren't linked to my custom formatter, everything works well, even through the combo box.

Do you guys know what may be the source of the problem?

Thanks in advance!

Here's some code:

Combo box action: - (void)subnetMaskByNumberOfSubnetBits:(id)sender{

// ------- Sets the subnet mask when the user selects the number of bits

NSNumberFormatter *stringToNumber = [[NSNumberFormatter alloc] init];//TURN A STRING INTO A NUMBER
NSNumber *selectedAmountOfBits = [[NSNumber alloc] init];//CONTAINS THE SELECTED NUMBER OF BITS

selectedAmountOfBits = [stringToNumber numberFromString:[sender objectValueOfSelectedItem]];

[self changeSubnetMaskUsingNumberOfMaskBits:selectedAmountOfBits];

//RELEASE
[stringToNumber release];

}

-(void)changeSubnetMaskUsingNumberOfMaskBits:(NSNumber *)numberOfMaskBitsSelected{

// --------- Change the subnet mask based on the number of bits


NSInteger numberOfFullOctets;
NSInteger valueOfLastOctet;
NSInteger octetCounter;
NSMutableDictionary *subnetMaskFields = [[NSMutableDictionary alloc] init];



//Contains keys to all the outlets
[subnetMaskFields setObject:subnetMaskOctet1 forKey:@"subnetMaskField1"];
[subnetMaskFields setObject:subnetMaskOctet2 forKey:@"subnetMaskField2"];
[subnetMaskFields setObject:subnetMaskOctet3 forKey:@"subnetMaskField3"];
[subnetMaskFields setObject:subnetMaskOctet4 forKey:@"subnetMaskField4"];


//NUMBER OF FULL OCTETS AND VALUE OF LAST OCTET
numberOfFullOctets = [numberOfMaskBitsSelected intValue]/8;

valueOfLastOctet = 256 - pow(2, 8 - ([numberOfMaskBitsSelected intValue] - (8 * ([numberOfMaskBitsSelected intValue]/8)))); //Big complicated formula


//-------Setting the fields------//

//SETTING THE FIELDS OF FULL OCTETS
for (octetCounter = 1; octetCounter <= numberOfFullOctets; octetCounter++) { 
    [[subnetMaskFields objectForKey:[NSString stringWithFormat:@"subnetMaskField%i", octetCounter]] setStringValue:@"255"];
}

//SETTING THE FIELD OF THE INCOMPLETE OCTET
[[subnetMaskFields objectForKey:[NSString stringWithFormat:@"subnetMaskField%i", octetCounter]] setIntegerValue:valueOfLastOctet];


//FILLING THE ZER0S
while (octetCounter < 4) {

    octetCounter++;
    [[subnetMaskFields objectForKey:[NSString stringWithFormat:@"subnetMaskField%i", octetCounter]] setStringValue:@"0"];


}

//RELEASE
[subnetMaskFields release];

}

¿Fue útil?

Solución

The problem is this line of code:

[[subnetMaskFields objectForKey:[NSString stringWithFormat:@"subnetMaskField%i", octetCounter]] setIntegerValue:valueOfLastOctet];

From what I understand, since the NSFormatter needs to get the string value of my text field, I cannot set the text field as an integer. This line of code

[[subnetMaskFields objectForKey:[NSString stringWithFormat:@"subnetMaskField%i", octetCounter]] setStringValue:[NSString stringWithFormat:@"%ld", valueOfLastOctet]]; 

solves the problem.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top