Object-c NSMutableDictionary的疑惑(为什么添加字典失败呢)
我的情况是这样子的,由于我要远程获取数据然后呈现数据,但是首先我获取的远程数据是json格式的,然后我移动端将其转换为字典类型。
出问题的代码如下:
我在.h文件中这样的
@property (strong, nonatomic) NSMutableDictionary *dataDic;
我在.m文件中这样的
-(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);
}
运行后,输出的结果如下:
2013-05-29 09:54:53.460 vlinkagePerson3[6359:907] self.dataDic = (null)
很奇怪,为什么结果为空呢,于是经过查找资料并进行尝试修改,结果就可以了
-(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];应该是没有分配空间吧
版权声明
由 davidzhang创作并维护的 Gowhich博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证。
本文首发于 博客( https://www.gowhich.com ),版权所有,侵权必究。
本文永久链接: https://www.gowhich.com/blog/164
版权声明
由 davidzhang创作并维护的 Gowhich博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证。
本文首发于 Gowhich博客( https://www.gowhich.com ),版权所有,侵权必究。
本文永久链接: https://www.gowhich.com/blog/164