UISegmentedControl导致“尝试插入nil对象”错误

时间:2014-05-15 15:58:57

标签: ios objective-c

我的视图控制器中有两个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

1 个答案:

答案 0 :(得分:0)

@{@"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,在那里它试图创建一个崩溃的字典。

如果相关数据不存在,可以更改访问者方法以返回NSNull个对象,或者确保实际上有/所有这些值的延迟加载数据。