內存管理,我現在已經堅持了這個錯誤「EXC_BAD_ACCESS」幾個小時,我似乎無法找出什麼是錯的。希望你們中的一些人能夠找到它。與GCD,NSMutableArray中和的UITableView
我downloadning含JSON-數據的文件,將其保存爲在一個NSMutableArray定製NSObject的,然後在一個UITableView呈現它。這是當我要在UITableView中呈現它時遇到問題。
ViewController.h
@property (nonatomic, retain) NSMutableArray *menuItems;
ViewController.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:@」 %@ 「[菜單項X]];」我得到「EXC_BAD_ACCESS(code = 1,address = 0xXXXXXXXX)」。
我不使用ARC,並且不能與這個項目轉換爲它在不久的將來。由於缺乏對內存管理的知識,這使其成爲一個問題。
有什麼想法嗎?
Thx!
#### #### #### #### #### #### #### #### ####這是菜單項的樣子:
#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
我瘋狂的猜測:你的第一段是點亮,但第二段有點偏離。我打賭什麼'x'是,它不是一個對象。 – Chuck 2013-05-06 23:56:09
我添加了MenuItem的代碼。 'x'是NSString。你覺得那個班有什麼問題嗎? – Cayak 2013-05-07 05:54:01
今天晚些時候我會嘗試殭屍。 – Cayak 2013-05-07 05:54:34