我收到一个参数的错误,该参数不能为 nil obj c

I get an error of a parameter that cannot be nil obj c

提问人:Stefano 提问时间:2/18/2020 最后编辑:Stefano 更新时间:2/18/2020 访问量:322

问:

此函数是另一个相同但具有不同键的副本。另一方面,它有效,我不明白为什么应用程序在这里崩溃..

    #pragma mark - ChangePin
- (void) doChangePin: (NSString *) pinUser card: (NSString *) pinCard viewController: (UIViewController *) viewController success:(successSDKType)success failure:(failureSDKType)failure{

    self.mcPersonalEnrollmentRequest.mcCardPan = pinCard;

    PRLog(@"Pin Inserito: %@", pinUser);

    SecurityCryptogram *securityCryptogram = [CryptogramManager getSecurityCriptogramWithPan:pinCard pin:pinUser];

    self.mcPersonalEnrollmentRequest.securityCryptogram = securityCryptogram;
    self.mcPersonalEnrollmentRequest.mcIdPublicKey = [[ServiceDataManager getInstance] getIdPublicKey];

    NSDictionary* dtoReq = [self.mcPersonalEnrollmentRequest getDictionary];

    [self.netManager doRequestWithCommand:CMD_STATUS_CHECK dtoReq:dtoReq successCompletion:^(NetworkResponse *successData) {

        NSDictionary *resources = (NSDictionary *)[successData res];

        McCardEnrolled *mcCardEnrolled = [[McCardEnrolled alloc] initWithResDict:resources];
        [[ServiceDataManager getInstance] storeMcCard:mcCardEnrolled];

        McCard *mcCard = [mcCardEnrolled getCard];

        //We have to go on main thread to pop the controller
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.mcCardEnrollmentSuccessCallback(mcCard);
            self.cardEnrollmentViewController.navigationController.navigationBar.translucent = NO;
            [self.cardEnrollmentViewController.navigationController popViewControllerAnimated:YES];

            self.cardEnrollmentViewController = nil;
            self.mcCardEnrollmentSuccessCallback = nil;
            self.mcCardEnrollmentFailureCallback = nil;
            self.mcCardEnrollmentRequest = nil;

        }];


    } failureCompletion:^(Result *failureResult) {

        //Same of success case. We have to go on main thread to pop the controller
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.mcCardEnrollmentFailureCallback(failureResult);
            self.cardEnrollmentViewController.navigationController.navigationBar.translucent = NO;
            [self.cardEnrollmentViewController.navigationController popViewControllerAnimated:YES];

            self.cardEnrollmentViewController = nil;
            self.mcCardEnrollmentSuccessCallback = nil;
            self.mcCardEnrollmentFailureCallback = nil;
            self.mcCardEnrollmentRequest = nil;
        }];

    }];

错误是:

由于未捕获的异常“NSInvalidArgumentException”而终止应用,原因:“*** -[__NSDictionaryM setObject:forKey:]:对象不能为零(键:req)”

更改的对象或变量如下所示: mcPersonalEnrollmentRequest CMD_STATUS_CHECK 我在这里为这个函数重新创建。

- (void) doRequestWithCommand:(NSString*)cmd dtoReq:(NSDictionary*)dtoReq successCompletion:(successCompletionType)success failureCompletion:(failureCompletionType)failure{

PRLog(@"NET-MAN> CMD:%@ - Prepare request with dtoReq: %@",cmd,[dtoReq description]);

NetworkRequest *request = [self prepareRequestForCommand:cmd dtoReq:dtoReq];
PRLog(@"NET-REQ> CMD:%@ - [DEBUG] Request created", cmd);

NSURLSessionConfiguration *defaultConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultConfig delegate:self delegateQueue:nil];

NSURLSessionDataTask* task = [defaultSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    PRLog(@"NET-MAN> CMD:%@ - Received response. Error: %@", cmd, [error description]);

    if (!error) {
        // Success
        NSString *errMsg;
        if ([response isKindOfClass:[NSHTTPURLResponse class]]) {

            NSDictionary *jsonResponse = [PRUtility dictionaryFromJsonData:data];
            if(jsonResponse){
                PRLog(@"NET-MAN> CMD:%@ - JSON RESPONSE != nil", cmd);

                NSDictionary* svcResDict = (NSDictionary*)[jsonResponse objectForKey:DTO_RESPONSE_TAG_SVC];
                if(svcResDict && svcResDict != (NSDictionary *)[NSNull null]){

                    PRLog(@"NET-MAN> CMD:%@ - Trying parsing response.",cmd);
                    NetworkResponse* networkResponse = [[NetworkResponse alloc] initWithCmd:cmd andNetworkData:svcResDict];

                    if (networkResponse) {
                        //Happy flow
                        PRLog(@"NET-MAN> CMD:%@ - Response created correctly.",cmd);

                        Result* resultForBadStatusCode = [self errorFromStatusCode:networkResponse.dtoStatus];

                        !resultForBadStatusCode ? success(networkResponse) : failure(resultForBadStatusCode);
                        return;
                    }
                    else{
                        errMsg = @"Error while accessing to networkResponse";
                    }
                }
                else{
                    errMsg = @"SVC field not found";
                }
            }
            else{
                errMsg = @"Cannot parses response into json";
            }
        }  else {
            errMsg = @"Recevied an error [response is not a NSHTTPURLResponse]";
        }

        Result *errorResponse = [[Result alloc] initGenericErrorWithMalformedNetworkResponse:errMsg];

        PRLog(@"NET-MAN: CMD:%@ - ERR: %@", cmd, errMsg);
        failure(errorResponse);

    } else {
        // Fail

        Result *errorNetwork = [self errorNetworkFromCode:error.code];

        PRLog(@"NET-MAN: CMD:%@ - ERR: %@", cmd, error.description);
        failure(errorNetwork);
    }

}];

PRLog(@"NET-MAN: CMD:%@ - Doing request...", request.cmd);
[task resume];

}

- (void) doRequestWithCommand:(NSString*)cmd dtoReq:(NSDictionary*)dtoReq successCompletion:(successCompletionType)success failureCompletion:(failureCompletionType)failure{

    PRLog(@"NET-MAN> CMD:%@ - Prepare request with dtoReq: %@",cmd,[dtoReq description]);

    NetworkRequest *request = [self prepareRequestForCommand:cmd dtoReq:dtoReq];
    PRLog(@"NET-REQ> CMD:%@ - [DEBUG] Request created", cmd);

    NSURLSessionConfiguration *defaultConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultConfig delegate:self delegateQueue:nil];

    NSURLSessionDataTask* task = [defaultSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        PRLog(@"NET-MAN> CMD:%@ - Received response. Error: %@", cmd, [error description]);

        if (!error) {
            // Success
            NSString *errMsg;
            if ([response isKindOfClass:[NSHTTPURLResponse class]]) {

                NSDictionary *jsonResponse = [PRUtility dictionaryFromJsonData:data];
                if(jsonResponse){
                    PRLog(@"NET-MAN> CMD:%@ - JSON RESPONSE != nil", cmd);

                    NSDictionary* svcResDict = (NSDictionary*)[jsonResponse objectForKey:DTO_RESPONSE_TAG_SVC];
                    if(svcResDict && svcResDict != (NSDictionary *)[NSNull null]){

                        PRLog(@"NET-MAN> CMD:%@ - Trying parsing response.",cmd);
                        NetworkResponse* networkResponse = [[NetworkResponse alloc] initWithCmd:cmd andNetworkData:svcResDict];

                        if (networkResponse) {
                            //Happy flow
                            PRLog(@"NET-MAN> CMD:%@ - Response created correctly.",cmd);

                            Result* resultForBadStatusCode = [self errorFromStatusCode:networkResponse.dtoStatus];

                            !resultForBadStatusCode ? success(networkResponse) : failure(resultForBadStatusCode);
                            return;
                        }
                        else{
                            errMsg = @"Error while accessing to networkResponse";
                        }
                    }
                    else{
                        errMsg = @"SVC field not found";
                    }
                }
                else{
                    errMsg = @"Cannot parses response into json";
                }
            }  else {
                errMsg = @"Recevied an error [response is not a NSHTTPURLResponse]";
            }

            Result *errorResponse = [[Result alloc] initGenericErrorWithMalformedNetworkResponse:errMsg];

            PRLog(@"NET-MAN: CMD:%@ - ERR: %@", cmd, errMsg);
            failure(errorResponse);

        } else {
            // Fail

            Result *errorNetwork = [self errorNetworkFromCode:error.code];

            PRLog(@"NET-MAN: CMD:%@ - ERR: %@", cmd, error.description);
            failure(errorNetwork);
        }

    }];

    PRLog(@"NET-MAN: CMD:%@ - Doing request...", request.cmd);
    [task resume];

}

    - (NetworkRequest*) prepareRequestForCommand:(NSString*)cmd dtoReq:(NSDictionary*)dtoReq{
    NSMutableDictionary* dtoAppRequest = [[NSMutableDictionary alloc] init];
    [dtoAppRequest setObject:cmd
                      forKey:CMD_TAG];
    [dtoAppRequest setObject:dtoReq forKey:REQ_TAG];

    NetworkRequest *request = [[NetworkRequest alloc] initWithDtoAppRequest:dtoAppRequest
                                                             jSessionClient:self.userConfig.jSessionClient
                                                                     userId:self.userConfig.userId
                                                                 fiscalCode:self.userConfig.fiscalCode];

    [request completeRequestObject];

    return request;
}
ios objective-c xcode 函数 null

评论

0赞 Asperi 2/18/2020
我在这里没有看到可变字典,所以崩溃应该在其他地方。
0赞 Stefano 2/18/2020
@Asperi 毫米,我更新了代码..i 插入在 do ChangePin 中调用的方法
0赞 Stefano 2/18/2020
我们在Skype上写@can?这是一个简单的错误,我相信它会立即修复它
0赞 Warren Burton 2/18/2020
嗨,Stefano,欢迎来到 SO。向我们展示一堵不相关的代码墙并不能帮助我们解决问题。您能否编辑为仅包含导致问题的几行。请阅读网站指南,了解什么是好问题。

答:

0赞 Asperi 2/18/2020 #1

这是生成 nil 的行,在 NSMutableDictionary 中不能插入 nil,

NSDictionary* dtoReq = [self.mcPersonalEnrollmentRequest getDictionary];

只需通过一些处理来添加此参数的验证,一切都会好起来的。