setNilValueForKey 错误

发布于 2025-01-01 07:33:54 字数 2088 浏览 2 评论 0原文

我有四个绑定到模型关键路径的文本字段。如果在文本字段中输入数字,一切都会按计划进行。但是,如果该数字被删除,那么我会在控制台中收到错误消息:

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你与昨日 2025-01-08 07:33:54

您对 -setNilValueForKey: 的重写不会完成任何事情,因为它只是调用 super (旨在引发异常)。您实际上需要正确处理 nil 值。具体如何处理它取决于您,但类似这样的事情可能是合理的:

- (void)setNilValueForKey:(NSString*)key 
{
    if ([key isEqualToString:@"rankine"])
    {
        self.rankine = 0;
        return;
    }

    [super setNilValueForKey:key];
}

您可能还想处理类中其他键(华氏度、开尔文和摄氏度)的 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:

- (void)setNilValueForKey:(NSString*)key 
{
    if ([key isEqualToString:@"rankine"])
    {
        self.rankine = 0;
        return;
    }

    [super setNilValueForKey:key];
}

You probably also want to handle nil values for the other keys in your class (Fahrenheit, Kelvin, and Celsius).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文