' NSInvalidArgumentException',原因:' *** - [__ NSPlaceholderDictionary initWithObjects:forKeys:count:]:尝试从对象中插入nil对象[3]'

时间:2014-05-27 19:51:31

标签: ios objective-c uitextfield

我的故事板中有两个UITextField,用于输入用户输入的数字,minPrice和maxPrice。当我在模拟器中单击提交时,应用程序崩溃,我收到以下错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[3]'
*** First throw call stack:
(
    0   CoreFoundation                      0x02a881e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x026468e5 objc_exception_throw + 44
    2   CoreFoundation                      0x02a4e376 -[__NSPlaceholderDictionary initWithObjects:forKeys:count:] + 390
    3   CoreFoundation                      0x02a7bc29 +[NSDictionary dictionaryWithObjects:forKeys:count:] + 73
    4   Parse+Storyboard                    0x0000b5b5 -[CriteriaViewController submitButton:] + 757
    5   libobjc.A.dylib                     0x02658880 -[NSObject performSelector:withObject:withObject:] + 77
    6   UIKit                               0x013083b9 -[UIApplication sendAction:to:from:forEvent:] + 108
    7   UIKit                               0x01308345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    8   UIKit                               0x01409bd1 -[UIControl sendAction:to:forEvent:] + 66
    9   UIKit                               0x01409fc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
    10  UIKit                               0x01409243 -[UIControl touchesEnded:withEvent:] + 641
    11  UIKit                               0x01347ddd -[UIWindow _sendTouchesForEvent:] + 852
    12  UIKit                               0x013489d1 -[UIWindow sendEvent:] + 1117
    13  UIKit                               0x0131a5f2 -[UIApplication sendEvent:] + 242
    14  UIKit                               0x01304353 _UIApplicationHandleEventQueue + 11455
    15  CoreFoundation                      0x02a1177f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    16  CoreFoundation                      0x02a1110b __CFRunLoopDoSources0 + 235
    17  CoreFoundation                      0x02a2e1ae __CFRunLoopRun + 910
    18  CoreFoundation                      0x02a2d9d3 CFRunLoopRunSpecific + 467
    19  CoreFoundation                      0x02a2d7eb CFRunLoopRunInMode + 123
    20  GraphicsServices                    0x02ce55ee GSEventRunModal + 192
    21  GraphicsServices                    0x02ce542b GSEventRun + 104
    22  UIKit                               0x01306f9b UIApplicationMain + 1225
    23  Parse+Storyboard                    0x000020ed main + 141
    24  libdyld.dylib                       0x038e2701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

CriteriaViewController.h:

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "SearchViewController.h"

@interface CriteriaViewController : UIViewController{

IBOutlet UISegmentedControl *Segment;
}


//@property (nonatomic) IBOutlet UITextField *itemSearch;
@property (weak, nonatomic) IBOutlet UITextField *minPrice;
@property (weak, nonatomic) IBOutlet UITextField *maxPrice;
@property (nonatomic, copy) NSNumber *chosenCategory;
@property (weak, nonatomic) NSString *itemCondition;
@property (weak, nonatomic) NSString *itemLocation;
@property (weak, nonatomic) NSString *itemSearch;

@end

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];

    // Condition UISegment
    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];


    // Location UISegment
    UISegmentedControl *locationSegmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Fast Shipping", @"Large Selection", nil]];
    locationSegmentedControl.frame = CGRectMake(67, 275, 200, 30);
    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 {



    [PFCloud callFunctionInBackground:@"addToMatchCenter"
                       withParameters:@{
//                                        @"searchTerm": self.itemSearch,
                                        @"categoryId": self.chosenCategory,
//                                        @"minPrice": self.minPrice,
//                                        @"maxPrice": self.maxPrice,
//                                        @"itemCondition": self.itemCondition,
//                                        @"itemLocation": self.itemLocation,
                                        }
                                block:^(NSString *result, NSError *error) {

                                    if (!error) {
                                        NSLog(@"'%@'", result);
                                    }
                                }];

    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

}



@end

Cloud Code:

Parse.Cloud.define("userCategorySave", function(request, response) {


  var userCategory = Parse.Object.extend("userCategory");
  var newUserCategory = new userCategory();
      newUserCategory.set("categoryId", request.params.categoryId);
      newUserCategory.set("minPrice", request.params.minPrice);
      newUserCategory.set("maxPrice", request.params.maxPrice);
      newUserCategory.set("itemCondition", request.params.itemCondition);
      newUserCategory.set("itemLocation", request.params.itemLocation);
      newUserCategory.set("parent", Parse.User.current());

      newUserCategory.save({ 

        success: function (){
          console.log ('userCategory successfully created!');
          response.success('userCategory successfully created!');
        },

        error: function (){
          console.log('error!!!');
        response.error('Request failed');
        }

      });
});

我认为问题是UITextFields以我的云代码不喜欢的格式发送数字。除了这个错误,我想设置它,以便在此字段中输入值时只允许数字。

如何以编程方式创建这两个字段,以便以与云代码兼容的格式发送数据,并且只允许数字?

2 个答案:

答案 0 :(得分:10)

你的一个属性 - 看起来像itemCondition - 显然是零。这可能是因为您的属性较弱,因此变量在您有机会查看之前就会被删除,或者可能是因为您选择的细分不是您要测试的值之一因此变量无论何时设定。我无法理解为什么这些字符串属性很弱,我强烈怀疑它们不应该是。

答案 1 :(得分:0)

如果是关于NSDictionary ......

NSDictionary *dic = @{@"id":ID,
                      @"condition":condition,
                      @"pageNo":[NSNumber numberWithInt:pageNo],
                      @"pageSize":[NSNumber numberWithInt:pageSize]};

这不好。如果任何对象为零,则会出现问题。

NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:
                         ID,@"id",
                         condition,@"condition",
                         [NSNumber numberWithInt:pageNo],@"pageNo",
                         [NSNumber numberWithInt:pageSize],@"pageSize",
                         nil];

这是对的。