我需要另一双眼睛。

奇怪的是,我似乎无法访问自定义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时遇到错误访问错误,无论是使用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的类型为'NSError *'的对象上找不到属性'code',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11888381/

10-13 04:00