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

foh*_*oho 1 objective-c nserror ios

如果出现问题我正试图设置错误信息,但是我知道了

This class is not key value coding-compliant for the key NSLocalizedDescription
Run Code Online (Sandbox Code Playgroud)

这是我使用的代码

-(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];
}
Run Code Online (Sandbox Code Playgroud)

zou*_*oul 8

你打电话给setValue:forKey:你应该打电话的地方setObject:forKey:

[details setObject:@"You don't have enough money" forKey:NSLocalizedDescriptionKey];
Run Code Online (Sandbox Code Playgroud)

此外,您必须在初始化程序中更改NSDictionaryNSMutableDictionary或设置值:

NSDictionary *details = [NSDictionary
    dictionaryWithObject:@"You don’t…"
    forKey:NSLocalizedDescriptionKey];
Run Code Online (Sandbox Code Playgroud)

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