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

2024-03-23

我有两个UITextField盒子,一个用于 minPrice,一个用于 maxPrice。当我尝试运行由submitButton IBAction,使用这两个文本字段内容作为参数,它表明对象为空,并出现以下错误:

'*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]'

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

标准 ViewController.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

标准 ViewController.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

您正在创建一个NSDictionary使用简写@{ key, value}与一个或多个nil value.

作为开发阶段的预防技术,我是否建议检查您的所有value(s) using:

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];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NSPlaceholderDictionary:尝试从对象插入 nil 对象[1] 的相关文章

  • 如何为移动应用程序创建无密码登录

    我有兴趣在移动应用程序和 API 之间构建某种无密码登录 假设我可以控制两者 动机是必须登录对用户来说非常烦人并且存在安全风险 例如 用户将重复使用现有密码 我希望用户能够立即开始使用该应用程序 我想知道是否有一些可行的技术 例如 在移动设
  • 自定义 OpenVPN iOS 客户端 [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我正在开发一个自定义 iOS OpenVPN 客户端 我找到了这个原生 OpenVPN 客户端核心源码https staging op
  • ios Facebook 添加 FBNativeAdView 作为子视图

    我想使用预建视图FBNativeAdView 不想自定义 FBNative 广告 如link https developers facebook com docs reference ios current class FBNativeAd
  • 如何快速从解析中加载图像?

    我想知道是否有人可以帮助我 我是应用程序开发的新手 我正在从我的应用程序上传图像以在解析文档的帮助下毫无问题地进行解析 let imageData UIImagePNGRepresentation scaledImage let image
  • Xcode 6.4 Swift 单元测试无法编译:“GPUImage.h 未找到”“无法导入桥接标头”

    我的 Xcode 项目构建并运行良好 它有 Swift 和 Objective C 代码 它已安装 GPUImage 我向它添加了单元测试 现在它将不再编译 找不到 GPUImage h 文件 导入桥接标头失败 以下是我发现并尝试过的解决方
  • 从基元创建自定义形状

    我正在尝试通过组合原始形状来创建自定义物理形状 目标是创建一个圆形立方体 合适的方法似乎是初始化 形状 变换 我在这里找到的https developer apple com library prerelease ios documenta
  • 将 Xcode 4.5 新 XIB 文件恢复到 iOS<6

    我已经安装了Xcode 4 5 with iOS6 SDK以及其他用于测试目的的旧 SDK 从 4 3 到 6 0 很美 但是有一个BIG问题 生成一个新的 XIB 文件以兼容 iOS6 这是一个问题 因为我的应用程序需要运行在旧设备 不只
  • 如何使用存储在 Cocoa Touch 框架中的 Localized.strings?

    我想为 CocoaTouch 框架添加多语言支持 问题 可本地化的字符串我创建的文件仅被使用NSLocalizedString当它是主应用程序及其目标的一部分时 我想将其存储在框架内以将事物分开 我怎样才能使用可本地化的字符串当放置在 Co
  • 具有透明背景的 Swift 模态视图控制器 [重复]

    这个问题在这里已经有答案了 我知道这个话题很受欢迎 但我在编程语言中遇到了一些问题 事实是我仍然不明白我把代码放在哪里 好吧 我就来说说整个案子 我正在尝试制作一个与正常情况稍有不同的模态 Swift 通过单击按钮 ViewControll
  • 如何通过我的 ios 应用程序的指示打开苹果地图应用程序

    我的目标是从 ios 应用程序打开带有方向的地图应用程序 我可以打开地图应用程序 但它没有显示方向 我编写的代码如下 NSString mystr NSString alloc initWithFormat http maps apple
  • 如何在 Xamarin.iOS 应用程序中创建导航?

    我习惯于与Xamarin Forms 我用 XAML 或 C 创建一个页面并导航到它 但现在这是我第一次尝试创建一个不适合的 iOS 应用程序Xamarin Forms 我在 Windows PC 上的 Visual Studio 中进行此
  • Xcode 11 无法识别静态库的架构:MacCatalyst(又名 UIKitForMac)

    在对 2019 年 WWDC 公告感到兴奋之后 我尝试使用 Xcode 11 0 beta 针对 MacOS 编译现有的 iOS 应用程序 不幸的是 事情并没有按预期进行 Xcode 说我的静态库是为 架构构建的 为 Mac 版 UIKit
  • 如何将 CIFilter 输出到相机视图?

    我刚刚开始使用 Objective C 我正在尝试创建一个简单的应用程序 它显示带有模糊效果的相机视图 我得到了与 AVFoundation 框架一起使用的相机输出 现在 我正在尝试连接 Core 图像框架 但不知道如何连接 Apple 文
  • 无法识别的选择器调用静态 iOS 库中的类别方法

    我正在使用一些第三方软件来帮助使用 Xcode 4 3 2 编写 iPad 应用程序 该软件是开源的 通常经过设置 因此其代码将与开发人员为应用程序编写的任何代码一起编译 因为我在很多地方使用该软件 所以我决定将其构建为 iOS 模拟器的静
  • 尝试注册 RCTBridgeModule 类 RCTFileReaderModule

    尝试为名称 FileReaderModule 注册 RCTBridgeModule 类 RCTFileReaderModule 但该名称已由类 FileReaderModule 注册 尝试使用命令react native run ios在i
  • Swift - 将图像插入 PDF 不再适用于 iOS 13

    目前正在开发在我的贷款计算器应用程序上导出 PDF 的功能 我有一个预览屏幕 可以在您保存 PDF 之前显示它 预览屏幕由带有 html 的 webView 组成 其中包含占位符 我能够成功地将图像插入到正确的占位符上 并将其显示在 PDF
  • TableView 中图像的大小不正确

    我正在使用来自 URL 的图像创建一个表视图 但图像不会调整到所有视图的大小 直到我将其按入行中 知道为什么会发生这种情况吗 这是一个自定义的表格视图 我的代码是 UITableViewCell tableView UITableView
  • SwiftUI - 从 NSObject 继承的 ObservableObject 在 iOS 13 中不会更新

    我知道 这是 无法在 iOS XX 中工作 问题之一 但我完全陷入困境 所以我有一个ObservableObject继承自的类NSObject 因为我需要听委托方法UISearchResultsUpdating class SearchBa
  • GoogleSignIn ios 附加到谷歌表格

    我目前正在开发一个 iOS 应用程序 该应用程序需要写入登录用户拥有的 Google 工作表 要登录我正在使用的用户GoogleSignInpod 并附加到我正在使用的谷歌表GoogleAPIClientForREST Sheets pod
  • 弱变量中间为零

    弱变量什么时候变为零 weak var backgroundNode SKSpriteNode texture SKTexture image initialBackgroundImage backgroundNode position C

随机推荐

  • React Hook useEffect 缺少依赖项:“dispatch”

    这是我第一次使用 React js 我试图在离开此视图时删除警报 因为我不想在其他视图上显示它 但如果没有错误 我想保留成功警报以显示当我要重定向到另一个视图时 但我在 google chrome 上收到此警告Line 97 6 React
  • 找不到 Django 媒体 URL

    我遇到了一个奇怪的问题 希望其他地方有人也遇到过同样的问题 我的问题是 django 应用程序中存储的媒体无法通过 MEDIA ROOT URL 提供服务 当我尝试获取列表时使用 URL myhost media 保存在我的应用程序中的媒体
  • 如何在Windows窗体中创建垂直导航栏?

    我正在开发我的学校项目 Windows 窗体应用程序 正如你所看到的 我创建了 3 个面板 一个用于标题 一个用于导航栏 一个用于内容 我可以使标题和导航栏静态化 例如网页中的布局 并在单击按钮时更改内容吗 我创建了几个面板 并更改每个面板
  • 将程序集编译为 x64 有什么优点吗?

    假设我有一个 Net Framework 3 5 SP1 CLR 2 0 应用程序 需要在 x86 和 x64 平台上运行 还假设出于某种原因 我需要创建单独的 x86 和 x64 安装程序 由于我无论如何都有一个特定于 x64 的安装程序
  • R data.table 在 i 语句中使用 max

    这应该很简单 但由于某种原因 data table 没有达到我的预期 我想取一行中两个值的最大值来确定是否应该过滤一行 似乎发生的情况是 max 函数正在查看整个列 这不是我想要的 这是代码 gt test dt lt data table
  • 在哪里可以找到带有源代码的简约 WDM 驱动程序模板? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我遇到过内核模式驱动程序 但经验很少 这就是我想做的 有一个加载驱动程序的用户模式应用程序 让用户模式应用程序写入它 以便向它发送指令 让
  • 如何在选择中进行选择

    我有一个包含唯一 ID 字段的表 另一个字段 REF 包含对另一个数据集的 ID 字段的引用 现在我必须选择 REF 指向不存在的数据集的所有数据集 SELECT FROM table WHERE no dataset with ID RE
  • 使用 CloudFront 时,S3 上的自定义重定向规则返回 403

    我在 S3 上为我的存储桶有一个自定义重定向规则
  • “git pull”坏了

    我最近将 MacBook Pro 升级到 Snow Leopard 并且 git pull 返回 rakudo git pull git pull is not a git command See git help Did you mean
  • 将大端字节集合编组到结构中以提取值

    有一个很有洞察力的问题从字节数组中读取 C 中的 C C 数据结构 https stackoverflow com questions 2871 reading a c c data structure in c from a byte a
  • AND 和 OR 运算符在 Bash 中如何工作?

    我在 bash 中尝试了以下命令 echo this echo that echo other 这给出了输出 this other 我不明白 我的试运行是这样的 echo this echo that echo other暗示true tr
  • IE 中的空白 iFrame

    我有一个 iframe 如果来自externaldomain com的something html有css html position relative 在 IE 中 它将把 iframe 渲染为空白 所有其他浏览器都很好 有人知道解决方案
  • 谷歌浏览器闪烁

    大家好 我是新来的 不是编码员 而是我们即将于 2017 年夏天发布的项目的经理 我的开发人员和编码人员无法找到我们网站仅在 Chrome 上存在闪烁的原因 www playinera com http www playinera com
  • 实体框架:如何捕获任何错误

    我正在尝试将数据插入到具有大量数据的 SQL Server 表中not null限制 CREATE TABLE dbo Customer CustomerId int IDENTITY 1 1 NOT NULL FirstName varc
  • 从 .exe 中提取 .ico 并使用 PyQt 进行绘制的最佳方法?

    我正在寻找一种使用 Python 从 exe 文件中提取图标的方法 我知道你可以使用 win32gui 的 ExtractIconEx 函数来获取 exe 的图标 但这会返回一个 HIcon 资源句柄 这不好 因为我想使用 PyQt 绘制图
  • 如何从 PHP cli 获取 Linux 控制台 $COLUMNS 和 $ROWS?

    我目前正在为 PHP 创建一个新的简洁的 CLI 库 我想计算出它运行的控制台的宽度 高度 我尝试过很多事情 比如挖掘 ENV exec echo COLUMNS 等 但没有结果 而如果我在 bash 命令行中输入 echo COLUMNS
  • 为什么主线程不等待其他异步进程(线程)完成。 allOff 无法正常工作

    我在 for 循环内调用一个异步方法 并将其 future 添加到列表中 我不确定为什么 allOff 不等待完成所有 future 并返回部分结果 看看我的代码 我有一个被重写的方法 Overide Async CompletableFu
  • 在图像的不同部分制作悬停效果,不是html而是img

    Basically i have a blueprint for a building project i need to make it so that when they hover on specific parts of the f
  • Flutter doctor:未安装 Flutter 和 dart 插件

    这是问题 我已经安装了 flutter 和 dart 插件 但它显示我还没有 我刚开始时就新安装了 android studio 和 flutter Doctor summary to see all details run flutter
  • NSPlaceholderDictionary:尝试从对象插入 nil 对象[1]

    我有两个UITextField盒子 一个用于 minPrice 一个用于 maxPrice 当我尝试运行由submitButton IBAction 使用这两个文本字段内容作为参数 它表明对象为空 并出现以下错误 NSPlaceholder