使用异步请求时的 exc_bad_access
我发出了一个异步请求,但是当我尝试用这段代码解析它时
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSDictionary *missionsDict = [responseString JSONValue];
/*NSArray *luckyNumbers = [json objectWithString:responseString error:&error];*/
NSLog(@"user Info array is: %@", missionsDict);
// NSDictionary *array = [luckyNumbers1 objectForKey:@"data"];
NSDictionary *missionsData;
missionsData = [missionsDict objectForKey:@"data"];
NSLog(@"missionsData is: %@", missionsData);
NSEnumerator *inner = [missionsData objectEnumerator];
UIScrollView *missionsScroll;
missionsScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
missionsScroll.contentSize = CGSizeMake(320, 1005);
[self.view addSubview:missionsScroll];
id value;
int badgeY1;
int badgeY2;
int badgeY3;
badgeY1 = 121;
badgeY2 = 161;
badgeY3 = 150;
while((value = [inner nextObject])) {
NSLog(@"progress is: %@", [value objectForKey:@"progress"]);
NSLog(@"user Info array is: %@", missionsDict);
NSLog(@"name is: %@",[value objectForKey:@"reward_definitions"]);
NSLog(@"missionsData is: %@", missionsData);
NSDictionary *moreData;
moreData = [value objectForKey:@"reward_definitions"];
NSEnumerator *inner = [moreData objectEnumerator];
id value2;
int badgeX;
badgeX = 10;
while((value2 = [inner nextObject])) {
UIProgressView *progressView;
progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, badgeY3, 372, 9)];
float progressValue;
progressValue = ([[[value objectForKey:@"progress"] objectForKey:@"earned"] floatValue] / [[[value objectForKey:@"progress"] objectForKey:@"possible"] floatValue]);
NSLog(@"progressValue is: %f", progressValue);
[progressView setProgress:progressValue];
[missionsScroll addSubview:progressView];
UILabel *missionName;
missionName = [[UILabel alloc] initWithFrame:CGRectMake(20, badgeY1, 280, 25)];
missionName.backgroundColor = [UIColor clearColor];
missionName.textColor = [UIColor whiteColor];
missionName.font = [UIFont fontWithName:@"Heiti TC" size:23.0];
missionName.text = [value objectForKey:@"name"];
[missionsScroll addSubview:missionName];
NSLog(@"badgeY2 is: %@", badgeY2);
badgesScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, badgeY2, 318, 70)];
badgesScroll.contentSize = CGSizeMake(320, 70);
[missionsScroll addSubview:badgesScroll];
NSLog(@"Image URL is: %@", [value2 objectForKey:@"image_url"]);
NSURL *url1 = [NSURL URLWithString: [NSString stringWithFormat:@"%@", [value2 objectForKey:@"image_url"]]];
NSData *urlData1 = [NSData dataWithContentsOfURL:url1];
UIImage *image1 = [UIImage imageWithData:urlData1];
UIImageView *badge = [[UIImageView alloc] initWithImage:image1];
[badge setFrame:CGRectMake(badgeX, -10, 70, 70)];
[badgesScroll addSubview:badge];
[badge release];
badgeCount = badgeCount+1;
badgeX = badgeX +80;
}
// NSLog(@"reward_definitions is: %@", [missionsData objectForKey:@"id"]);
// NSLog(@"Image URL is: %@", [[value objectForKey:@"reward_definitions"] objectForKey:@"image_url"]);
//if ( [array isKindOfClass:[NSDictionary class]] ) {
badgeY1 = badgeY1 +100;
badgeY2 = badgeY2 +100;
badgeY3 = badgeY3 +100;
missionCount = missionCount+1;
}
for (int b; badgeCount > 4; b = b+1) {
[badgesScroll setContentSize:CGSizeMake(badgesScroll.contentSize.width+80, badgesScroll.contentSize.height)];
}
for (int a; missionCount > 4; a = a+1) {
missionsScroll.contentSize = CGSizeMake(776, missionsScroll.contentSize.height+200);
}
}
它因 NSLog(@"badgeY2 is: %@", badgeY2); 上的 EXC_BAD_ACCESS 而崩溃
Im making a async request but when I try to parse it with this code
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSDictionary *missionsDict = [responseString JSONValue];
/*NSArray *luckyNumbers = [json objectWithString:responseString error:&error];*/
NSLog(@"user Info array is: %@", missionsDict);
// NSDictionary *array = [luckyNumbers1 objectForKey:@"data"];
NSDictionary *missionsData;
missionsData = [missionsDict objectForKey:@"data"];
NSLog(@"missionsData is: %@", missionsData);
NSEnumerator *inner = [missionsData objectEnumerator];
UIScrollView *missionsScroll;
missionsScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
missionsScroll.contentSize = CGSizeMake(320, 1005);
[self.view addSubview:missionsScroll];
id value;
int badgeY1;
int badgeY2;
int badgeY3;
badgeY1 = 121;
badgeY2 = 161;
badgeY3 = 150;
while((value = [inner nextObject])) {
NSLog(@"progress is: %@", [value objectForKey:@"progress"]);
NSLog(@"user Info array is: %@", missionsDict);
NSLog(@"name is: %@",[value objectForKey:@"reward_definitions"]);
NSLog(@"missionsData is: %@", missionsData);
NSDictionary *moreData;
moreData = [value objectForKey:@"reward_definitions"];
NSEnumerator *inner = [moreData objectEnumerator];
id value2;
int badgeX;
badgeX = 10;
while((value2 = [inner nextObject])) {
UIProgressView *progressView;
progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, badgeY3, 372, 9)];
float progressValue;
progressValue = ([[[value objectForKey:@"progress"] objectForKey:@"earned"] floatValue] / [[[value objectForKey:@"progress"] objectForKey:@"possible"] floatValue]);
NSLog(@"progressValue is: %f", progressValue);
[progressView setProgress:progressValue];
[missionsScroll addSubview:progressView];
UILabel *missionName;
missionName = [[UILabel alloc] initWithFrame:CGRectMake(20, badgeY1, 280, 25)];
missionName.backgroundColor = [UIColor clearColor];
missionName.textColor = [UIColor whiteColor];
missionName.font = [UIFont fontWithName:@"Heiti TC" size:23.0];
missionName.text = [value objectForKey:@"name"];
[missionsScroll addSubview:missionName];
NSLog(@"badgeY2 is: %@", badgeY2);
badgesScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, badgeY2, 318, 70)];
badgesScroll.contentSize = CGSizeMake(320, 70);
[missionsScroll addSubview:badgesScroll];
NSLog(@"Image URL is: %@", [value2 objectForKey:@"image_url"]);
NSURL *url1 = [NSURL URLWithString: [NSString stringWithFormat:@"%@", [value2 objectForKey:@"image_url"]]];
NSData *urlData1 = [NSData dataWithContentsOfURL:url1];
UIImage *image1 = [UIImage imageWithData:urlData1];
UIImageView *badge = [[UIImageView alloc] initWithImage:image1];
[badge setFrame:CGRectMake(badgeX, -10, 70, 70)];
[badgesScroll addSubview:badge];
[badge release];
badgeCount = badgeCount+1;
badgeX = badgeX +80;
}
// NSLog(@"reward_definitions is: %@", [missionsData objectForKey:@"id"]);
// NSLog(@"Image URL is: %@", [[value objectForKey:@"reward_definitions"] objectForKey:@"image_url"]);
//if ( [array isKindOfClass:[NSDictionary class]] ) {
badgeY1 = badgeY1 +100;
badgeY2 = badgeY2 +100;
badgeY3 = badgeY3 +100;
missionCount = missionCount+1;
}
for (int b; badgeCount > 4; b = b+1) {
[badgesScroll setContentSize:CGSizeMake(badgesScroll.contentSize.width+80, badgesScroll.contentSize.height)];
}
for (int a; missionCount > 4; a = a+1) {
missionsScroll.contentSize = CGSizeMake(776, missionsScroll.contentSize.height+200);
}
}
It crashes because of EXC_BAD_ACCESS on NSLog(@"badgeY2 is: %@", badgeY2);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的徽章Y2 包含一个整数值。要使用 NSLog 打印整数值,必须使用 %d
在使用 NSLog 时你应该时刻记住它返回什么样的值!
还有另一个解决方案可以让您知道
这些是 NSLog 格式说明符:
your badgeY2 contains an integer value. To print the integer value with NSLog, you must use %d
You should always keep it in mind while using NSLog, what kind of value it returns!
There is another heck solution to just let you know
These are the NSLog format specifiers :
应该是
badY2 是一个 int,所以你必须使用 %i 而不是 %@
Should be
badgeY2 is an int, so you must use %i instead of %@