1

如果出现问题,我正在尝试设置错误消息,但我得到了

This class is not key value coding-compliant for the key NSLocalizedDescription

这是我使用的代码

-(id)getMoneyFromAccount:(int) sum error:(NSError **)error
{
    if(self.balance - sum < 0)
    {
        NSDictionary *details = [NSDictionary dictionary];
        [details setValue:@"You don't have enough money" forKey:NSLocalizedDescriptionKey];
        *error = [NSError errorWithDomain:@"money" code:200 userInfo:details];
        return nil;
    }
    self.balance = self.balance - sum;
    return  [NSNumber numberWithInt:self.balance];
}
4

4 回答 4

8

您正在呼叫setValue:forKey:您应该呼叫的地方setObject:forKey:

[details setObject:@"You don't have enough money" forKey:NSLocalizedDescriptionKey];

此外,您必须从 to 更改NSDictionaryNSMutableDictionary设置初始化程序中的值:

NSDictionary *details = [NSDictionary
    dictionaryWithObject:@"You don’t…"
    forKey:NSLocalizedDescriptionKey];

可以使用setValue:forKey:可变字典,但最好setObject:forKey:直接调用。

于 2012-03-06T10:31:47.187 回答
1

当您需要更改已初始化的 NSError 的本地化描述时,您可以覆盖 NSError 类,如下所示:

@interface ValidationError : NSError

@property (nonatomic, strong) NSString *localizedDescription;
@end
于 2013-07-30T15:08:50.273 回答
0

不, setValue: forKey: 是正确的使用方法。也许这是因为您需要创建 NSMutableDictionary 而不是 NSDictionary。

于 2012-03-06T10:39:56.960 回答
0

您需要使用 NSMutableDictionary 或初始化程序来设置值。NSDictionary 初始化后就无法更改。

NSDictionary* details = [NSDictionary dictionaryWithObject: @"..." 
                                                    forKey: NSLocalizedDescriptionKey];
于 2012-03-06T10:51:31.967 回答