我正在使用AFNetworking 3.0
。
在View控制器中,我在每个单元格中显示了多个专业标题。点击特定的单元格后,我得到了服务器的响应,如下所示:
[{"dp":{"id":0,"qualification":"MD(Doctor)","reg_id":0,"specialization1":"Orthopaedics","specialization2":"Dermatologist","specialization3":"Neurology","url":"www.batras.com"},"ds":{"city":"agiripalle","consultation_fee":450,"contact_no":"1234567890","country":"India","id":18,"organization_name":"Batra","pin_code":"411052","reg_id":0,"state":"andhra pradesh","street_name":"jhbfahjf","wellness_flag":false},"reg":{"mobile_no":0,"name":"Batra","profile_id":0,"reg_id":157,"wellness_id":"251215782521"}},{"dp":{"id":0,"qualification":"MD(Doctor)","reg_id":0,"specialization1":"Orthopaedics","specialization2":"Dermatologist","specialization3":"Neurology","url":"www.batras.com"},"ds":{"city":"anur","consultation_fee":458,"contact_no":"1234567898","country":"India","id":19,"organization_name":"Danny","pin_code":"411052","reg_id":0,"state":"himachal pradesh","street_name":"afafwfw","wellness_flag":false},"reg":{"mobile_no":0,"name":"Batra","profile_id":0,"reg_id":157,"wellness_id":"251215782521"}},{"dp":{"id":0,"qualification":"MD(Doctor)","reg_id":0,"specialization1":"Orthopaedics","specialization2":"Dermatologist","specialization3":"Neurology","url":"www.batras.com"},"ds":{"city":"pabal","consultation_fee":500,"contact_no":"4512451252","country":"India","id":20,"organization_name":"Org Name","pin_code":"411052","reg_id":0,"state":"maharashtra","street_name":"afawafww","wellness_flag":false},"reg":{"mobile_no":0,"name":"Batra","profile_id":0,"reg_id":157,"wellness_id":"251215782521"}},{"dp":{"id":0,"qualification":"MD(Doctor)","reg_id":0,"specialization1":"Orthopaedics","specialization2":"Dermatologist","specialization3":"Neurology","specialization4":"Gynecologist","url":"danny.com"},"ds":{"city":"bumlitan","consultation_fee":504,"contact_no":"1234567898","country":"India","id":21,"organization_name":"Danny","pin_code":"411052","reg_id":0,"state":"andaman and nicobar islands","street_name":"East Street","wellness_flag":false},"reg":{"mobile_no":0,"name":"Danny","profile_id":0,"reg_id":167,"wellness_id":"311220165848"}},{"dp":{"id":0,"qualification":"MD(Doctor)","reg_id":0,"specialization1":"Orthopaedics","specialization2":"Dermatologist","specialization3":"Neurology","specialization4":"Gynecologist","url":"danny.com"},"ds":{"city":"amtala","consultation_fee":451,"contact_no":"1234567898","country":"India","id":22,"organization_name":"Danny","pin_code":"411052","reg_id":0,"state":"assam","street_name":"West","wellness_flag":false},"reg":{"mobile_no":0,"name":"Danny","profile_id":0,"reg_id":167,"wellness_id":"311220165848"}},{"dp":{"id":0,"qualification":"MD(Doctor)","reg_id":0,"specialization1":"Orthopaedics","specialization2":"Dermatologist","specialization3":"Neurology","specialization4":"Gynecologist","url":"danny.com"},"ds":{"city":"hala","consultation_fee":541,"contact_no":"1234567890","country":"India","id":23,"organization_name":"Danny Clinic","pin_code":"411123","reg_id":0,"state":"chandigarh","street_name":"Chandigarh","wellness_flag":false},"reg":{"mobile_no":0,"name":"Danny","profile_id":0,"reg_id":167,"wellness_id":"311220165848"}}]
之后我按照以下方式处理响应:
NSLog(@"Response from specialization server : %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
NSArray *ResponseArray = [NSJSONSerialization JSONObjectWithData: responseObject options: kNilOptions error: nil];
if (ResponseArray.count >0)
{
_spclarr = [ResponseArray mutableCopy];
NSLog(@"special_array : %@",_spclarr);
_spclstr=[NSString stringWithFormat:@"%@",_spclarr];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[prefs setObject:_spclstr forKey:@"spcl"];
[self performSegueWithIdentifier:@"doctorspcl" sender:self];
}
[self getdata:responseObject];
} failure:^(NSURLSessionTask *operation, NSError *error)
{
// If Error occure, then this is AlertController Appear
NSLog(@"Error: %@", error);
在下一个视图中:
- (void)viewDidLoad {
[super viewDidLoad];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *s = [prefs stringForKey:@"spcl"];
NSLog(@"spcl: %@",s);
menuItems=[s componentsSeparatedByString:@","];
NSLog(@"menu: %@",menuItems);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [menuItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Doctordetail";
DoctorDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *content = [menuItems objectAtIndex:indexPath.row];
NSLog(@"content: %@",content);
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%ld",(long)indexPath.row);
NSDictionary *content = [menuItems objectAtIndex:indexPath.row];
NSLog(@"Cell Clicked: %@",menuItems);
NSLog(@"content2: %@",content);
}
当我在模拟器上运行app时,它可以工作。当我点击特定单元格时,日志会打印该单元格的数据。
这里是menuItems:
menu: (
"(\n {\n dp = {\n id = 0;\n qualification = \"MD(Doctor)\";\n \"reg_id\" = 0;\n specialization1 = Orthopaedics;\n specialization2 = Dermatologist;\n specialization3 = Neurology;\n url = \"www.batras.com\";\n };\n ds = {\n city = agiripalle;\n \"consultation_fee\" = 450;\n \"contact_no\" = 1234567890;\n country = India;\n id = 18;\n \"organization_name\" = Batra;\n \"pin_code\" = 411052;\n \"reg_id\" = 0;\n state = \"andhra pradesh\";\n \"street_name\" = jhbfahjf;\n \"wellness_flag\" = 0;\n };\n reg = {\n \"mobile_no\" = 0;\n name = Batra;\n \"profile_id\" = 0;\n \"reg_id\" = 157;\n \"wellness_id\" = 251215782521;\n };\n }",
"\n {\n dp = {\n id = 0;\n qualification = \"MD(Doctor)\";\n \"reg_id\" = 0;\n specialization1 = Orthopaedics;\n specialization2 = Dermatologist;\n specialization3 = Neurology;\n url = \"www.batras.com\";\n };\n ds = {\n city = anur;\n \"consultation_fee\" = 458;\n \"contact_no\" = 1234567898;\n country = India;\n id = 19;\n \"organization_name\" = Danny;\n \"pin_code\" = 411052;\n \"reg_id\" = 0;\n state = \"himachal pradesh\";\n \"street_name\" = afafwfw;\n \"wellness_flag\" = 0;\n };\n reg = {\n \"mobile_no\" = 0;\n name = Batra;\n \"profile_id\" = 0;\n \"reg_id\" = 157;\n \"wellness_id\" = 251215782521;\n };\n }",
"\n {\n dp = {\n id = 0;\n qualification = \"MD(Doctor)\";\n \"reg_id\" = 0;\n specialization1 = Orthopaedics;\n specialization2 = Dermatologist;\n specialization3 = Neurology;\n url = \"www.batras.com\";\n };\n ds = {\n city = pabal;\n \"consultation_fee\" = 500;\n \"contact_no\" = 4512451252;\n country = India;\n id = 20;\n \"organization_name\" = \"Org Name\";\n \"pin_code\" = 411052;\n \"reg_id\" = 0;\n state = maharashtra;\n \"street_name\" = afawafww;\n \"wellness_flag\" = 0;\n };\n reg = {\n \"mobile_no\" = 0;\n name = Batra;\n \"profile_id\" = 0;\n \"reg_id\" = 157;\n \"wellness_id\" = 251215782521;\n };\n }",
"\n {\n dp = {\n id = 0;\n qualification = \"MD(Doctor)\";\n \"reg_id\" = 0;\n specialization1 = Orthopaedics;\n specialization2 = Dermatologist;\n specialization3 = Neurology;\n specialization4 = Gynecologist;\n url = \"danny.com\";\n };\n ds = {\n city = bumlitan;\n \"consultation_fee\" = 504;\n \"contact_no\" = 1234567898;\n country = India;\n id = 21;\n \"organization_name\" = Danny;\n \"pin_code\" = 411052;\n \"reg_id\" = 0;\n state = \"andaman and nicobar islands\";\n \"street_name\" = \"East Street\";\n \"wellness_flag\" = 0;\n };\n reg = {\n \"mobile_no\" = 0;\n name = Danny;\n \"profile_id\" = 0;\n \"reg_id\" = 167;\n \"wellness_id\" = 311220165848;\n };\n }",
"\n {\n dp = {\n id = 0;\n qualification = \"MD(Doctor)\";\n \"reg_id\" = 0;\n specialization1 = Orthopaedics;\n specialization2 = Dermatologist;\n specialization3 = Neurology;\n specialization4 = Gynecologist;\n url = \"danny.com\";\n };\n ds = {\n city = amtala;\n \"consultation_fee\" = 451;\n \"contact_no\" = 1234567898;\n country = India;\n id = 22;\n \"organization_name\" = Danny;\n \"pin_code\" = 411052;\n \"reg_id\" = 0;\n state = assam;\n \"street_name\" = West;\n \"wellness_flag\" = 0;\n };\n reg = {\n \"mobile_no\" = 0;\n name = Danny;\n \"profile_id\" = 0;\n \"reg_id\" = 167;\n \"wellness_id\" = 311220165848;\n };\n }",
"\n {\n dp = {\n id = 0;\n qualification = \"MD(Doctor)\";\n \"reg_id\" = 0;\n specialization1 = Orthopaedics;\n specialization2 = Dermatologist;\n specialization3 = Neurology;\n specialization4 = Gynecologist;\n url = \"danny.com\";\n };\n ds = {\n city = hala;\n \"consultation_fee\" = 541;\n \"contact_no\" = 1234567890;\n country = India;\n id = 23;\n \"organization_name\" = \"Danny Clinic\";\n \"pin_code\" = 411123;\n \"reg_id\" = 0;\n state = chandigarh;\n \"street_name\" = Chandigarh;\n \"wellness_flag\" = 0;\n };\n reg = {\n \"mobile_no\" = 0;\n name = Danny;\n \"profile_id\" = 0;\n \"reg_id\" = 167;\n \"wellness_id\" = 311220165848;\n };\n }\n)"
)
和内容:
content: (
{
dp = {
id = 0;
qualification = "MD(Doctor)";
"reg_id" = 0;
specialization1 = Orthopaedics;
specialization2 = Dermatologist;
specialization3 = Neurology;
url = "www.batras.com";
};
ds = {
city = agiripalle;
"consultation_fee" = 450;
"contact_no" = 1234567890;
country = India;
id = 18;
"organization_name" = Batra;
"pin_code" = 411052;
"reg_id" = 0;
state = "andhra pradesh";
"street_name" = jhbfahjf;
"wellness_flag" = 0;
};
reg = {
"mobile_no" = 0;
name = Batra;
"profile_id" = 0;
"reg_id" = 157;
"wellness_id" = 251215782521;
};
}
2016-12-21 13:52:48.184 Wellness_24x7[1223:55790] content:
{
dp = {
id = 0;
qualification = "MD(Doctor)";
"reg_id" = 0;
specialization1 = Orthopaedics;
specialization2 = Dermatologist;
specialization3 = Neurology;
url = "www.batras.com";
};
ds = {
city = anur;
"consultation_fee" = 458;
"contact_no" = 1234567898;
country = India;
id = 19;
"organization_name" = Danny;
"pin_code" = 411052;
"reg_id" = 0;
state = "himachal pradesh";
"street_name" = afafwfw;
"wellness_flag" = 0;
};
reg = {
"mobile_no" = 0;
name = Batra;
"profile_id" = 0;
"reg_id" = 157;
"wellness_id" = 251215782521;
};
}
2016-12-21 13:52:48.185 Wellness_24x7[1223:55790] content:
{
dp = {
id = 0;
qualification = "MD(Doctor)";
"reg_id" = 0;
specialization1 = Orthopaedics;
specialization2 = Dermatologist;
specialization3 = Neurology;
url = "www.batras.com";
};
ds = {
city = pabal;
"consultation_fee" = 500;
"contact_no" = 4512451252;
country = India;
id = 20;
"organization_name" = "Org Name";
"pin_code" = 411052;
"reg_id" = 0;
state = maharashtra;
"street_name" = afawafww;
"wellness_flag" = 0;
};
reg = {
"mobile_no" = 0;
name = Batra;
"profile_id" = 0;
"reg_id" = 157;
"wellness_id" = 251215782521;
};
}
2016-12-21 13:52:48.187 Wellness_24x7[1223:55790] content:
{
dp = {
id = 0;
qualification = "MD(Doctor)";
"reg_id" = 0;
specialization1 = Orthopaedics;
specialization2 = Dermatologist;
specialization3 = Neurology;
specialization4 = Gynecologist;
url = "danny.com";
};
ds = {
city = bumlitan;
"consultation_fee" = 504;
"contact_no" = 1234567898;
country = India;
id = 21;
"organization_name" = Danny;
"pin_code" = 411052;
"reg_id" = 0;
state = "andaman and nicobar islands";
"street_name" = "East Street";
"wellness_flag" = 0;
};
reg = {
"mobile_no" = 0;
name = Danny;
"profile_id" = 0;
"reg_id" = 167;
"wellness_id" = 311220165848;
};
}
2016-12-21 13:52:52.862 Wellness_24x7[1223:55790] content:
{
dp = {
id = 0;
qualification = "MD(Doctor)";
"reg_id" = 0;
specialization1 = Orthopaedics;
specialization2 = Dermatologist;
specialization3 = Neurology;
specialization4 = Gynecologist;
url = "danny.com";
};
ds = {
city = amtala;
"consultation_fee" = 451;
"contact_no" = 1234567898;
country = India;
id = 22;
"organization_name" = Danny;
"pin_code" = 411052;
"reg_id" = 0;
state = assam;
"street_name" = West;
"wellness_flag" = 0;
};
reg = {
"mobile_no" = 0;
name = Danny;
"profile_id" = 0;
"reg_id" = 167;
"wellness_id" = 311220165848;
};
}
2016-12-21 13:52:52.962 Wellness_24x7[1223:55790] content:
{
dp = {
id = 0;
qualification = "MD(Doctor)";
"reg_id" = 0;
specialization1 = Orthopaedics;
specialization2 = Dermatologist;
specialization3 = Neurology;
specialization4 = Gynecologist;
url = "danny.com";
};
ds = {
city = hala;
"consultation_fee" = 541;
"contact_no" = 1234567890;
country = India;
id = 23;
"organization_name" = "Danny Clinic";
"pin_code" = 411123;
"reg_id" = 0;
state = chandigarh;
"street_name" = Chandigarh;
"wellness_flag" = 0;
};
reg = {
"mobile_no" = 0;
name = Danny;
"profile_id" = 0;
"reg_id" = 167;
"wellness_id" = 311220165848;
};
}
)
2016-12-21 13:52:53.918 Wellness_24x7[1223:55790] content: (
{
dp = {
id = 0;
qualification = "MD(Doctor)";
"reg_id" = 0;
specialization1 = Orthopaedics;
specialization2 = Dermatologist;
specialization3 = Neurology;
url = "www.batras.com";
};
ds = {
city = agiripalle;
"consultation_fee" = 450;
"contact_no" = 1234567890;
country = India;
id = 18;
"organization_name" = Batra;
"pin_code" = 411052;
"reg_id" = 0;
state = "andhra pradesh";
"street_name" = jhbfahjf;
"wellness_flag" = 0;
};
reg = {
"mobile_no" = 0;
name = Batra;
"profile_id" = 0;
"reg_id" = 157;
"wellness_id" = 251215782521;
};
}
但我的问题是,它没有显示在UITableViewCell
我尝试按以下方式显示它:
[cell.drname setText:[[content objectForKey:@"dp"]valueForKey:@"qualification"]];
以及
[cell.drname setText:[NSString stringWithFormat:@"%@", [[content objectForKey:@"dp"]valueForKey:@"qualification"]]];
但它给我的错误如下:
由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [__ NSCFString objectForKey:]:无法识别的选择器发送到实例0x7a07c600'
我没错,但我错了。
请任何人都可以解决我的问题。帮助将是可观的。
答案 0 :(得分:2)
问题是您有NSArray
而不是NSDictionary
。试试这个 -
NSArray* arr = @[@{@"dp":@{@"id":@"0",@"qualification":@"MD(Doctor)"}},
@{@"ds":@{@"id":@"1",@"qualification":@"MD(Nurse)"}}];
NSDictionary *dpDict = [[arr objectAtIndex:0]objectForKey:@"dp"];
NSString *str = [dpDict valueForKey:@"qualification"];
在这里,你得到了字符串。它并没有崩溃。
在cellForRowAtIndexPath
中使用
NSString *str = [[[menuItems objectAtIndex:indexPath.row]objectForKey:@"dp"]valueForKey:@"qualification"];
得到字符串。
答案 1 :(得分:2)
您需要进行以下更改。
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:@"YourURL" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"JSON: %@", responseObject);
NSArray *ResponseArray = (NSArray *)responseObject;
if (ResponseArray.count >0)
{
_spclarr = [ResponseArray mutableCopy];
NSLog(@"special_array : %@",_spclarr);
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving Array to NSUserDefaults
[prefs setObject:_spclarr forKey:@"spcl"];
[prefs synchronize];
[self performSegueWithIdentifier:@"doctorspcl" sender:self];
}
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
详细信息视图控制器
-(void)viewDidLoad
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
//directly reading the array
menuItems = [prefs objectForKey:@"spcl"];
}
你的cellForRowAtIndexPath看起来应该是这样的
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSArray *menuItems = [prefs objectForKey:@"spcl"];
NSDictionary *content = [menuItems objectAtIndex:indexPath.row];
[cell.textLabel setText:[[content objectForKey:@"dp"]valueForKey:@"qualification"]];
return cell;
}
当我们以数组格式获得响应时,为什么要将它作为NSString存储在NSUserDefaults
中?要摆脱它,您需要直接在NSUserDefaults
中存储一个数组并直接使用该数组。
答案 2 :(得分:0)
试试这个:
NSDictionary *dpDict = [content objectForKey:@"dp"];
if (dpDict && [dpDict isKindOfClass:[NSDictionary class]] && [dpDict objectForKey:@"qualification"] != nil) {
[cell.drname setText:[dpDict objectForKey:@"qualification"]];
}