如何从Xcode中的textfield值中找到索引数组?

时间:2014-03-04 09:46:44

标签: php mysql ios json xcode

我需要从mysql获取值并动态显示在Xcode文本域中。(当用户输入firstName时,想要自动输入lastName和phoneNumber中的字符串) 在这里我的Xcode ..

  NSError *err;
  NSString *strURL=[NSString stringWithFormat:@"http://192.168.1.22:81/priya/sam.php"];
  NSData *dataURL=[NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

  NSString *name=firstname.text;
  NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:dataURL options:  NSJSONReadingMutableContainers error:&err];
  NSArray *array=[jsonArray objectForKey:@"key"];


  lastname.text=[[array objectAtIndex:0]objectForKey:@"lastname"];
  phone.text=[[array objectAtIndex:0]objectForKey:@"phone"];

然后如何使用名字查找索引的文本数组。    提前谢谢

2 个答案:

答案 0 :(得分:0)

我不太确定问题是什么(你只有1行而不是所需的行数吗?)

显示收到数据的所有行:

for (NSDictionary *item in array) {
    firstname.text=[item objectForKey:@"firstName"];
    lastname.text=[item objectForKey:@"lastname"];
    phone.text=[item objectForKey:@"phone"];
}

答案 1 :(得分:0)

您为服务器获取的输出是字典,父键是“Key”。要动态获取数组索引,请参阅下面的代码

NSMutableArray *infoArray=[[NSMutableArray alloc]init];
infoArray =[jsonArray objectForKey:@"Key"]; // this is your array with all information stored index wise.
NSLog(@"%@", infoArray);
for(int i=0; i<infoArray.count; i++)
{
NSLog(@"%@", [[infoArray objectAtIndex:i]objectForKey:@"firstname"]);
NSLog(@"%@", [[infoArray objectAtIndex:i]objectForKey:@"lastnamename"]);
}

或 问题是PHP方要求他们用“(”和“)”替换“[”和“]”。

希望这会有所帮助