- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有两个 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, 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, 105, 75, 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
。
作为开发阶段的预防技术,我建议您使用以下方法检查您所有的value(s)
:
NSParameterAssert(self.itemSearch);
NSParameterAssert(self.chosenCategory);
// etc.
这将抛出一个断言并让您知道您的 NSDictionary 构造注定会失败。
现在找出原因(在您的代码中)...如评论中所述,您在本地使用局部变量覆盖了类成员的范围:
// This allocates the textfield and sets its frame
UITextField *minPrice = ...
考虑使用这种构造(如果不是,当然,使用 IB 创建 UI 对象,您似乎正在这样做)
self.minPrice = ({
UITextField *minPrice = [[UITextField alloc] init...];
// more initialization of minPrice...
minPrice;
});
[self.view addSubview:self.minPrice];
关于ios - NSPlaceholderDictionary : attempt to insert nil object from objects[1],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24601377/
在检查对象是否为 nil 时,有人使用 1: if (object == nil) { //... } 有人用 2: if (nil == object) { //... } 1和2有
func InsertApData(w http.ResponseWriter, r *http.Request) { decoder := json.NewDecoder(r.Body)
为什么 nil.to_s 返回 "",而 nil.inspect 返回 "nil"(当显然 .inspect 使用 .to_s 方法) 最佳答案 "inspect" method of the Obj
运行时间 https://play.golang.org/p/sl12vfS9vP package main import "fmt" func main() { err := run()
我可以将三元条件运算符用于 if {} else {} 语句,如下所示:a ? x : y,还是问题?回答 1 : 回答 2。 是否可以使用这种格式来检查,而不是 a 是 true 还是 false,
无法弄清楚这里出了什么问题。按照设计设置说明,用谷歌搜索我能想到的一切,仍然没有运气。 undefined method `email' for # Extracted source (around
今天写一些 rspec 时,我遇到了一些意外的行为,将日期(和时间)实例与 nil 进行比较。这是一个使用原始 ruby 的示例(没有 Rails 或其他库): user@MacBook-Work
我将数据类型的非零值分配给非可选属性,然后将其分配给可选属性,最后用所述数据实例化图像。当可选项通过 if-let 子句时,它的 block 执行,抛出错误: Fatal error: Unexpec
swift 5.1 。 考虑以下。 let x: Any? = nil let y: Any = x print("x \(x)") // x nil pri
请耐心听我解释这一点, 我正在创建一个聊天室,用户可以在其中上传照片供其他人查看。当用户点击图标时,他们可以将照片上传到我的 Firebase 数据库(这已成功完成,我已经对此进行了测试)。照片的 U
我的 xCode 5.0 目前遇到一个奇怪的问题:一个对象在控制台中似乎为 nil,但同时它可以通过代码访问。 图 1:对象似乎为零 图2:NSLog(@"%@", imgDownloader) 的输
我有一个实现协议(protocol)的类,以便添加 3 个变量。我专门设置了图像变量,调试器显示该变量存在,但是在我打印它时在代码中显示为 nil,我的 if let 语句也认为该变量为 nil。 @
这个问题在这里已经有了答案: Swift 2 ( executeFetchRequest ) : error handling (5 个答案) 关闭 7 年前。 开始在 SWIFT 中学习编码,每次
两者 (not 'nil) 和 (not nil) 求值为T,那么'nil和nil有什么区别吗?那么 ''nil 呢?如果 ''nil 的计算结果为 'nil,那么 ''nil 是否也应该计算为 ni
(if '(nil nil) 'print-true 'print-false) (if '(nil) 'print-true 'print-false) 在上面的代码
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: obj.nil? vs. obj == nil 我发现了一个问题 - == nil 和 nil 哪个更好?
此代码段抛出异常: x = nil jsoned = x.to_json puts 'x.to_json=' + jsoned.inspect puts 'back=' + JSON.parse(js
基本上我有一个对象想要传递给前端。我在后端记录了它,它不是空的,但是在前端,当我提醒它时,它变成了空。 ... presentation := &presentationStruct { Obje
我创建了一个自定义错误类型来包装错误,以便在 Golang 中更轻松地进行调试。当有错误要打印时它可以工作,但现在它会引起 panic 。 Demo type Error struct { E
写一个符合我当前问题的标题有点难..我有一个 main() 函数,它使用另一个包 (database_sql) 中的函数。该函数初始化一个全局变量 sql.DB*。初始化后,变量不为nil,但是对于其
我是一名优秀的程序员,十分优秀!