2013-05-19 2 views
2

나는 아이폰 OS에 새로운, 그래서 완전히 바보 :)아이폰 OS가 EXC_BAD_ACCESS 함께 충돌 (코드 = 1)

다음

간다 보이는 내 코드의 지적에 대해 부끄러워하지 않습니다 ...

두 개의보기 컨트롤러 - OrderViewController 및 LineItemViewController - 사용자가 LineItemViewController에있을 때이 항목을 스캔 한 것으로 표시하도록 서버에 요청하는 "스캔"단추를 클릭 할 수 있습니다. 그것은 잘 작동하는 것 같다,하지만 난 콘솔 응용 프로그램에서이 오류를 얻을 : 28 :

5/19/13 (11) 04.044 오전 EvoScanner : tcp_connection_destination_fail net_helper_connect_fail이

응용 오류가 계속 후 잘 실행에 실패했습니다. 문제는 "Back"을 클릭하여 OrderViewController로 돌아갈 때 응용 프로그램이 EXC_BAD_ACCESS (코드 = 1)와 충돌합니다.

저는 ARC가 활성화 된 상태에서 XCode 4.6.2를 사용하고 있습니다.

// Interface 
#import <UIKit/UIKit.h> 
#import "LineItemModel.h" 

@interface LineItemViewController : UIViewController 
@property (strong, nonatomic) LineItemModel* _line_item; 
-(void)setDetailItem:(LineItemModel *) lineItem; 

@property (strong, nonatomic) IBOutlet UIButton *scanButton; 
- (IBAction)scanItem:(id)sender; 

@property (strong, nonatomic) IBOutlet UILabel *itemLabel; 
-(IBAction)scanItem; 
@end 

// Implementation 
#import "LineItemViewController.h" 
#import "LineItemModel.h" 
#import "HUD.h" 
#import "JSONModelLib.h" 

@interface LineItemViewController() { 
    LineItemModel *_line_item; 
} 
@end 

@implementation LineItemViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.itemLabel.text = _line_item.product_title; 
    // Do any additional setup after loading the view. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(void)setDetailItem:(id)lineItem { 
    if(_line_item != lineItem) { 
    _line_item = lineItem; 

    [self configureView]; 
    } 
} 

- (void)configureView 
{ 
    // Update the user interface for the detail item. 
    if (self._line_item) { 
     // self.detailDescriptionLabel.text = [self.detailItem description]; 
    } 
} 

- (IBAction)scanItem:(id)sender { 
    NSLog(@"Scanning!"); 
    NSString *string_url = [NSString stringWithFormat:(NSString *)@"%@/%@", @"http://localhost:3000/api/scan_item", _line_item.id ]; 
    NSURL *url = [NSURL URLWithString:string_url]; 
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
    NSString *messageBody = [NSString stringWithFormat:@"status=%@",@1]; 
    NSString *msgLength = [NSString stringWithFormat:@"%d", [messageBody length]]; 
    [theRequest setHTTPMethod:@"POST"]; 
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; 
    [theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [theRequest setHTTPBody:[messageBody dataUsingEncoding:NSUTF8StringEncoding]]; 
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
    if(theConnection) 
    { 
     NSLog(@"Connection Successful"); 

     //receivedData = [[NSMutableData data] retain]; 
    } 
    else 
    { 
     NSLog(@"There was an error: "); 
//  UIAlertView *alert1 = [[[UIAlertView alloc] initWithTitle:@"Alert" message:@"There was an issue sending the data. Please check your internet connection." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease]; 
//  [alert1 show]; 
    } 
} 
@end 

그리고 OrderViewController :

// Interface 
#import <UIKit/UIKit.h> 
#import "OrderModel.h" 


@interface OrderViewController : UITableViewController 
@property (strong, nonatomic) OrderModel* _order; 
@property (strong, nonatomic) id detailItem; 
@end 

// Implementation 
#import "OrderViewController.h" 
#import "OrderModel.h" 
#import "LineItemModel.h" 
#import "LineItemCell.h" 
#import "HUD.h" 
#import "JSONModelLib.h" 
#import "LineItemViewController.h" 


@interface OrderViewController() { 
    OrderModel* _order; 
    NSMutableArray* listOfItems; 
} 
@end 



@implementation OrderViewController 

-(void)viewDidAppear:(BOOL)animated 
{ 
    NSLog(@"View did appear"); 
    // show loader view 
    //[HUD showUIBlockingIndicatorWithText:@"Fetching order"]; 

    //Initialize the array. 
    listOfItems = [[NSMutableArray alloc] init]; 

    NSMutableArray *unPackedArray = [NSMutableArray array]; 
    NSMutableArray *packedArray = [NSMutableArray array]; 
    NSLog(@"ORDER: %@", _order); 
    for(int i = 0; i < _order.line_items.count; i++) { 
     NSLog(@"object in for loop: %@", _order.line_items[i]); 
     LineItemModel *li = _order.line_items[i]; 
     if (li.qty_packed != li.quantity) { 
      [unPackedArray addObject:(LineItemModel *)_order.line_items[i]]; 
     } else { 
      [packedArray addObject:(LineItemModel *)_order.line_items[i]]; 
     } 

    } 
    NSLog(@"unpacked array: %@", unPackedArray); 
    NSLog(@"packed array: %@", packedArray); 

    NSDictionary *unPackedDict = [NSDictionary dictionaryWithObject:unPackedArray forKey:@"LineItems"]; 


    NSDictionary *packedDict = [NSDictionary dictionaryWithObject:packedArray forKey:@"LineItems"]; 

    [listOfItems addObject:unPackedDict]; 
    [listOfItems addObject:packedDict]; 


    // TODO: set the order id from the selected cell here 
    [self.tableView reloadData]; 

    self.navigationItem.title = _order.customer_name; 

} 



- (void)setDetailItem:(id)newDetailItem 
{ 
    NSLog(@"MAKE DETAIL ITEM"); 
    if (_order != newDetailItem) { 
     _order = newDetailItem; 

     // Update the view. 
     [self configureView]; 
    } 

    // show loader view 
    [HUD showUIBlockingIndicatorWithText:@"Fetching order"]; 


    NSString *order_id = _order.id; 
    NSString *url = [NSString stringWithFormat:(NSString *)@"%@/%@.%@", @"http://localhost:3000/api/order", order_id, @"json" ]; 
    _order = [[OrderModel alloc] initFromURLWithString:url completion: ^(JSONModel *model, JSONModelError *err) { 

     // hide loader view 
     [HUD hideUIBlockingIndicator]; 

     [self.tableView reloadData]; 
    }]; 


} 

- (void)configureView 
{ 
    // Update the user interface for the detail item. 

    if (_order) { 
     // self.detailDescriptionLabel.text = [self.detailItem description]; 
    } 
} 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    NSLog(@"View DID LOAD"); 
    [super viewDidLoad]; 

    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
#warning Potentially incomplete method implementation. 
    // Return the number of sections. One for Packed items, one for items not packed. 
    return [listOfItems count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    NSDictionary *dictionary = [listOfItems objectAtIndex:section]; 
    NSArray *array = [dictionary objectForKey:@"LineItems"]; 
    return [array count]; 
} 


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // NSLog(@"ORDER IN LINEITEM CELL: %@", _order); 

    // NSLog(@"LINEITEM: %@", line_item); 

    // New view code with subclass 
    LineItemCell *cell = (LineItemCell *)[tableView dequeueReusableCellWithIdentifier:@"LineItemCell"]; 
    if (!cell) { 
     cell = [[LineItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LineItemCell"]; 
    } 

    // Get the Line Item object for this row and section 
    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section]; 
    NSArray *array = [dictionary objectForKey:@"LineItems"]; 
    LineItemModel* line_item = [array objectAtIndex:indexPath.row]; 


    cell.productLabel.text = line_item.product_title; 
    cell.variantLabel.text = line_item.variant_title; 

    int remaining = line_item.quantity - line_item.qty_packed; 
    cell.remainingLabel.text = [NSString stringWithFormat:@"%i", remaining]; 
    // NSLog(@"LINE ITEM CELL: %@", cell); 
    return cell; 
} 


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

    if(section == 0) 
     return @"Unpacked Items"; 
    else 
     return @"Packed Items"; 
} 


/* 
// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 
*/ 

/* 
// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 
*/ 

/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 

#pragma mark - Table view delegate 
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section]; 
    NSArray *array = [dictionary objectForKey:@"LineItems"]; 
    LineItemModel *li = [array objectAtIndex:indexPath.row]; 
    NSLog(@"Line Item in final: %@", li); 


    LineItemViewController *vc = [segue destinationViewController]; 
    [vc setDetailItem:li]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Navigation logic may go here. Create and push another view controller. 
    /* 
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    */ 

} 



@end 

UPDATE :

#import <UIKit/UIKit.h> 

#import "EvoAppDelegate.h" 

int main(int argc, char *argv[]) 
{ 
    @autoreleasepool { 
     // Breakpoint leads to this line 
     // Thread 1: EXC_BAD_ACCESS(code=1, ...) 
     return UIApplicationMain(argc, argv, nil, NSStringFromClass([EvoAppDelegate class])); 
    } 
} 
+2

정확히 중단 점 설정을 시도하여 EXC_BAD_ACCESS가 어디에서 던져 졌는지 확인할 수 있습니까? – IkegawaTaro

+0

뷰 컨트롤러는 어떻게 제공됩니까? 네비게이션 컨트롤러에서? – Wain

+0

당신의 문제를 잘 설명 할 수 있도록 +1을 드리겠습니다. 예외의 원인을 추적하기 위해 몇 가지 중단 점을 설정해야하며 오류 메시지의 원인을 추적해야합니다. –

답변

0

시도 링크 해제 : 모든 예외에 대한 중단 점을 설정 한 후 추적이 날 리드 여기

내 LineItemViewController입니다 delegatedatasource은 IB 및 UITableViewdelegatedatasourceOrderViewController에 프로그래밍 방식으로 설정하면

+0

I think * 델리게이트와 데이터 소스의 연결을 끊었다.나는 스토리 보드 파일을 열고 "Reference Outlets"아래에 가서 데이터 소스와 델리게이트를위한 두 개의 엔트리를 삭제함으로써 이것을했다. 프로그래밍 방식으로 설정하는 방법을 잘 모르는 경우 약 15 분 동안 시도했지만 완료하는 방법을 모릅니다. 도와 주셔서 감사합니다! – Chris

+0

viewWillAppear에서 설정을 시도 할 수 있습니다 (이 설정을 재정의해야합니다). tableview.datasource = self를 사용하여 설정하십시오. tableview.delegate = 자기; – IkegawaTaro

+0

Google에서 작동하지 않는 경우 내장 된 좀비 진단 도구를 사용하는 방법을 검색하십시오. 할당 해제 된 메모리의 정확한 부분을 찾는 데 사용될 수 있습니다. – IkegawaTaro