Objective c 创建字典并将值存储在Obj C中

Objective c 创建字典并将值存储在Obj C中,objective-c,Objective C,我对Obj-C和iOS开发非常陌生,已经建立了故事板和基本控制器 我现在正在尝试在字典中存储一些基本的用户输入,但对于如何设置字典还不是100%确定。我看了一下文件,但有点卡住了 基本上,我希望实现: 创建字典 从“titleTextField”访问输入值,并将其设置为键title 然后我只想使用字典中的键访问该值,然后记录它以确认 我已经粘贴了代码,看到了底部的函数,但我不能100%确定所有属性的语法是否正确。您可以在页面顶部看到@property的定义,然后我尝试在底部的addButtonT

我对Obj-C和iOS开发非常陌生,已经建立了故事板和基本控制器

我现在正在尝试在字典中存储一些基本的用户输入,但对于如何设置字典还不是100%确定。我看了一下文件,但有点卡住了

基本上,我希望实现:

  • 创建字典
  • 从“
    titleTextField
    ”访问输入值,并将其设置为键
    title
  • 然后我只想使用字典中的键访问该值,然后记录它以确认
  • 我已经粘贴了代码,看到了底部的函数,但我不能100%确定所有属性的语法是否正确。您可以在页面顶部看到@property的定义,然后我尝试在底部的
    addButtonTapped
    函数中访问这些属性

    任何帮助或指导都将不胜感激

    //  AddViewController.m
    //  TodoApp
    //
    
    #import "AddViewController.h"
    
    @interface AddViewController ()
    @property (strong, nonatomic) IBOutlet UITextView *notesTextView;
    @property (strong, nonatomic) IBOutlet UITextField *titleTextField;
    - (IBAction)addButtonTapped:(UIBarButtonItem *)sender;
    
    @end
    
    @implementation AddViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (IBAction)addButtonTapped:(UIBarButtonItem *)sender {
        NSLog(@"Add button tapped");
    
        //Create dictionary and store the values from the text fields and views
    
        NSMutableDictionary *items = [NSMutableDictionary dictionaryWithObjectsAndKeys:_titleTextField,@"title", nil];
        [items setObject:_titleTextField forKey:@"title"];
    
    
    
    
    }
    @end
    

    你在正确的轨道上。这就是你要找的东西:

    - (IBAction)addButtonTapped:(UIBarButtonItem *)sender {
        NSLog(@"Add button tapped");
    
        // Create dictionary and store the values from the text fields and views
        // This is a class method, which you can tell because it is called
        // with NSMutableDictionary as its receiver.
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
        // This is the modern way to set dictionary values. You could also
        // use [dict setObject:self.titleTextField.text forKey:@"title] , 
        // but that's outdated and less clear.
        dict[@"title"] = self.titleTextField.text;
    
        // Log out the value to confirm
        NSLog(@"title : %@", dict[@"title"]);
    }
    
    基于我在您的代码中看到的误解,我建议您阅读以下内容:


    谢谢@Riley!这段代码非常有意义,实际上更加清晰和简单。当我运行该功能(即按下模拟器中的按钮)时,我得到了这个致命错误。这很奇怪,因为我没有给setObjectForKey打电话?有什么想法吗<代码>2015-02-15 16:37:36.711 TodoApp[50163:607]***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“***setObjectForKey:object不能为零(键:标题)”错误消息告诉您错误:值(在本例中,
    self.titleTextField.text
    )为
    nil
    NSDictionary
    无法存储
    nil
    值。这意味着文本字段的
    text
    属性是
    nil
    ,或者更可能的是,
    self.titleTextField
    本身是
    nil
    。您应该使用断点或
    NSLog
    进行检查。是的,self.titleTextField不返回任何内容。我会确保它链接正确@赖利