ios – NSPlaceholderDictionary:尝试从对象中插入nil对象[1]

ios – NSPlaceholderDictionary:尝试从对象中插入nil对象[1],第1张

概述我有两个UITextField框,一个用于minPrice,另一个用于maxPrice.当我尝试运行submitButton IBAction触发的函数时,使用这两个文本字段内容作为参数,它声明对象为空,并出现以下错误: ‘*** – [__ NSPlaceholderDictionary initWithObjects:forKeys:count:]:尝试从对象[1]中插入nil对象 我似乎无法 我有两个UITextFIEld框,一个用于minPrice,另一个用于maxPrice.当我尝试运行submitbutton IBAction触发的函数时,使用这两个文本字段内容作为参数,它声明对象为空,并出现以下错误:

‘*** – [__ NSPlaceholderDictionary initWithObjects:forKeys:count:]:尝试从对象[1]中插入nil对象

我似乎无法弄清楚为什么输入这些字段的文本没有正确保存.当我以编程方式初始化它时,我忘记了什么吗?

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 (nonatomic,copy) Nsstring *chosencategoryname;@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];    [self addMinTextFIEld];    [self addMaxTextFIEld];    // 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); // 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)addMinTextFIEld{    // This allocates a label    UILabel *prefixLabel = [[UILabel alloc]initWithFrame:CGRectZero];    // This sets the Font for the label    [prefixLabel setFont:[UIFont boldSystemFontOfSize:14]];    // This fits the frame to size of the text    [prefixLabel sizetoFit];    // This allocates the textfIEld and sets its frame    UITextFIEld *minPrice = [[UITextFIEld  alloc] initWithFrame:                             CGRectMake(70,105,75,30)];    // This sets the border style of the text fIEld    minPrice.borderStyle = UITextborderStyleRoundedRect;    minPrice.contentVerticalAlignment =    UIControlContentVerticalAlignmentCenter;    [minPrice setFont:[UIFont boldSystemFontOfSize:12]];    //Placeholder text    minPrice.placeholder = @"150";    //Prefix label is set as left vIEw and the text starts after that    minPrice.leftVIEw = prefixLabel;    //It set when the left prefixLabel to be displayed    minPrice.leftviewmode = UITextFIEldviewmodeAlways;    // Adds the textFIEld to the vIEw.    [self.vIEw addSubvIEw:minPrice];    // sets the delegate to the current class    minPrice.delegate = self;}-(voID)addMaxTextFIEld{    // This allocates a label    UILabel *prefixLabel = [[UILabel alloc]initWithFrame:CGRectZero];    // This sets the Font for the label    [prefixLabel setFont:[UIFont boldSystemFontOfSize:14]];    // This fits the frame to size of the text    [prefixLabel sizetoFit];    UITextFIEld *maxPrice = [[UITextFIEld  alloc] initWithFrame:                             CGRectMake(185,30)];    //for max price    maxPrice.borderStyle = UITextborderStyleRoundedRect;    maxPrice.contentVerticalAlignment =    UIControlContentVerticalAlignmentCenter;    [maxPrice setFont:[UIFont boldSystemFontOfSize:12]];    //Placeholder text is displayed when no text is typed    maxPrice.placeholder = @"300";    //Prefix label is set as left vIEw and the text starts after that    maxPrice.leftVIEw = prefixLabel;    //It set when the left prefixLabel to be displayed    maxPrice.leftviewmode = UITextFIEldviewmodeAlways;    // Adds the textFIEld to the vIEw.    [self.vIEw addSubvIEw:maxPrice];    // sets the delegate to the current class    maxPrice.delegate = self;}- (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.minPriceFIEld.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,@"categoryname": self.chosencategoryname,}                                         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
解决方法 您正在使用带有一个或多个nil值的简写@ {key,value}创建NSDictionary.

作为开发阶段的预防技术,我建议您使用以下方法检查所有价值:

NSParameterassert(self.itemSearch);NSParameterassert(self.chosencategory);// etc.

这将抛出一个断言,让你知道你的NSDictionary结构必然会失败.

现在为原因(在你的代码中)……
如注释中所述,您使用局部变量在本地覆盖类成员的范围:

// This allocates the textfIEld and sets its frameUITextFIEld *minPrice = ...

考虑使用这种结构(如果没有,当然,用IB创建UI对象,你似乎要做)

self.minPrice = ({    UITextFIEld *minPrice = [[UITextFIEld alloc] init...];    // more initialization of minPrice...    minPrice;});[self.vIEw addSubvIEw:self.minPrice];
总结

以上是内存溢出为你收集整理的ios – NSPlaceholderDictionary:尝试从对象中插入nil对象[1]全部内容,希望文章能够帮你解决ios – NSPlaceholderDictionary:尝试从对象中插入nil对象[1]所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1073809.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存