UISegmentedControl 导致"attempt to insert nil object"错误



我的视图控制器中有两个UISegmentedControls,单击提交按钮后,我收到下面的错误。我相信无论是一个UISegmentedControls,或两个文本字段在我的故事板没有正确发送他们的数据。然而,我似乎无法弄清楚是什么导致了这一点。

错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[4]'
*** First throw call stack:
(
    0   CoreFoundation                      0x02a7a1e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x026388e5 objc_exception_throw + 44
    2   CoreFoundation                      0x02a40376 -[__NSPlaceholderDictionary initWithObjects:forKeys:count:] + 390
    3   CoreFoundation                      0x02a6dc29 +[NSDictionary dictionaryWithObjects:forKeys:count:] + 73
    4   Parse+Storyboard                    0x000055db -[CriteriaViewController submitButton:] + 395
    5   libobjc.A.dylib                     0x0264a880 -[NSObject performSelector:withObject:withObject:] + 77
    6   UIKit                               0x012fa3b9 -[UIApplication sendAction:to:from:forEvent:] + 108
    7   UIKit                               0x012fa345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    8   UIKit                               0x013fbbd1 -[UIControl sendAction:to:forEvent:] + 66
    9   UIKit                               0x013fbfc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
    10  UIKit                               0x013fb243 -[UIControl touchesEnded:withEvent:] + 641
    11  UIKit                               0x01339ddd -[UIWindow _sendTouchesForEvent:] + 852
    12  UIKit                               0x0133a9d1 -[UIWindow sendEvent:] + 1117
    13  UIKit                               0x0130c5f2 -[UIApplication sendEvent:] + 242
    14  UIKit                               0x012f6353 _UIApplicationHandleEventQueue + 11455
    15  CoreFoundation                      0x02a0377f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    16  CoreFoundation                      0x02a0310b __CFRunLoopDoSources0 + 235
    17  CoreFoundation                      0x02a201ae __CFRunLoopRun + 910
    18  CoreFoundation                      0x02a1f9d3 CFRunLoopRunSpecific + 467
    19  CoreFoundation                      0x02a1f7eb CFRunLoopRunInMode + 123
    20  GraphicsServices                    0x02cd75ee GSEventRunModal + 192
    21  GraphicsServices                    0x02cd742b GSEventRun + 104
    22  UIKit                               0x012f8f9b UIApplicationMain + 1225
    23  Parse+Storyboard                    0x000024ed main + 141
    24  libdyld.dylib                       0x038d4701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

CriteriaViewController.m:

#import "CriteriaViewController.h"
@interface CriteriaViewController ()
@property (weak, nonatomic) IBOutlet UISegmentedControl *itemConditionSegment;
@property (weak, nonatomic) IBOutlet UISegmentedControl *itemLocationSegment;

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

- (void)viewDidLoad
{        
    [super viewDidLoad];
    UISegmentedControl *conditionSegmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Only New", @"Any", nil]];
    conditionSegmentedControl.frame = CGRectMake(87, 190, 157, 30);
    conditionSegmentedControl.selectedSegmentIndex = 0;
    conditionSegmentedControl.tintColor = [UIColor blackColor];
    [conditionSegmentedControl addTarget:self action:@selector(ConditionSegmentControlAction:) forControlEvents: UIControlEventValueChanged];
    [self.view addSubview:conditionSegmentedControl];

    UISegmentedControl *locationSegmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Fast Shipping", @"Large Selection", nil]];
    locationSegmentedControl.frame = CGRectMake(67, 275, 200, 301);
    locationSegmentedControl.selectedSegmentIndex = 0;
    locationSegmentedControl.tintColor = [UIColor blackColor];
    [locationSegmentedControl addTarget:self action:@selector(LocationSegmentControlAction:) forControlEvents: UIControlEventValueChanged];
    [self.view addSubview:locationSegmentedControl];

    // Submit button
    UIButton *submitButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // Create Round Rect Type button.
    submitButton.frame = CGRectMake(100, 100, 100, 100); // define position and width and height for the button.
    [submitButton setTitle:@"Submit" forState:UIControlStateNormal];
    [submitButton addTarget:self action:@selector(submitButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:submitButton];

}

- (void)ConditionSegmentControlAction:(UISegmentedControl *)segment
{
    if(segment.selectedSegmentIndex == 0)
    {
        // set condition to new
        self.itemCondition = @"new";
    }
    else if (segment.selectedSegmentIndex == 1)
    {
        // set condition to all
        self.itemCondition = @"all";
    }
}
- (void)LocationSegmentControlAction:(UISegmentedControl *)segment
{
    if(segment.selectedSegmentIndex == 0)
    {
        // set location to us
        self.itemLocation = @"US";
    }
    else if (segment.selectedSegmentIndex == 1)
    {
        // set clocation to worldwide
        self.itemLocation = @"Worldwide";
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//add all the info to users respective new category object
- (IBAction)submitButton:(id)sender
{
    //if (self.minPrice.text.length > 0 && self.maxPrice.text.length > 0) {
        [PFCloud callFunctionInBackground:@"userCategorySave"
                           withParameters:@{@"categoryId": self.chosenCategory,
                                              @"minPrice": self.minPrice,
                                              @"maxPrice": self.maxPrice,
                                         @"itemCondition": self.itemCondition,
                                          @"itemLocation": self.itemLocation}
                                         block:^(NSString *result, NSError *error) {
                                             if (!error) {
                                                 NSLog(@"Criteria successfully saved.");
                                                     [self performSegueWithIdentifier:@"SearchCategoryChooserToMatchCenterSegue" sender:self];
                                             }
                                         }];
    //}
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
@end
@{@"categoryId": self.chosenCategory,
@"minPrice": self.minPrice,
@"maxPrice": self.maxPrice,
@"itemCondition": self.itemCondition,
@"itemLocation": self.itemLocation}

这是一个创建NSDictionary的文字构造。编译器将其转换为对initWithObjects:forKeys:count的调用。方法坚持要传递的所有对象都存在—否则将抛出您所看到的断言。您可以在调用堆栈中看到事件序列-

+[NSDictionary dictionaryWithObjects:forKeys:count:] + 73

-[CriteriaViewController submitButton:] + 395

submitButton被调用,在那里它试图创建一个字典,这崩溃了。

如果相关数据不存在,可以更改accessor方法以返回NSNull对象,或者确保您实际上有/lazy load所有这些值的数据

最新更新