ios中的程序流问题
我正在解析json以在tableview中显示内容。我在getReceivedData中填充了包含解析json的数组,该数组在UITAbleView委托方法之后调用。因此,填充tableview是一个问题,因为当编译器尝试填充tableview时,数组尚未初始化ios中的程序流问题,ios,objective-c,nsmutabledictionary,Ios,Objective C,Nsmutabledictionary,我正在解析json以在tableview中显示内容。我在getReceivedData中填充了包含解析json的数组,该数组在UITAbleView委托方法之后调用。因此,填充tableview是一个问题,因为当编译器尝试填充tableview时,数组尚未初始化 - (void)getReceivedData:(NSMutableData *)data sender:(RestAPI *)sender{ NSError * error=nil; NSArray *receivedData =
- (void)getReceivedData:(NSMutableData *)data sender:(RestAPI *)sender{
NSError * error=nil;
NSArray *receivedData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSString *dictionaryKey=@"department";
NSString *predicateString=@"software";
NSPredicate *predicate=[NSPredicate predicateWithFormat:@" %K == %@ ", dictionaryKey,predicateString];
NSArray *shortlisted=[receivedData filteredArrayUsingPredicate:predicate];
for(int i = 0; i<shortlisted.count; i++)
{
NSDictionary *detailItems=[shortlisted objectAtIndex:i];
NSString *name=[detailItems objectForKey:@"emp_name"];
NSString *designation=[detailItems objectForKey:@"designation"];
NSString *email=[detailItems objectForKey:@"email"];
NSString *phone_no=[detailItems objectForKey:@"phone_no"];
// NSString *image=[detailItems objectForKey:@"url_path"];
dictionary1=[NSMutableDictionary dictionaryWithObjectsAndKeys:
name, @"keyname",
designation, @"keydesignation",
email, @"keyid",
phone_no, @"keyphone",
nil];
[myObject1 addObject:dictionary1];
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section{
if(isfiltered==YES){
return [filteredArray count];
}
else{
return [myObject1 count];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"myCell"];
if(!cell){
[tableView registerNib:[UINib nibWithNibName:@"MyTableCell" bundle:nil]
forCellReuseIdentifier:@"myCell"];
cell=[tableView dequeueReusableCellWithIdentifier:@"myCell"];
}
if(isfiltered==NO)
{
NSDictionary * tmpdict= [myObject objectAtIndex:indexPath.row];
cell.nameLabel.text=[NSMutableString stringWithFormat:@"%@",[tmpdict objectForKeyedSubscript:@"keyname"]];
cell.designationLabel.text=[NSMutableString stringWithFormat:@"%@",[tmpdict objectForKeyedSubscript:@"keydesignation"]];
cell.idLabel.text=[NSMutableString stringWithFormat:@"%@",[tmpdict objectForKeyedSubscript:@"keyid"]];
cell.phoneLabel.text=[NSMutableString stringWithFormat:@"%@",[tmpdict objectForKeyedSubscript:@"keyphone"]];
cell.mainImg.image = [UIImage imageNamed:[tmpdict objectForKeyedSubscript:@"keyimage"]];
}
else{
NSDictionary * tmpdict= [filteredArray objectAtIndex:indexPath.row];
cell.nameLabel.text=[NSMutableString stringWithFormat:@"%@",[tmpdict objectForKeyedSubscript:@"keyname"]];
cell.designationLabel.text=[NSMutableString stringWithFormat:@"%@",[tmpdict objectForKeyedSubscript:@"keydesignation"]];
cell.idLabel.text=[NSMutableString stringWithFormat:@"%@",[tmpdict objectForKeyedSubscript:@"keyid"]];
cell.phoneLabel.text=[NSMutableString stringWithFormat:@"%@",[tmpdict objectForKeyedSubscript:@"keyphone"]];
cell.mainImg.image = [UIImage imageNamed:[tmpdict objectForKeyedSubscript:@"keyimage"]];
}
return cell;
}
-(void)getReceivedData:(NSMutableData*)数据发送方:(RestAPI*)发送方{
n错误*错误=nil;
NSArray*receivedData=[NSJSONSerialization JSONObjectWithData:数据选项:NSJSONReadingAllowFragments错误:&错误];
NSString*dictionaryKey=@“部门”;
NSString*谓词字符串=@“软件”;
NSPredicate*谓词=[NSPredicate谓词格式:@“%K==%”,字典键,谓词字符串];
NSArray*入围=[receivedData filteredArrayUsingPredicate:predicate];
对于(int i=0;i实际上,这不是像您那样初始化字典的正确方法,@“%@”被使用%@是任何对象的格式字符串中的占位符
dictionary1=[NSMutableDictionary dictionaryWithObjectsAndKeys:
name,@"keyname",
designation,@"keydesignation",
email,@"keyid",
phone_no,@"keyphone",
nil];
实际上,这不是像您那样初始化字典的正确方法,@“%@”被使用%@是任何对象的格式字符串中的占位符
dictionary1=[NSMutableDictionary dictionaryWithObjectsAndKeys:
name,@"keyname",
designation,@"keydesignation",
email,@"keyid",
phone_no,@"keyphone",
nil];
在易变字典中,首先你必须给对象,然后给它的键。你做错了
for(int i = 0; i<shortlisted.count; i++)
{
NSDictionary *detailItems=[shortlisted objectAtIndex:i];
NSString *name=[detailItems objectForKey:@"emp_name"];
NSString *designation=[detailItems objectForKey:@"designation"];
NSString *email=[detailItems objectForKey:@"email"];
NSString *phone_no=[detailItems objectForKey:@"phone_no"];
dictionary1=[NSMutableDictionary dictionaryWithObjectsAndKeys:
name,@"keyname",
designation,@"keydesignation",
email,@"keyid",
phone_no,@"keyphone",
nil];
[myObject1 addObject:dictionary1];
}
for(inti=0;i在易变字典中,首先必须给对象赋值,然后才是它的键。你做错了
for(int i = 0; i<shortlisted.count; i++)
{
NSDictionary *detailItems=[shortlisted objectAtIndex:i];
NSString *name=[detailItems objectForKey:@"emp_name"];
NSString *designation=[detailItems objectForKey:@"designation"];
NSString *email=[detailItems objectForKey:@"email"];
NSString *phone_no=[detailItems objectForKey:@"phone_no"];
dictionary1=[NSMutableDictionary dictionaryWithObjectsAndKeys:
name,@"keyname",
designation,@"keydesignation",
email,@"keyid",
phone_no,@"keyphone",
nil];
[myObject1 addObject:dictionary1];
}
for(int i=0;i@“%@”,name,@“keyname”,
=>[NSString stringWithFormat:@“%@”,name],@“keyname”
,等等。或者干脆NSString*name=[NSString stringWithFormat:@“%@”,“detailItems objectForKey:@“emp_name”];和
name,@“keyname”`(不带“%@”)。只需像dictionary1一样更改即可=[NSMutableDictionary Dictionary With Objects and Keys:名称、@“keyname”、名称、@“keydesignation”、电子邮件、@“keyid”、电话号码、@“keyphone”、无];
@Larme非常感谢much@Anbu.Karthik非常感谢你,它成功了again@AashimaAnand-您是否获得了正确的字典数组,那么为什么再次更改,在需要@“%@”,name,@“keyname”,name,
=>[NSString stringWithFormat:@“%@”,name],@“keyname”的地方直接使用字典数组
,等等。或者简单地NSString*name=[NSString stringWithFormat:@“%@”、[detailItems objectForKey:@“emp_name”];和name、@“keyname”`(不带“%@”)。只需像dictionary1=[NSMutableDictionary dictionary dictionary with objectsandkeys:name、@“keyname”、designation、@“keynesignation”这样更改即可,电子邮件,@“keyid”,电话号码,@“keyphone”,无];
@Larme非常感谢much@Anbu.Karthik非常感谢你,它成功了again@AashimaAnand-你得到的字典数组是否正确,那么你为什么再次更改,在需要的地方直接使用你的字典数组