简体   繁体   中英

perform segue throws error

i have implemented some Json post in objective c and after i received the information i try to push the segue method but it throws this exception

2015-06-25 00:08:33.807 BarApp[1446:109590] -[NSNull length]: unrecognized selector sent to instance 0x37439830

2015-06-25 00:08:33.809 BarApp[1446:109590] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x37439830'

and this is my code

-(void) MakePost: (int)typePost {
    NSString *mainUrl = @"http://url.com/barap/usuario.php";
    NSString *post;
    
    if(typePost == 0) {
        post = [NSString stringWithFormat:@"?type=0&email=%@&password=%@",self.emailTextField.text, self.passwordTextField.text];
    }else if(typePost == 1){
        post = [NSString stringWithFormat:@"?type=1&fb_id=%@&nombre=%@&apellido_m=%@&email=%@&profile_picture=%@",fb_id, nombre, apellidoM, email, profilePictureUrl];
    }
    
    NSString *webPostUrl = [NSString stringWithFormat:@"%@%@", mainUrl, post];
    webPostUrl =[webPostUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *postUrl = [NSURL URLWithString:webPostUrl];
    NSData *userInfo = [[NSData alloc] initWithContentsOfURL:postUrl];

    if(userInfo){
        NSMutableDictionary *userResults = [NSJSONSerialization JSONObjectWithData:userInfo options:NSJSONReadingMutableContainers error:nil];
        if (![userResults[@"id"] isEqualToString:@""]) {
            [defaults setObject:userResults[@"id"] forKey:@"userId"];
            NSLog(@"%@", [[NSUserDefaults standardUserDefaults] valueForKey:@"userId"]);

this is where my code breaks up!

[self performSegueWithIdentifier:@"loginSuccess" sender:self];
            
            
            if(typePost == 1){
                [FBSDKAccessToken setCurrentAccessToken:nil];
                [FBSDKProfile setCurrentProfile:nil];
            }
        }
    }else {
        UIAlertView* cError = [[UIAlertView alloc]initWithTitle:@"Erro!" message:@"Tuvimos un Error intente mas tarde" delegate:self cancelButtonTitle:@"Cancelar" otherButtonTitles:@"OK", nil];
        [cError show];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([[segue identifier] isEqualToString:@"loginSuccess"]) {
        [segue destinationViewController];
    }
}

The message indicates that the length method was invoked on an NSNull object. Since the length method will most likely be invoked on an NSString

Check this link

-[NSNull length]: unrecognized selector sent to JSON objects

you should check for [NSNULL null]. You are receiving a or more NSNUll value in your Json request.

check this may it is yor need- [NSNull isEqualToString:]

these lines will help you more- NSMutableDictionary *dicAfterRemovingNull = [NSMutableDictionary dictionaryWithDictionary: yourDictionary]; // if the dictionary and for array change this line with MutableArray

for(NSString *key in [dicAfterRemovingNull allKeys])
{
    const id object = [dicAfterRemovingNull objectForKey:key];
    if(object == [NSNull null])
    {
        [dicAfterRemovingNull setValue:@"whatever you want" forKey:key];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM