gpt4 book ai didi

objective-c - 尝试为NSError设置本地化描述时代码崩溃

转载 作者:行者123 更新时间:2023-12-01 17:23:58 25 4
gpt4 key购买 nike

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

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

最佳答案

您在应调用setValue:forKey:的位置调用setObject:forKey:

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

另外,您必须从 NSDictionary更改为 NSMutableDictionary或在初始化程序中设置值:
NSDictionary *details = [NSDictionary
dictionaryWithObject:@"You don’t…"
forKey:NSLocalizedDescriptionKey];
setValue:forKey:可以与可变字典配合使用,但是最好直接调用 setObject:forKey:

关于objective-c - 尝试为NSError设置本地化描述时代码崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9581801/

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