gpt4 book ai didi

objective-c - 自定义格式化程序奇怪的行为

转载 作者:行者123 更新时间:2023-12-03 17:24:46 25 4
gpt4 key购买 nike

我有一个链接到 4 个 NSTextFields 的自定义 NSFormatter。当我手动更改文本字段的值时,一切正常。但是当我通过组合框更改它时。我收到如下错误:

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

我注意到应用程序不断发送此错误,并且实例始终相同 (0xc0c3)。另外,当我的 NSTextFields 未链接到我的自定义格式化程序时,即使通过组合框,一切也能正常工作。

你们知道问题的根源是什么吗?

提前致谢!

这是一些代码:

组合框操作:- (void)subnetMaskByNumberOfSubnetBits:(id)发送者{

// ------- 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];

}

最佳答案

问题出在这行代码:

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

据我了解,由于 NSFormatter 需要获取文本字段的字符串值,因此我无法将文本字段设置为整数。这行代码

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

解决了问题。

关于objective-c - 自定义格式化程序奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9957275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com