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)
你打电话给setValue:forKey:你应该打电话的地方setObject:forKey:
[details setObject:@"You don't have enough money" forKey:NSLocalizedDescriptionKey];
Run Code Online (Sandbox Code Playgroud)
此外,您必须在初始化程序中更改NSDictionary为NSMutableDictionary或设置值:
NSDictionary *details = [NSDictionary
dictionaryWithObject:@"You don’t…"
forKey:NSLocalizedDescriptionKey];
Run Code Online (Sandbox Code Playgroud)
这setValue:forKey:将使用可变字典,但最好setObject:forKey:直接调用.