setNilValueForKey 错误
我有四个绑定到模型关键路径的文本字段。如果在文本字段中输入数字,一切都会按计划进行。但是,如果该数字被删除,那么我会在控制台中收到错误消息:
[Temperature 0x1003144b0 setNilValueForKey]: Could not set nil as the value for the keyrankine
我尝试使用 setNilValueForKey 修复此问题,但它似乎不起作用(请参阅Temperature.h 文件底部的代码)。任何有关如何解决此问题的建议都会有所帮助。
#import <Foundation/Foundation.h>
@interface Temperature : NSObject {
double celsius;
}
- (void)setCelsius:(double)degreesC;
- (double)celsius;
- (void)setKelvin:(double)degreesK;
- (double)kelvin;
- (void)setFahrenheit:(double)degreesF;
- (double)fahrenheit;
- (void)setRankine:(double)degreesR;
- (double)rankine;
@end
并
#import "Temperature.h"
@implementation Temperature
+ (NSSet *)keyPathsForValuesAffectingFahrenheit {
return [NSSet setWithObject:@"celsius"];
}
+ (NSSet *)keyPathsForValuesAffectingKelvin {
return [NSSet setWithObject:@"celsius"];
}
+ (NSSet *)keyPathsForValuesAffectingRankine {
return [NSSet setWithObject:@"celsius"];
}
- (void)setCelsius:(double)degreesC {
celsius = degreesC;
}
- (double)celsius {
return celsius;
}
- (void)setKelvin:(double)degreesK {
[self setCelsius:degreesK - 273.15];
}
- (double)kelvin {
return [self celsius] + 273.15;
}
- (void)setFahrenheit:(double)degreesF {
[self setCelsius:(degreesF - 32) / 1.8];
}
- (double)fahrenheit {
return [self celsius] * 1.8 + 32;
}
- (void)setRankine:(double)degreesR {
[self setCelsius:(degreesR - 491.67) * 5/9];
}
- (double)rankine {
return ([self celsius] + 273.15) * 9/5;
}
- (void)setNilValueForKey:(NSString *)rankine {
[super setNilValueForKey:rankine];
}
@end
...根据评论回答...
- (void)setNilValueForKey:(NSString*)key {
if ([key isEqualToString:@"celsius"]) return [self setCelsius: 0];
if ([key isEqualToString:@"fahrenheit"]) return [self setFahrenheit: 0];
if ([key isEqualToString:@"kelvin"]) return [self setKelvin: 0];
if ([key isEqualToString:@"rankine"]) return [self setRankine: 0];
[super setNilValueForKey:key];
}
I have four textfields that bind to the model key path. If a number is typed into the textfield, everything works as planned. However, if the number is deleted then I get an error in the console with:
[Temperature 0x1003144b0 setNilValueForKey]: could not set nil as the value for the key rankine
I tried to fix this using setNilValueForKey but it doesn't seem to work (see code at bottom of Temperature.h file). Any suggestions on how to fix this would be helpful.
#import <Foundation/Foundation.h>
@interface Temperature : NSObject {
double celsius;
}
- (void)setCelsius:(double)degreesC;
- (double)celsius;
- (void)setKelvin:(double)degreesK;
- (double)kelvin;
- (void)setFahrenheit:(double)degreesF;
- (double)fahrenheit;
- (void)setRankine:(double)degreesR;
- (double)rankine;
@end
and
#import "Temperature.h"
@implementation Temperature
+ (NSSet *)keyPathsForValuesAffectingFahrenheit {
return [NSSet setWithObject:@"celsius"];
}
+ (NSSet *)keyPathsForValuesAffectingKelvin {
return [NSSet setWithObject:@"celsius"];
}
+ (NSSet *)keyPathsForValuesAffectingRankine {
return [NSSet setWithObject:@"celsius"];
}
- (void)setCelsius:(double)degreesC {
celsius = degreesC;
}
- (double)celsius {
return celsius;
}
- (void)setKelvin:(double)degreesK {
[self setCelsius:degreesK - 273.15];
}
- (double)kelvin {
return [self celsius] + 273.15;
}
- (void)setFahrenheit:(double)degreesF {
[self setCelsius:(degreesF - 32) / 1.8];
}
- (double)fahrenheit {
return [self celsius] * 1.8 + 32;
}
- (void)setRankine:(double)degreesR {
[self setCelsius:(degreesR - 491.67) * 5/9];
}
- (double)rankine {
return ([self celsius] + 273.15) * 9/5;
}
- (void)setNilValueForKey:(NSString *)rankine {
[super setNilValueForKey:rankine];
}
@end
...answer based on comments...
- (void)setNilValueForKey:(NSString*)key {
if ([key isEqualToString:@"celsius"]) return [self setCelsius: 0];
if ([key isEqualToString:@"fahrenheit"]) return [self setFahrenheit: 0];
if ([key isEqualToString:@"kelvin"]) return [self setKelvin: 0];
if ([key isEqualToString:@"rankine"]) return [self setRankine: 0];
[super setNilValueForKey:key];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您对 -setNilValueForKey: 的重写不会完成任何事情,因为它只是调用 super (旨在引发异常)。您实际上需要正确处理 nil 值。具体如何处理它取决于您,但类似这样的事情可能是合理的:
您可能还想处理类中其他键(华氏度、开尔文和摄氏度)的 nil 值。
Your override of -setNilValueForKey: doesn't accomplish anything because it just calls super (which is designed to throw an exception). You need to actually properly handle the nil value. Exactly how you handle it is up to you, but something like this might be reasonable:
You probably also want to handle nil values for the other keys in your class (Fahrenheit, Kelvin, and Celsius).