【问题标题】:[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x7faf59e2fd10[__NSArrayM objectForKey:]:无法识别的选择器发送到实例 0x7faf59e2fd10
【发布时间】:2016-10-18 20:31:21
【问题描述】:

我正在尝试制作通过 Json 从 php 文件中获取数据的登录页面。在我使用这句话 [jsonData objectForKey:@"state"] 之前它工作正常,出现异常。我试图制作另一个字典,比如之前的一些问题的答案。但没有什么对我有用。

- (IBAction)login:(id)sender {
    @try {

        NSString *post =[[NSString alloc] initWithFormat:@"email=%@&password=%@",[firsttext text],[second text]];
        NSLog(@"PostData: %@",post);

        NSURL *url=[NSURL URLWithString:@"http://itsolution.co.in/coverageapi/login.php"];

        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:url];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];

        [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

        NSError *error = nil;
        NSHTTPURLResponse *response = nil;
        NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

        NSLog(@"Response code: %d", [response statusCode]);
        if ([response statusCode] >=200 && [response statusCode] <300)
        {
            NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
            NSLog(@"Response ==> %@", responseData);
            SBJsonParser *jsonParser = [SBJsonParser new];
            NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
            NSLog(@"jsondata %@",jsonData);
            NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
            NSLog(@"%d",success);
            if(success == 1)
            {
                NSLog(@"Login SUCCESS");
            } else {
                NSLog(@"failed");
            }

        } else {
            if (error) NSLog(@"Error: %@", error);
        }

    }
    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);
    }
   }

【问题讨论】:

  • [__NSArrayM objectForKey:]: unrecognized selector 简单地说,接收者是一个数组而不是字典。
  • @Dipek 关键“状态”在您的回复中不存在。

标签: ios unrecognized-selector


【解决方案1】:

试试这个并请检查状态密钥是否存在 1st

- (void) logIn { 

     ///1 
 NSMutableDictionary *responseDic_Lv=[[NSMutableDictionary alloc]init];
NSString *post_Lv = [NSString       stringWithFormat:@"email_id=%@&password=%@"     ,userNameTFT1.text,passwordTFT2.text];


 ///2
 NSData *postData_Lv = [post_Lv dataUsingEncoding:NSASCIIStringEncoding      allowLossyConversion:YES];
NSString *postLength_Lv = [NSString stringWithFormat:@"%lu",(unsigned     long)[postData_Lv length]];

//NSLog(@"Post Length Is... %@",postLength_Lv);


 //3
 NSString *BaseUrl_Login = [NSString stringWithFormat:@"Your_Login_URL"];

   NSMutableURLRequest *request_Lv =[[NSMutableURLRequest alloc] init];
 [request_Lv setURL:[NSURL URLWithString:BaseUrl_Login]];

 //NSLog(@" Rahul Goku request is >>> %@",request);

   [request_Lv setHTTPMethod:@"POST"]; //Write Post or Get accordind  method here

   [request_Lv setValue:postLength_Lv forHTTPHeaderField:@"Content-Length"];

   [request_Lv setValue:@"application/x-www-form-urlencoded"   forHTTPHeaderField:@"Content-Type"];

   [request_Lv setHTTPBody:postData_Lv]; //Set HTTP

    NSData *response_Lv = [NSURLConnection   sendSynchronousRequest:request_Lv
     returningResponse:nil error:nil];

  responseDic_Lv=[NSJSONSerialization JSONObjectWithData:response_Lv    options:NSJSONReadingAllowFragments error:nil];



 ///4 Chk Data...
   NSString *ResultStr1 = (NSString *)[responseDic_Lv   valueForKey:@"YourKey"];

NSLog(@"Result From Server is=====> %@",ResultStr1);


 if ([ResultStr1 isEqualToString:@"success"]) {


 NSString *ResponseStr2 = (NSString *)[responseDic_Lv  valueForKey:@"YourKey"];



 } else {  // Login Error password or email.... Error...


    }



}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    相关资源
    最近更新 更多