我的情况是这样子的,由于我要远程获取数据然后呈现数据,但是首先我获取的远程数据是json格式的,然后我移动端将其转换为字典类型。
出问题的代码如下:
我在.h文件中这样的
1
| @property (strong, nonatomic) NSMutableDictionary *dataDic;
|
我在.m文件中这样的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| -(void) initData { NSDate *date = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; NSString *dateString = [dateFormatter stringFromDate: [NSDate dateWithTimeInterval:-(2 * 24 * 60 * 60) sinceDate:date]];
NSString *urlString = @"http://xxx.xxx.xxx.xxx:xxxxxxxxxx/xxxxx/xxxxxx?"; urlString = [urlString stringByAppendingFormat:@"order_by=%@",@"index"]; urlString = [urlString stringByAppendingFormat:@"&start_date=%@",dateString]; urlString = [urlString stringByAppendingFormat:@"&end_date=%@",dateString]; urlString = [urlString stringByAppendingFormat:@"&start=%@",@"1"]; urlString = [urlString stringByAppendingFormat:@"&offset=%@",@"20"]; urlString = [urlString stringByAppendingFormat:@"&app_key=%@",@""]; NSURL *url = [[NSURL alloc] initWithString:urlString]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60]; NSURLResponse *response = [[NSURLResponse alloc] init]; NSError *receiveDataError = [[NSError alloc] init];
NSMutableData *receivedData = (NSMutableData *)[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&receiveDataError]; if(receivedData == nil) { NSLog(@"获取数据失败"); } NSError *jsonError = [[NSError alloc] init]; NSDictionary *personDictionary = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableContainers error:&jsonError]; NSMutableDictionary *personInfo = [personDictionary objectForKey:@"data"]; NSMutableDictionary *personList = [personInfo objectForKey:@"list"];
if([personList isKindOfClass:[NSMutableArray class]]) { int i = 0; for (NSDictionary *dic in personList) { [self.dataDic setObject:dic forKey:[NSString stringWithFormat:@"%d",i]]; i++; } } if([personList isKindOfClass:[NSMutableDictionary class]]) { NSLog(@"NSMutableDictionary 类"); } NSLog(@"self.dataDic = %@",self.dataDic); }
|
运行后,输出的结果如下:
1
| 2013-05-29 09:54:53.460 vlinkagePerson3[6359:907] self.dataDic = (null)
|
很奇怪,为什么结果为空呢,于是经过查找资料并进行尝试修改,结果就可以了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| -(void) initData { NSDate *date = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; NSString *dateString = [dateFormatter stringFromDate: [NSDate dateWithTimeInterval:-(2 * 24 * 60 * 60) sinceDate:date]];
NSString *urlString = @"http://xxx.xxx.xxx.xxx:xxxxxxxxxx/xxxxx/xxxxxx?"; urlString = [urlString stringByAppendingFormat:@"order_by=%@",@"index"]; urlString = [urlString stringByAppendingFormat:@"&start_date=%@",dateString]; urlString = [urlString stringByAppendingFormat:@"&end_date=%@",dateString]; urlString = [urlString stringByAppendingFormat:@"&start=%@",@"1"]; urlString = [urlString stringByAppendingFormat:@"&offset=%@",@"20"]; urlString = [urlString stringByAppendingFormat:@"&app_key=%@",@""]; NSURL *url = [[NSURL alloc] initWithString:urlString]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60]; NSURLResponse *response = [[NSURLResponse alloc] init]; NSError *receiveDataError = [[NSError alloc] init];
NSMutableData *receivedData = (NSMutableData *)[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&receiveDataError]; if(receivedData == nil) { NSLog(@"获取数据失败"); } NSError *jsonError = [[NSError alloc] init]; NSDictionary *personDictionary = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableContainers error:&jsonError]; NSMutableDictionary *personInfo = [personDictionary objectForKey:@"data"]; NSMutableDictionary *personList = [personInfo objectForKey:@"list"]; self.dataDic = [NSMutableDictionary dictionaryWithCapacity:20]; if([personList isKindOfClass:[NSMutableArray class]]) { int i = 0; for (NSDictionary *dic in personList) { [self.dataDic setObject:dic forKey:[NSString stringWithFormat:@"%d",i]]; i++; } } if([personList isKindOfClass:[NSMutableDictionary class]]) { NSLog(@"NSMutableDictionary 类"); } NSLog(@"self.dataDic = %@",self.dataDic); }
|
关键一点是这里self.dataDic = [NSMutableDictionary dictionaryWithCapacity:20];
应该是没有分配空间吧