gpt4 book ai didi

objective-c - 在自定义 NSError 上的类型为 'code' 的对象上找不到属性 'NSError *'

转载 作者:行者123 更新时间:2023-11-28 18:40:13 25 4
gpt4 key购买 nike

我需要另一双眼睛。

奇怪的是,我似乎无法访问自定义 NSError 的属性。我不断收到 EXC_BAD_ACCESS 错误。这是我的代码:

        if (response.isUnauthorized)
{
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:response.bodyAsString forKey:@"Error Message"];

NSError *unAuthorizedError = [NSError errorWithDomain:@"MyApp" code: [response statusCode] userInfo:userInfo];
[delegate dataControllerLoadFailed:unAuthorizedError];
[ErrorHandler logError:unAuthorizedError fromClassName:NSStringFromClass([self class]) fromSelectorName:NSStringFromSelector(_cmd) ];
}

这会调用:

-(void)dataControllerLoadFailed:(NSError *)error
{
NSString *message = [NSString stringWithFormat:@"Encountered an error: %@ - ", error.code];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MyApp"
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[activityIndicator stopAnimating];

}

我在 dataControllerLoadFailed 中创建消息 NSString 时收到 Bad Access 错误,无论是使用 error.code 还是错误对象上的任何其他成员...

所以失败了:

NSString *message = [NSString stringWithFormat:@"Encountered an error: %@ - ", error.code];

但奇怪的是,这成功了:

NSString *message = [NSString stringWithFormat:@"Encountered an error: %@ - ", error];

感谢所有为此付出时间的人!

最佳答案

code 是一个 NSInteger,它只是一个类型定义的 int。您需要使用 %d 而不是 %@

关于objective-c - 在自定义 NSError 上的类型为 'code' 的对象上找不到属性 'NSError *',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11888381/

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