我现在被这个错误“EXC_BAD_ACCESS”困了几个小时,我似乎找不到什么问题。希望你们中的一些人会找到它。
我正在下载一个包含 json-data 的文件,将其保存为 NSMutableArray 中的自定义 NSObject,然后将其呈现在 UITableView 中。当我要在 UITableView 中呈现它时,我遇到了问题。
视图控制器.h
@property (nonatomic, retain) NSMutableArray *menuItems;
视图控制器.m
- (void)viewDidLoad
{
[super viewDidLoad];
menuItems = [[NSMutableArray alloc] init];
}
- (void)viewDidAppear:(BOOL)animated
{
[self downloadMenu];
}
-(void)downloadMenu
{
dispatch_queue_t fetchfromServerQueue = dispatch_queue_create("fetchfromServer", NULL);
[self createProgressionAlertWithMessage:@"Fetching menu"];
dispatch_async(fetchfromServerQueue, ^{
NSURL *menuURL = [NSURL URLWithString:@"anURL"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:menuURL];
NSError *error = nil;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
responseArray = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
menuItems = [[NSMutableArray alloc] initWithCapacity:[responseArray count]];
for (int i = 0; i<[responseArray count]; i++) {
NSDictionary *menuObject = [responseArray objectAtIndex:i];
NSString *x = [NSString stringWithFormat:@"%@", [menuObject objectForKey:@"x"]];
NSString *y = [NSString stringWithFormat:@"%@", [menuObject objectForKey:@"y"]];
NSString *z = [NSString stringWithFormat:@"%@", [menuObject objectForKey:@"z"]];
MenuItem *menuItem = [[MenuItem alloc] initWithX:x andY:y andZ:z];
[menuItems addObject:menuItem];
[menuItem release];
}
dispatch_async(dispatch_get_main_queue(), ^{
[progressAlert dismissWithClickedButtonIndex:0 animated:YES];
[menuTable reloadData];
});
});
dispatch_release(fetchfromServerQueue);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [menuItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
MenuItem *menuItem = (MenuItem *)[menuItems objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@", [menuItem x]];
return cell;
}
在 "cell.textLabel.text = [NSString stringWithFormat:@"%@", [menuItem x]];" 我得到“EXC_BAD_ACCESS(代码=1,地址=0xXXXXXXXX)”。
我不使用 ARC,并且在不久的将来无法通过该项目转换为它。由于我缺乏内存管理知识,这使它成为一个问题。
知道出了什么问题吗?
谢谢!
#### #### #### #### #### #### #### #### ####这是 MenuItem 的样子:
#import <Foundation/Foundation.h>
@interface MenuItem : NSObject
{
NSString *x;
NSString *y;
NSString *z;
}
-(id)initWithX:(NSString *)x_ andY:(NSString *)y_ andZ:(NSString *)z_;
-(NSString *)x;
-(NSString *)y;
-(NSString *)z;
@property(retain, nonatomic) NSString *x;
@property(retain, nonatomic) NSString *y;
@property(retain, nonatomic) NSString *z;
@end
#import "MenuItem.h"
@implementation MenuItem
@synthesize x;
@synthesize y;
@synthesize z;
-(id)initWithX:(NSString *)x_ andY:(NSString *)y_ andZ:(NSString *)z_
{
self = [super init];
if(self)
{
x = x_;
y = y_;
z = z_;
}
return self;
}
-(void)dealloc
{
[x release];
[y release];
[z release];
[super dealloc];
}
-(NSString *)x
{
return x;
}
-(NSString *)y
{
return y;
}
-(NSString *)z
{
return z;
}
@end