执行segue抛出错误

时间:2015-06-25 05:16:57

标签: ios objective-c iphone

我在目标c中实现了一些Json帖子,在我收到信息之后我尝试推送segue方法,但它抛出了这个异常

  

2015-06-25 00:08:33.807 BarApp [1446:109590] - [NSNull length]:无法识别的选择器发送到实例0x37439830

     

2015-06-25 00:08:33.809 BarApp [1446:109590] ***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [NSNull length]:无法识别的选择器发送到实例0x37439830'

这是我的代码

-(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"]);

这是我的代码崩溃的地方!

[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];
    }
}

2 个答案:

答案 0 :(得分:3)

该消息表明在length对象上调用了NSNull方法。由于最有可能在length

上调用NSString方法

检查此链接

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

希望它有所帮助。

答案 1 :(得分:1)

你应该检查[NSNULL null]。您在Json请求中收到一个或多个NSNUll值。

检查这可能是你需要 - [NSNull isEqualToString:]

这些线路将帮助您更多 -  NSMutableDictionary * dicAfterRemovingNull = [NSMutableDictionary dictionaryWithDictionary:yourDictionary]; //如果字典和数组使用MutableArray

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