Objective c 创建字典并将值存储在Obj C中
我对Obj-C和iOS开发非常陌生,已经建立了故事板和基本控制器 我现在正在尝试在字典中存储一些基本的用户输入,但对于如何设置字典还不是100%确定。我看了一下文件,但有点卡住了 基本上,我希望实现:Objective c 创建字典并将值存储在Obj C中,objective-c,Objective C,我对Obj-C和iOS开发非常陌生,已经建立了故事板和基本控制器 我现在正在尝试在字典中存储一些基本的用户输入,但对于如何设置字典还不是100%确定。我看了一下文件,但有点卡住了 基本上,我希望实现: 创建字典 从“titleTextField”访问输入值,并将其设置为键title 然后我只想使用字典中的键访问该值,然后记录它以确认 我已经粘贴了代码,看到了底部的函数,但我不能100%确定所有属性的语法是否正确。您可以在页面顶部看到@property的定义,然后我尝试在底部的addButtonT
titleTextField
”访问输入值,并将其设置为键title
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"]);
}
基于我在您的代码中看到的误解,我建议您阅读以下内容:
self.titleTextField.text
)为nil
NSDictionary
无法存储nil
值。这意味着文本字段的text
属性是nil
,或者更可能的是,self.titleTextField
本身是nil
。您应该使用断点或NSLog
进行检查。是的,self.titleTextField不返回任何内容。我会确保它链接正确@赖利