2016-06-20 1 views
1

사용자가 열려있는 바구니를 가지고 있는지 확인하고 싶으면 메뉴를 보낸 다음 열려있는 바구니보기로 보내면 오류가 발생합니다. 제 질문은이 문제를 없애는 방법입니다. 내 논리가 맞습니까? 버튼 addToOrderButtonTapped가 menuview"인스턴스로 보낸 인식 할 수없는 선택기"를 디버깅하는 방법

[[OrderManager sharedManager]getBasketsForMerchant:merchant success:^(NSArray *baskets) { 
     for (Basket *basket in baskets){ 
      if ([basket.status isEqualToString:kBasketStatusOpen]) { 
       [self.openBaskets addObject:basket]; 
      } 
     }if (self.openBaskets.count > 0){ 
      self.openBaskets = self.openBaskets; 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       [self performSegueWithIdentifier:kSegueMenu sender:self]; 
      }); 
     } 
     else { 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       [self performSegueWithIdentifier:KSegueOpenBasket sender:self]; 
      }); 
     } 
    } failure:^(NSError *error, NSHTTPURLResponse *response) { 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      NSString *localizedString = NSLocalizedString(@"Order.ErrorLoadingOpenOrders", "Get error message"); 
      [self showMessage:localizedString withTitle:MessageTypeError]; 
      [MBProgressHUD hideAllHUDsForView:self.view animated:YES]; 
     }); 
    }]; 

menuview 언론이 때 열려있는 바구니를 확인하기 위해이 방법을 사용

'NSInvalidArgumentException', reason: '-[Merchant merchantId]: unrecognized selector sent to instance 0x7fad19529490'. 

메신저

// 
// MenuItemViewController.m 
// BaseApp 
// 

    #import "MenuItemViewController.h" 
    #import "RoundedButton.h" 
    #import "MenuOptionCell.h" 
    #import "MenuItem.h" 
    #import "MenuOptionButton.h" 
    #import "MenuChoice.h" 
    #import "OrderManager.h" 
    #import "TypeOfChoiceCell.h" 
    #import "MenuOption.h" 
    #import "OrderPricingTableViewCell.h" 
    #import "RKManagedObjectStore.h" 
    #import "NSManagedObjectContext+RKAdditions.h" 
    #import "MerchantManager.h" 
    #import "Merchant.h" 
    #import "OrdersViewController.h" 
    #import "MenuItem.h" 
    #import "MenuViewController.h" 
    #import "OrderConfirmationViewController.h" 
    #import "OrderManager.h" 
    #import "APIClient.h" 
    #import "MenuManager.h" 
    #import "DiscoverViewController.h" 
    #import "DiscoverCollectionViewCell.h" 
    #import "MerchantManager.h" 
    #import "MenuViewController.h" 
    #import "MenuManager.h" 
    #import "MerchantDetailsViewController.h" 
    #import "OpenOrdersViewController.h" 

    typedef NS_ENUM(NSUInteger, MenuOptionsSection) { 
MenuOptionsSectionRequired = 0, 
MenuOptionsSectionOptional = 1 
}; 
static NSString *const OpenBasketsSegue = @"OpenBaskets"; 
static NSString *const kSegueMenu = @"ShowMenu"; 
static NSString *const KSegueOpenBasket = @"OpenBaskets"; 
@interface MenuItemViewController() <UITableViewDelegate, UITableViewDataSource> 

    @property (nonatomic, weak) IBOutlet UILabel *menuItemName; 
    @property (nonatomic, weak) IBOutlet UILabel *quantityLabel; 
    @property (nonatomic, weak) IBOutlet UILabel *menuItemPrice; 
    @property (nonatomic, weak) IBOutlet UILabel *subTotalLabel; 
    @property (nonatomic, weak) IBOutlet UITextView *menuItemDescription; 
    @property (nonatomic, weak) IBOutlet RoundedButton *addToOrderButton; 
    @property (nonatomic, weak) IBOutlet UITableView *tableView; 
    @property (nonatomic, weak) IBOutlet UIView *cartView; 
    @property (nonatomic, weak) IBOutlet UIButton *subtractButton; 
    @property (nonatomic) NSDecimalNumber *temporarySubtotal; 
    @property (nonatomic, strong) NSMutableArray<MenuOption *> *requiredChoices; 
    @property (nonatomic, strong) NSMutableArray<MenuOption *> *optionalChoices; 
    @property (nonatomic, strong) NSMutableArray<NSMutableArray *> *optionChoicesSection; 
    @property (nonatomic, strong) NSMutableArray<NSDictionary *> *options; 
    @property (nonatomic, strong) NSMutableArray <Basket *>*openBaskets; 
    @property (nonatomic) BOOL launchViewFirstTime; 

    @end 

    @implementation MenuItemViewController 


    #pragma - Lifecycle 
- (void)viewDidLoad { 
[super viewDidLoad]; 
self.navigationItem.title = @"Menu Item"; 
self.menuItemName.text = self.menuCategory.selectedBasketLine.menuItem.name; 
self.menuItemPrice.text = [NSString stringWithFormat:@"%@", [Basket formattedCurrencyStringForAmount:self.menuCategory.selectedBasketLine.menuItem.price]]; 
self.quantityLabel.text = self.menuCategory.selectedBasketLine.quantity.stringValue; 
self.temporarySubtotal = [self calculateTemporarySubtotal:[OrderManager sharedManager].currentBasket.subtotal menuItemPrice:self.menuCategory.selectedBasketLine.menuItem.price]; 
[self setSubtotalText:self.temporarySubtotal]; 
self.menuItemDescription.text = self.menuCategory.selectedBasketLine.menuItem.menuItemDescription; 
self.subtractButton.alpha = 0.65; 
self.requiredChoices = [[NSMutableArray alloc] init]; 
self.optionalChoices = [[NSMutableArray alloc] init]; 
self.optionChoicesSection = [[NSMutableArray alloc] init]; 
self.options = [[NSMutableArray alloc] init]; 
[self initializeChoiceArrays]; 
self.tableView.delegate = self; 
self.tableView.dataSource = self; 
    } 

    - (void)viewWillAppear:(BOOL)animated { 
[super viewWillAppear:animated]; 
self.launchViewFirstTime = YES; 
if (self.optionChoicesSection.count > 0) { 
    [self.tableView reloadData]; 
     } else { 
    self.tableView.hidden = YES; 
     } 
     } 

    - (void)viewDidAppear:(BOOL)animated { 
[super viewDidAppear:animated]; 
self.launchViewFirstTime = NO; 
    } 

    - (void)viewDidDisappear:(BOOL)animated { 
[super viewDidDisappear:animated]; 
for (MenuOption *menuOption in self.requiredChoices) { 
    [menuOption resetNumberOfChoicesSelected]; 
     } 
for (MenuOption *menuOption in self.optionalChoices) { 
    [menuOption resetNumberOfChoicesSelected]; 
     } 
self.menuCategory = nil; 
    } 

    - (void)viewDidLayoutSubviews { 
[super viewDidLayoutSubviews]; 
[self.menuItemDescription setContentOffset:CGPointZero animated:YES]; 
    } 


    #pragma - IBActions 
    - (IBAction)addButtonTapped:(id)sender { 
NSInteger count = self.quantityLabel.text.integerValue; 
count++; 
if (count > 1) { 
    self.subtractButton.alpha = 1; 
} 
NSDecimalNumber *newSubTotal = [self.temporarySubtotal decimalNumberByAdding:self.menuCategory.selectedBasketLine.menuItem.price]; 
[self modifyCurrentBasketSubtotal:newSubTotal quantity:count]; 
    } 

    - (IBAction)subtractButtonTapped:(id)sender { 
NSInteger count = self.quantityLabel.text.integerValue; 
if (count > 1) { 
    count--; 
    NSDecimalNumber *newSubTotal = [self.temporarySubtotal decimalNumberBySubtracting:self.menuCategory.selectedBasketLine.menuItem.price]; 
    [self modifyCurrentBasketSubtotal:newSubTotal quantity:count]; 
    if (count == 1) { 
     self.subtractButton.alpha = 0.65; 
    } 
    } 
    } 

    - (IBAction)addToOrderButtonTapped:(id)sender { 
MenuOption *menuOption; 
Merchant *merchant = [[Merchant alloc]init]; 
// First check if there are any missing required options that have to be selected 
if ((menuOption = [self checkMissingRequiredOptionsHaveBeenSelected])) { 
    NSString *localizedString = NSLocalizedString(@"AddMenuItem.RequiredChoicesNotSelected", @"Get string for error"); 
    NSString *formattedString = [NSString stringWithFormat:localizedString, menuOption.name]; 
    [self showMessage:formattedString withTitle:MessageTypeError]; 
    return; 
} 
// Now check if the minimum and maximum choice seletion have been met for the menu options 
if ((menuOption = [self validateMinMaxForMenuOptionsHaveBeenMet])) { 
    NSString *localizedString = NSLocalizedString(@"AddMenuItem.MinMaxNotFulfilled", @"Get string for error"); 
    NSString *formattedString = [NSString stringWithFormat:localizedString, menuOption.name]; 
    [self showMessage:formattedString withTitle:MessageTypeError]; 
    return; 
    } 
// Add the menu item to the basket 
    if (self.menuItemAddedBlock) { 
    self.menuItemAddedBlock(self, self.menuCategory); 
    // [self dismissViewControllerAnimated:YES completion:nil]; 

    //checking for open basket here 
    [[OrderManager sharedManager]getBasketsForMerchant:merchant success:^(NSArray *baskets) { 
     for (Basket *basket in baskets){ 
      if ([basket.status isEqualToString:kBasketStatusOpen]) { 
       [self.openBaskets addObject:basket]; 
      } 
     }if (self.openBaskets.count > 0){ 
      self.openBaskets = self.openBaskets; 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       [self performSegueWithIdentifier:kSegueMenu sender:self]; 
      }); 
     } 
     else { 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       [self performSegueWithIdentifier:KSegueOpenBasket sender:self]; 
      }); 
     } 
    } failure:^(NSError *error, NSHTTPURLResponse *response) { 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      NSString *localizedString = NSLocalizedString(@"Order.ErrorLoadingOpenOrders", "Get error message"); 
      [self showMessage:localizedString withTitle:MessageTypeError]; 
      [MBProgressHUD hideAllHUDsForView:self.view animated:YES]; 
     }); 
    }]; 


    } 
    } 

    - (IBAction)cancelButtonTapped:(UIBarButtonItem *)sender { 
[self dismissViewControllerAnimated:YES completion:nil]; 
    } 


     #pragma - UITableViewDelegate, UITableViewDataSource 
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return self.optionChoicesSection.count; 
    } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
return self.optionChoicesSection[section].count; 
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
MenuOptionCell *cell = (MenuOptionCell *)[tableView dequeueReusableCellWithIdentifier:[MenuOptionCell reuseIdentifier]]; 
cell.menuOption = self.optionChoicesSection[indexPath.section][indexPath.row]; 
cell.menuOptionCellButtonPressedBlock = ^(MenuOptionButton *button, MenuOption *option, MenuChoice *choice) { 
    [self adjustSelectedOptions:option choice:choice indexPath:indexPath]; 
}; 
cell.menuOptionCellDefaultOptionDetectedBlock = ^(MenuOptionButton *button, MenuOption *option, MenuChoice *choice) { 
    [self adjustSelectedOptions:option choice:choice indexPath:indexPath]; 
    }; 
cell.menuOptionCellDeselectedBlock = ^(MenuOptionButton *button, MenuOption *option, MenuChoice *choice) { 
    [self adjustSelectedOptions:option choice:choice indexPath:indexPath]; 
     }; 
[cell configureCell:self.launchViewFirstTime]; 
return cell; 
    } 

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
TypeOfChoiceCell *cell = (TypeOfChoiceCell *)[tableView dequeueReusableCellWithIdentifier:[TypeOfChoiceCell reuseIdentifier]]; 
switch ((MenuOptionsSection)section) { 
    case MenuOptionsSectionRequired: 
     if (self.requiredChoices.count > 0) { 
      cell.title = NSLocalizedString(@"AddMenuItem.RequiredChoiceText", @"Get string for title"); 
      return cell; 
     } else if (self.optionalChoices.count > 0) { 
      cell.title = NSLocalizedString(@"AddMenuItem.OptionalChoiceText", @"get string for title"); 
      return cell; 
     } 
    case MenuOptionsSectionOptional: 
     if (self.optionalChoices.count > 0) { 
      cell.title = NSLocalizedString(@"AddMenuItem.OptionalChoiceText", @"Get string for title"); 
      return cell; 
     } 
     } 
return nil; 
     } 

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
MenuOption *option = self.optionChoicesSection[indexPath.section][indexPath.row]; 
return [MenuOptionCell heightForMenuOption:option]; 
     } 

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
return [TypeOfChoiceCell height]; 
     } 

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { 
return 0.01f; 
     } 


     #pragma - Helpers 
    - (void)setSubtotalText:(NSDecimalNumber *)subtotal { 
if (!subtotal) { 
    self.subTotalLabel.text = NSLocalizedString(@"AddMenuItem.SubtotalDefault", @"Get string for subtotal"); 
} else { 
    NSString *localizedString = NSLocalizedString(@"AddMenuItem.SubtotalFormatted", @"Get string for subtotal"); 
    self.subTotalLabel.text = [NSString stringWithFormat:localizedString, [Basket formattedCurrencyStringForAmount:subtotal]]; 
     } 
     } 

    - (void)modifyCurrentBasketSubtotal:(NSDecimalNumber *)subtotal quantity:(NSInteger)quantity { 
self.menuCategory.selectedBasketLine.quantity = [NSNumber numberWithInteger:quantity]; 
self.menuCategory.selectedBasketLine.subtotal = subtotal; 
self.temporarySubtotal = subtotal; 
[self setSubtotalText:subtotal]; 
self.quantityLabel.text = self.menuCategory.selectedBasketLine.quantity.stringValue; 
    } 

    - (NSDecimalNumber *)calculateTemporarySubtotal:(NSDecimalNumber *)orderManagerSubTotal menuItemPrice:(NSDecimalNumber *)menuItemPrice { 
if (orderManagerSubTotal == 0) { 
    return menuItemPrice; 
} else { 
    return [orderManagerSubTotal decimalNumberByAdding:menuItemPrice]; 
     } 
    } 

    - (MenuOption *)checkMissingRequiredOptionsHaveBeenSelected { 
for (MenuOption *menuOption in self.requiredChoices) { 
    if (!menuOption.selected) { 
     return menuOption; 
    } 
    } 
return nil; 
    } 

    - (MenuOption *)validateMinMaxForMenuOptionsHaveBeenMet { 
for (MenuOption *menuOption in self.requiredChoices) { 
    if ([menuOption validateIndividualMinMaxForMenuOption]) { 
     return menuOption; 
    } 
} 
for (MenuOption *menuOption in self.optionalChoices) { 
    if (menuOption.selected) { 
     if ([menuOption validateIndividualMinMaxForMenuOption]) { 
      return menuOption; 
     } 
     } 
     } 
    return nil; 
     } 

    - (void)initializeChoiceArrays { 
NSArray<MenuOption *> *menuOptions = [self.menuCategory.selectedBasketLine.menuItem.menuOptions allObjects]; 
for (MenuOption *menuOption in menuOptions) { 
    if (menuOption.minimumChoices == nil) { 
     menuOption.minimumChoices = [NSNumber numberWithInt:0]; 
    } 
    if (menuOption.maximumChoices == nil) { 
     menuOption.maximumChoices = [NSNumber numberWithInt:0]; 
    } 
    // For now make an optional choice required if minimumChoices > 0 
    if (menuOption.isRequired || [menuOption.minimumChoices intValue] > 0) { 
     menuOption.isRequired = YES; 
     [self.requiredChoices addObject:menuOption]; 
    } else { 
     [self.optionalChoices addObject:menuOption]; 
    } 
    } 
if (self.requiredChoices.count > 0) { 
    [self.optionChoicesSection addObject:self.requiredChoices]; 
    } 
if (self.optionalChoices.count > 0) { 
    [self.optionChoicesSection addObject:self.optionalChoices]; 
    } 
    } 

    - (void)adjustSelectedOptions:(MenuOption *)option choice:(MenuChoice *)choice indexPath:(NSIndexPath *)indexPath { 
self.menuCategory.selectedBasketLine.subtotal = self.menuCategory.selectedBasketLine.menuItem.price; 
if (option.selected && option.menuChoices.count == 0) { 
    [self.options addObject:@{kOption: option.menuOptionId ?: @"", kChoice: @""}]; 
    if (option.price) { 
     self.temporarySubtotal = [self.temporarySubtotal decimalNumberByAdding:option.price]; 
    } 
    if (option.isRequired) { 
     self.requiredChoices[indexPath.row].selected = option.selected; 
     self.requiredChoices[indexPath.row].numberOfChoicesSelected = option.numberOfChoicesSelected; 
    } else { 
     self.optionalChoices[indexPath.row].selected = option.selected; 
     self.optionalChoices[indexPath.row].numberOfChoicesSelected = option.numberOfChoicesSelected; 
    } 
    } else { 
    if (option.menuChoices.count == 0) { 
     [self.options removeObject:@{kOption: option.menuOptionId ?: @"", kChoice: @""}]; 
     if (option.price) { 
      self.temporarySubtotal = [self.temporarySubtotal decimalNumberBySubtracting:option.price]; 
     } 
     if (option.isRequired) { 
      self.requiredChoices[indexPath.row].selected = option.selected; 
      self.requiredChoices[indexPath.row].numberOfChoicesSelected = option.numberOfChoicesSelected; 
     } else { 
      self.optionalChoices[indexPath.row].selected = option.selected; 
      self.optionalChoices[indexPath.row].numberOfChoicesSelected = option.numberOfChoicesSelected; 
     } 
     } 
     } 
if (choice.selected && option.menuChoices.count > 0) { 
    [self.options addObject:@{kOption: choice.menuOption.menuOptionId ?: @"", kChoice: choice.menuChoiceId ?: @""}]; 
    if (choice.price) { 
     self.temporarySubtotal = [self.temporarySubtotal decimalNumberByAdding:choice.price]; 
    } 
    if (option.isRequired) { 
     self.requiredChoices[indexPath.row].selected = choice.selected; 
     self.requiredChoices[indexPath.row].numberOfChoicesSelected = option.numberOfChoicesSelected; 
    } else { 
     self.optionalChoices[indexPath.row].selected = choice.selected; 
     self.optionalChoices[indexPath.row].numberOfChoicesSelected = option.numberOfChoicesSelected; 
     } 
    } else { 
    if (option.menuChoices.count > 0) { 
     [self.options removeObject:@{kOption: choice.menuOption.menuOptionId ?: @"", kChoice: choice.menuChoiceId ?: @""}]; 
     if (choice.price) { 
      self.temporarySubtotal = [self.temporarySubtotal decimalNumberBySubtracting:choice.price]; 
     } 
     if (option.isRequired) { 
      if ([option.numberOfChoicesSelected intValue] == 0) { 
       self.requiredChoices[indexPath.row].selected = option.selected; 
      } else { 
       self.requiredChoices[indexPath.row].selected = !choice.selected; 
      } 
      self.requiredChoices[indexPath.row].numberOfChoicesSelected = option.numberOfChoicesSelected; 
     } else { 
      self.optionalChoices[indexPath.row].selected = choice.selected; 
      self.optionalChoices[indexPath.row].numberOfChoicesSelected = option.numberOfChoicesSelected; 
     } 
    } 
    } 
    [self setSubtotalText:self.temporarySubtotal]; 
self.menuCategory.selectedBasketLine.attributes = self.options; 
    } 

    @end 

이 코드 줄이 나에게주는 이잖아입니다 오류

+ (void)getBasketsForMerchant:(Merchant *)merchant success:(void (^)(NSArray *basket))success failure:(void (^)(NSError *, NSHTTPURLResponse *))failure { 
NSMutableDictionary* params = @{@"expand": @"merchant"}.mutableCopy; 
if (merchant) { 
    params[@"merchant"] = merchant.merchantId; 
} 
[[RKObjectManager sharedManager] getObjectsAtPath:kOrdersEndpoint parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult){ 
    NSArray* items = mappingResult.array; 
    if (success) { 
    success(items); 
    } 
} failure:^(RKObjectRequestOperation *operation, NSError *error) { 
    if (failure) { 
     failure(error, operation.HTTPRequestOperation.response); 
    } else { 
     _defaultFailureBlock(operation, error); 
    } 
}]; 
} 
+2

질문에 코드가 너무 많습니다. 관심있는 부분 만 붙여 넣어야합니다. – AnthoPak

+1

괜찮습니다. 편집 해 드리겠습니다. –

+0

오류로 인해 Xcode의 디버거에서 한 줄이 강조 표시됩니까? @AnthoninC 이후 어떤 행을 트리거하는지 아는 것이 유용 할 것입니다. 우리가 작업 할 수있는 많은 양의 코드가 있습니다. 강조 표시된 행이 없으면이 메서드와 같은 모든 예외에 중단 점을 추가하십시오. [link] (http://stackoverflow.com/a/29486686/4191758) –

답변

3

이 오류 :

'NSInvalidArgumentException', reason: '-[Merchant merchantId]: unrecognized selector sent to instance 0x7fad19529490'. 

당신이 위치 0x7fad19529490에서 객체가 있고 그 위에 "MERCHANTID"를 호출하는 노력을 의미한다. 해당 개체가 merchantId에 응답하지 않습니다.

그래서 Merchant의 정의를 매우주의 깊게 살펴보십시오. merchantId의 맞춤법을 fieldMappings에 바로 입력 했습니까? 자본 D가 merchantID입니까?

가 판매자의 이름이 merchantId 인 것으로 확신하는 경우 0x7fad19529490의 상품이 판매자가 아니라는 것이 다음에 들릴 가능성이 높습니다.

가장 쉬운 방법은 중단 점 탐색기의 맨 아래에 예외 중단 점 (+를 사용)을 추가하는 것입니다. 이 예외가 발생하면 포인터 주소를 오류에서 잡아 (매번 다를 수 있음) 디버거에서 무엇이 있는지 확인하십시오. 이렇게하려면 (lldb) 프롬프트에서 다음을 입력하십시오.

po (NSObject*)(0x7fad19529490) 

오류가있는 주소를 사용하십시오. 당신이 description를 오버라이드 (override) 한 경우, 그것은 그 출력 될 것

<Merchant: 0x7fad19529490> 

또는 : 그것은 상인의 ​​경우에, 나는 뭔가처럼 말을 기대합니다.

+1

오브젝트가 '상인'이 아니면 예외 이유는 오브젝트가 실제로 어떤 클래스이든 "상인"이 아니라고 말할 것입니다. – dan

+0

좋습니다. –

+0

그래서 단지 PO이 (lldb)을 얻었다 0x7fd3c50b1500 <판매자 : 0x7fd3c50b1500> (엔티티 : ; ID (NULL); 데이터 : ) <판매자 : 0x7fd3c50b1500> (엔티티 : ; ID (NULL) ; data : ) –

관련 문제