概要:
先谢过各位走过路过的V友。本人模仿AFNetWorking的Demo写了非ARC的Demo,结果有内存bug,以下为主要代码,忘大神们点拨下。
rootVC.h中申明
@
property (copy, nonatomic) NSMutableArray *dataSource;
rootVC.h中请求数据
- (void)requestJsonData
{
// NSString *therUrl = [NSString stringWithFormat:@"%@GetTestInfo.php",BaseURLString];
NSURL *url = [NSURL URLWithString:BaseURLString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSDictionary *tmpDic= (NSDictionary *)JSON;
NSDictionary *dic =[tmpDic valueForKey:@"data"];
NSArray *wetherArray = [dic valueForKey:@"weather"];
NSMutableArray *dataSourceArray = [NSMutableArray arrayWithCapacity:[wetherArray count]];
for (NSDictionary *item in wetherArray) {
RootViewCellModel *metaData = [[RootViewCellModel alloc] initWithAttributes:item];
[dataSourceArray addObject:metaData];
[metaData release];
}
self.dataSource = dataSourceArray;
[self.tableView reloadData];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSString *errorInfo = [NSString stringWithFormat:@"%@",error];
NSLog(@"%@",error);
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
message:[NSString stringWithFormat:@"%@",error]
delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[av show];
}];
[operation start];
}
metaData.h 中声明
@
interface metaData : NSObject
@
property (nonatomic, copy) NSString *projectName;
@
property (nonatomic, copy) NSString *addDec;
@
property (nonatomic, copy) NSString *price;
@
property (nonatomic, copy) NSString *tel;
@
property (nonatomic, copy) NSString *imageURLString;
- (id)initWithAttributes:(NSDictionary *)attributes;
@
end重点来了
metaData.m实现
@
implementation RootViewCellModel
- (void)dealloc
{
[_projectName release];
[_addDec release];
[_price release];
[_tel release];
[_imageURLString release];
[super dealloc];
}
- (id)initWithAttributes:(NSDictionary *)attributes {
self = [super init];
if (self) {
NSString *tmp= nil;
tmp = [attributes valueForKey:@"date"];
NSLog(@"tmp retainCount %d",[tmp retainCount]);
_projectName = [NSString stringWithString:tmp];
NSLog(@"tmp retainCount %d",[tmp retainCount]);
tmp = [attributes valueForKey:@"precipMM"];
_addDec = [NSString stringWithString:tmp];
tmp = [attributes valueForKey:@"tempMaxC"];
_price = [NSString stringWithString:tmp];
tmp = [attributes valueForKey:@"weatherCode"];
_tel = [NSString stringWithString:tmp];
}
return self;
}
@
end 可能代码有点多,再次感谢看完的V友。